Skip to content

Instantly share code, notes, and snippets.

@BrianMacIntosh
Last active June 4, 2019 07:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrianMacIntosh/fefca14bcc5ff82491f3 to your computer and use it in GitHub Desktop.
Save BrianMacIntosh/fefca14bcc5ff82491f3 to your computer and use it in GitHub Desktop.
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);
Copy link

ghost commented Oct 3, 2015

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