Using debug draw

While creating an object of the GLESDebugDraw class, we need only pass in the pixel-to-meter ratio so the class knows exactly how much to scale the rendering of our physics objects. This class also gives us the option of choosing the elements we want to be drawn on to the screen. For this game, we only want our bodies' shapes to be drawn so all other flags are commented. Doing this will not render the shapes on the screen and we must add the following code to our layer's draw function:

#ifdef ENABLE_DEBUG_DRAW
void GameWorld::draw()
{
  CCLayer::draw();
  ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
  kmGLPushMatrix();
  world_->DrawDebugData();
  kmGLPopMatrix();
}
#endif

An important thing to remember is that the GLESDebugDraw class is intended for debugging purposes only and should be turned off unless absolutely required. This is why the preceding code is wrapped inside a pre-processor conditional. You might use this class the most in the initial stages of developing a game when you're still creating the world and all its bodies, fixtures, and joints. So when you're done figuring out the sizes of the various shapes, be sure to comment the definition of this pre-processor directive in GameGlobals.h.

The following screenshot has been taken mid-game to show you the output of calling the DrawDebugData function of the b2World class. The red rectangle is the dynamic body of the clown, the green rectangles are the static bodies of the collectibles, and the green segment is the trampoline/platform over which the clown will bounce:

Using debug draw
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.237.123