I think there is a simple answer to my seemingly simple question, but I can be absolutely wrong. In any case, I'm new to box2dWeb, and in my Box2dWeb world, I create a floor and a simple falling object. When I "debug a draw" on my canvas, I see a box and that's it. All I want to do is infer the x position of the falling object that I created in the console.log browsers, and this does not work perfectly correctly. The .log console simply displays the starting position of my object, but the number is not updated, although my object falls inside the canvas. After hours of searching with many search engines and places like Seth Ladds tutorials, I walked over empty. I hope someone here can help. I have provided some code examples to help me explain a little better. Hope it helps. Thanks to everyone who answers.
var world;
function init() {
var b2Vec2 = Box2D.Common.Math.b2Vec2
, b2BodyDef = Box2D.Dynamics.b2BodyDef
, b2Body = Box2D.Dynamics.b2Body
, b2FixtureDef = Box2D.Dynamics.b2FixtureDef
, b2Fixture = Box2D.Dynamics.b2Fixture
, b2World = Box2D.Dynamics.b2World
, b2MassData = Box2D.Collision.Shapes.b2MassData
, b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape
, b2CircleShape = Box2D.Collision.Shapes.b2CircleShape
, b2DebugDraw = Box2D.Dynamics.b2DebugDraw
;
world = new b2World(
new b2Vec2(0, 10)
, true
);
var fixDef = new b2FixtureDef;
fixDef.density = 1.0;
fixDef.friction = 0.5;
fixDef.restitution = 0.2;
var bodyDef = new b2BodyDef;
bodyDef.type = b2Body.b2_staticBody;
bodyDef.position.x = 9;
bodyDef.position.y = 13;
fixDef.shape = new b2PolygonShape;
fixDef.shape.SetAsBox(10, 0.5);
world.CreateBody(bodyDef).CreateFixture(fixDef);
crateFixture = new b2FixtureDef;
crateFixture.density = 0.9;
crateFixture.friction = 0.5;
crateFixture.restitution = 0.5;
crateDef = new b2BodyDef;
crateDef.type = b2Body.b2_dynamicBody;
crateDef.position.x = 5;
crateDef.position.y = 5;
crateDef.angle = 65;
crateFixture.shape = new b2PolygonShape;
crateFixture.shape.SetAsBox(2, 2);
world.CreateBody(crateDef).CreateFixture(crateFixture);
var debugDraw = new b2DebugDraw();
debugDraw.SetSprite(document.getElementById("canvas").getContext("2d"));
debugDraw.SetDrawScale(30.0);
debugDraw.SetFillAlpha(0.3);
debugDraw.SetLineThickness(1.0);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
world.SetDebugDraw(debugDraw);
window.setInterval(update, 1000 / 60);
};
function update() {
world.Step(
1 / 60
, 10
, 10
);
world.DrawDebugData();
world.ClearForces();
console.log('the crate is located at ' + crateDef.position.x);
};