I use box2dweb. I am trying to develop a game. At some point I need to find out the contact point between a "Circle" and "Box". All I know is it can be done using b2ContactListener. We can receive contact data by implementing b2ContactListener using Post-Solve Event. Please help!
Asked
Active
Viewed 3,667 times
8
-
1This might help: http://www.iforce2d.net/b2dtut/collision-anatomy – iforce2d Jun 07 '12 at 03:30
1 Answers
16
You are on the right track there are various events you can hook into with the b2ContactListener:
var b2Listener = Box2D.Dynamics.b2ContactListener;
//Add listeners for contact
var listener = new b2Listener;
listener.BeginContact = function(contact) {
//console.log(contact.GetFixtureA().GetBody().GetUserData());
}
listener.EndContact = function(contact) {
// console.log(contact.GetFixtureA().GetBody().GetUserData());
}
listener.PostSolve = function(contact, impulse) {
if (contact.GetFixtureA().GetBody().GetUserData() == 'ball' || contact.GetFixtureB().GetBody().GetUserData() == 'ball') {
var impulse = impulse.normalImpulses[0];
if (impulse < 0.2) return; //threshold ignore small impacts
world.ball.impulse = impulse > 0.6 ? 0.5 : impulse;
console.log(world.ball.impulse);
}
}
listener.PreSolve = function(contact, oldManifold) {
// PreSolve
}
this.world.SetContactListener(listener);
Just remove the postSolve code and depending on what you need to do hook into the appropriate events.
Seth ladd has some great articles on his blog about collision/reacting to them. This is where I picked up these bits so full credit goes to him.
I hope this helps.
Thanks, Gary

Gary
- 747
- 3
- 10
- 24
-
I wrote a blog on it. If you want you can check it out. http://thenightowl.xp3.biz/ – Shekhar Aug 14 '12 at 10:46
-
-
I'm sorry but I'm a complete noob with Box2D and if I add my own listener with empty `BeginContact` and `EndContact` functions my script throws an error and nothing is rendered. All I want to do is change an objects linear damping when it touches the ground and set it back to normal when it's in the air. – powerbuoy Apr 02 '15 at 16:58
-