In the book Pro HTML5 games there is a part that shows how to create 2 bodies in Box2dWeb. For example, a circle and a rectangle, one could do the following:
function createRectangularBody(){
var bodyDef = new b2BodyDef;
bodyDef.type = b2Body.b2_dynamicBody;
bodyDef.position.x = 40/scale;
bodyDef.position.y = 100/scale;
var fixtureDef = new b2FixtureDef;
fixtureDef.density = 1.0;
fixtureDef.friction = 0.5;
fixtureDef.restitution = 0.3;
fixtureDef.shape = new b2PolygonShape;
fixtureDef.shape.SetAsBox(30/scale,50/scale);
var body = world.CreateBody(bodyDef);
var fixture = body.CreateFixture(fixtureDef);
}
And
function createCircularBody(){
var bodyDef = new b2BodyDef;
bodyDef.type = b2Body.b2_dynamicBody;
bodyDef.position.x = 130/scale;
bodyDef.position.y = 100/scale;
var fixtureDef = new b2FixtureDef;
fixtureDef.density = 1.0;
fixtureDef.friction = 0.5;
fixtureDef.restitution = 0.7;
fixtureDef.shape = new b2CircleShape(30/scale);
var body = world.CreateBody(bodyDef);
var fixture = body.CreateFixture(fixtureDef);
}
To create the bodies, it is then called both functions:
createRectangularBody();
createCircularBody();
Afterwards, it is called a method that draws the bodies, so we have
createRectangularBody();
createCircularBody();
setupDebugDraw();//Draws the bodies defined by functions above
My questions is, inside both functions we have the same objects bodyDef
and fixtureDef
and then proceed to change attributes of these objects.
After that, we pass both objects to the world.CreateBody()
method and then store then in variable body
and fixture
. How come there is no conflict? Are the objects created treated as different since they are in different functions? I thought that the function declared after the other would modify the objects and the whole thing wouldn't work, but that wasn't the case.