Time for action – creating a ground in the world
Carry out the following steps to create a static ground:
- Open the
box2dcargame.js
JavaScript file. - Define the following
pxPerMeter
variable in the file; this is the unit setting in the Box2D world:var pxPerMeter = 30; // 30 pixels = 1 meter
- Add the following function to the end of the JavaScript file; this creates a fixed body as the playground:
function createGround() { var bodyDef = new b2BodyDef; var fixDef = new b2FixtureDef; bodyDef.type = b2Body.b2_staticBody; bodyDef.position.x = 250/pxPerMeter; bodyDef.position.y = 370 /pxPerMeter; fixDef.shape = new b2PolygonShape(); fixDef.shape.SetAsBox(250/pxPerMeter, 25/pxPerMeter); fixDef.restitution = 0.4; // create the body from the definition. var body = carGame.world.CreateBody(bodyDef); body.CreateFixture(fixDef); return body; }
- Call the
createGround
function in theinitGame
function after we have created the world as follows:createGround();
- As we are still...