Even tough Box2D doesn’t display anything on the screen it has a debug mode that you can use to see what Box2D is actually doing. Very useful of course. Now let’s see how to create a debug mode.

Setting the debug mode:

var debug_draw:b2DebugDraw = new b2DebugDraw();

var debug_sprite:Sprite = new Sprite();

addChild(debug_sprite);

debug_draw.SetSprite(debug_sprite);

debug_draw.SetFillAlpha(0.3);

debug_draw.SetDrawScale(SCALE);

debug_draw.SetFlags(b2DebugDraw.e_shapeBit);

world.SetDebugDraw(debug_draw);

You need to pass a Sprite to the b2DebugDraw object and of course add this Sprite on the screen so you can see the Box2D objects. You can debug at a different scale but usually you pass the same scale value. Note that all the examples in this article will be referencing a public static const called SCALE and set to a value of 30. Also the setFlags() method allows you to see different box2D features. You can set alpha for the fill and the lines. Feel free to experiment.
Finally let’s update our debug mode as well:

private function updateWorld(e:TimerEvent):void

{

    world.Step(1 / 30, 10, 10);

    world.DrawDebugData();

    world.ClearForces();

}

That’s it, now everything we create in Box2D will be shown in our debug Sprite. Here again that was pretty easy. Now to see something we of course need to create something so let’s see that now.