VarunJi
VarunJi

Reputation: 694

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.

Upvotes: 3

Views: 1590

Answers (1)

VarunJi
VarunJi

Reputation: 694

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;
}

Upvotes: 4

Related Questions