"Avoided Making My Own js physics engine by compiling bullet from C++ "
var world = new b2World(new b2Vec2(0, 10), true);
world.b2dScale = 30;
// Step one: creating the fixture
var marioFixture = new b2FixtureDef;
marioFixture.restitution = 0.0; marioFixture.friction = 0.1; marioFixture.density = 0.4 / ratioHeight; marioFixture.shape = new b2PolygonShape; marioFixture.shape.SetAsBox( 30 / SCALE, 30 / SCALE);
//Step two: body definition var marioBodyDef = new b2BodyDef; marioBodyDef.type = b2Body.b2_dynamicBody; marioBodyDef.position.x = 100 / SCALE; marioBodyDef.position.y = 100 / SCALE;
var b2dMario = game.b2dWorld.CreateBody(marioBodyDef); b2dMario.CreateFixture(marioFixture);
var groundBodyDef = new b2BodyDef; //setting this as a static object! groundBodyDef.type = b2Body.b2_staticBody; // positions the center of the object (not upper left!) groundBodyDef .position.x = canvas.width / 2 / SCALE; groundBodyDef .position.y = (canvas.height / SCALE) - 1; groundFixDef.shape = new b2PolygonShape; // half width, half height. groundFixDef.shape.SetAsBox((canvas.width / SCALE) / 2, 0.5 / 2);
//then we create a ground var ground =
world.CreateBody(groundBodyDef ).CreateFixture(groundFixDef);
//First we set up a direction by with a vector
var v = new b2Vec2((game.controls.right - game.controls.left) * 0.1, 0);
//then we can apply this force to a body!
mario.ApplyImpulse(v, mario.GetWorldCenter());
function update() { requestAnimationFrame(update);
world.Step(1 / 60, 10, 10 ; world.DrawDebugData(); world.ClearForces(); mario.update(); };
context.drawImage(marioImage,
mario.GetBody().GetPosition().x * SCALE ,
mario.GetBody().GetPosition().y * SCALE)
//setup debug draw
var debugDraw = new b2DebugDraw();
debugDraw.SetSprite(document.getElementById("c").getContext("2d"));
debugDraw.SetDrawScale(SCALE);
debugDraw.SetFillAlpha(0.3);
debugDraw.SetLineThickness(1.0);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
world.SetDebugDraw(debugDraw);
revoluteJointDef = new b2RevoluteJointDef(); revoluteJointDef.bodyA = firstPlatform; revoluteJointDef.bodyB = secondPlatform;
revoluteJoint = world.CreateJoint(revoluteJointDef);
Distance