Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collision detection without impecting in Box2d Javascript

I am creating a Car Race Game, Where i need help in Car over some rough surface area by Box2d Javascript.

  1. I created Car game by following link http://www.codekites.com/make-racing-car-box2d-javascript/
  2. Also created obstacles.

Question:

  1. Now i want such obstacles, which should not be collide with Car. But when Car goes over that shape, then Car become slow.

So please help me, how to go with that. i found IsSensor, but that is not working. So please let me know, how to go with this.

like image 392
VarunJi Avatar asked Sep 27 '26 22:09

VarunJi


1 Answers

Aahhhh... Finally Got the solution from stackoverflow itself. here it is.

How to detect collision but do not collide in box2d?

i was using wrong keyword. its is

fixturedef.isSensor = true; 

Thats It ;) and to detect the collision we have to write the listener like this

 world.SetContactListener(listener);
 var listener = new Box2D.Dynamics.b2ContactListener;
 listener.BeginContact = function(contact) {
    // console.log(contact.GetFixtureA().GetBody().GetUserData());
    div = document.getElementById("textUI");
    div.innerHTML = "Come "+contact.GetFixtureA().GetBody();
    defaultCarSpeed = defaultCarSpeed/2;
}

listener.EndContact = function(contact) {
    // console.log(contact.GetFixtureA().GetBody().GetUserData());
    div = document.getElementById("textUI");
    div.innerHTML = "Go "+contact.GetFixtureA().GetBody();
    defaultCarSpeed = defaultCarSpeed*2;
}
like image 158
VarunJi Avatar answered Sep 30 '26 12:09

VarunJi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!