Skip to content

Instantly share code, notes, and snippets.

@mlogan
Created August 1, 2014 01:08
Show Gist options
  • Save mlogan/086c0b29ba5eafcd3af6 to your computer and use it in GitHub Desktop.
Save mlogan/086c0b29ba5eafcd3af6 to your computer and use it in GitHub Desktop.
Reproduction for box2d.js/emscripten bug.
Box2D = require './box2d'
_createCollider = (body, id, radius) ->
shape = new Box2D.b2CircleShape()
shape.set_m_radius radius
fixtureDef = new Box2D.b2FixtureDef()
fixtureDef.set_density(1)
fixtureDef.set_shape(shape)
fixtureDef.set_userData(id)
return body.CreateFixture fixtureDef
_createBody = (x, y) ->
pos = new Box2D.b2Vec2(x, y)
bodyDef = new Box2D.b2BodyDef()
bodyDef.set_type Box2D.b2_dynamicBody
bodyDef.set_position(pos)
return world.CreateBody bodyDef
contactCount = 0
contactCb = new Box2D.b2ContactListener()
Box2D.customizeVTable(contactCb, [{
original: Box2D.b2ContactListener.prototype.BeginContact,
replacement:
beginContact = (thsPtr, contactPtr) ->
contact = Box2D.wrapPointer(contactPtr, Box2D.b2Contact)
fixtureA = contact.GetFixtureA()
fixtureB = contact.GetFixtureB()
console.log 'BEGIN', fixtureA.GetUserData(), fixtureB.GetUserData()
contactCount++
}])
Box2D.customizeVTable(contactCb, [{
original: Box2D.b2ContactListener.prototype.EndContact
replacement:
endContact = (thsPtr, contactPtr) ->
contact = Box2D.wrapPointer(contactPtr, Box2D.b2Contact)
fixtureA = contact.GetFixtureA()
fixtureB = contact.GetFixtureB()
console.log 'END', fixtureA.GetUserData(), fixtureB.GetUserData()
}])
world = new Box2D.b2World(new Box2D.b2Vec2(0, 0))
world.SetContactListener(contactCb)
b1 = _createBody(1, 1)
f1 = _createCollider b1, 1, 1
b2 = _createBody(1, 5)
f2 = _createCollider b2, 2, 1
b2.SetLinearVelocity(new Box2D.b2Vec2(0, -0.5))
while contactCount == 0
world.Step(0.1, 2, 2)
for i in [0...50]
world.Step(0.1, 2, 2)
b2.SetLinearVelocity(new Box2D.b2Vec2(0, 0.5))
for i in [0...100]
world.Step(0.1, 2, 2)
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment