Time for action – drawing the physics world into the Canvas
Carry out the following steps to draw the useful debug view:
- First, open the
box2dcargame.js
JavaScript file:var shouldDrawDebug = false;
- Add a function that draws the debugging lines:
function showDebugDraw() { shouldDrawDebug = true; //setup debug draw var debugDraw = new b2DebugDraw(); debugDraw.SetSprite(document.getElementById('game').getContext('2d')); debugDraw.SetDrawScale(pxPerMeter); debugDraw.SetFillAlpha(0.3); debugDraw.SetLineThickness(1.0); debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit); carGame.world.SetDebugDraw(debugDraw); carGame.world.DrawDebugData(); }
- Add a
showDebugDraw
function call at the end of theinitGame
method:showDebugDraw();
- Now, reopen the game in a browser, and we should see the outline of the ground body in the canvas, as shown in the following screenshot:
What just happened?
We have just defined a method that asks the Box2D engine...