Skip to content

Instantly share code, notes, and snippets.

@mlogan
Created August 1, 2014 01:09
Show Gist options
  • Save mlogan/04a7de8d5e3792b398ab to your computer and use it in GitHub Desktop.
Save mlogan/04a7de8d5e3792b398ab to your computer and use it in GitHub Desktop.
// Generated by CoffeeScript 1.6.3
(function() {
var Box2D, b1, b2, beginContact, contactCb, contactCount, endContact, f1, f2, i, world, _createBody, _createCollider, _i, _j;
Box2D = require('./box2d');
_createCollider = function(body, id, radius) {
var fixtureDef, shape;
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 = function(x, y) {
var bodyDef, pos;
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 = function(thsPtr, contactPtr) {
var contact, fixtureA, fixtureB;
contact = Box2D.wrapPointer(contactPtr, Box2D.b2Contact);
fixtureA = contact.GetFixtureA();
fixtureB = contact.GetFixtureB();
console.log('BEGIN', fixtureA.GetUserData(), fixtureB.GetUserData());
return contactCount++;
}
}
]);
Box2D.customizeVTable(contactCb, [
{
original: Box2D.b2ContactListener.prototype.EndContact,
replacement: endContact = function(thsPtr, contactPtr) {
var contact, fixtureA, fixtureB;
contact = Box2D.wrapPointer(contactPtr, Box2D.b2Contact);
fixtureA = contact.GetFixtureA();
fixtureB = contact.GetFixtureB();
return 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 = _i = 0; _i < 50; i = ++_i) {
world.Step(0.1, 2, 2);
}
b2.SetLinearVelocity(new Box2D.b2Vec2(0, 0.5));
for (i = _j = 0; _j < 100; i = ++_j) {
world.Step(0.1, 2, 2);
}
return;
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment