Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Sample code showing how to create a contact listener in box2d.js
//sample code showing how to create a contact listener with
//kripken's Javascript port of Box2D.js
//https://github.com/kripken/box2d.js/
var gravity = new Box2D.b2Vec2(0.0, 10.0);
var world = new Box2D.b2World(gravity);
var contactListener = new Box2D.JSContactListener();
contactListener.BeginContact = function(contact)
{
contact = Box2D.wrapPointer(contact, Box2D.b2Contact);
//[contact logic here]
}
contactListener.EndContact = function(contact)
{
contact = Box2D.wrapPointer(contact, Box2D.b2Contact);
//[contact logic here]
}
contactListener.PreSolve = function(contact, oldManifold)
{
contact = Box2D.wrapPointer(contact, Box2D.b2Contact);
oldManifold = Box2D.wrapPointer(oldManifold, Box2D.b2Manifold);
//[contact logic here]
}
contactListener.PostSolve = function(contact, impulse)
{
contact = Box2D.wrapPointer(contact, Box2D.b2Contact);
impulse = Box2D.wrapPointer(impulse, Box2D.b2ContactImpulse);
//[contact logic here]
}
world.SetContactListener(contactListener);

Thanks, this helps. I think the reason I wasn't able to get ahold of the "JSContactListener" was because it wasn't apart of the "b2" namespace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment