1
votes

I've created a pretty simple setup using Cocos2d (2.0) and Box2d that comes packaged with it. I have a few bodies in my world, but don't have sprites linked up with them yet and I want to debug their orientations, positions, etc.

This seems like a pretty standard task, but I could not find out how to do this easily. From my research it seems related to these methods:

_world->SetDebugDraw(...);
_world->DrawDebugData(...);
// and the GLES-Render class

Help?

2
Doesn't the default cocos2d + Box2D project include GLES-Render and a sample debug draw layer? If not, check out Kobold2D at kobold2d.com and start a cocos2d+Box2D project. Includes the debug draw layer.LearnCocos2D

2 Answers

4
votes

I figured it out in case anyone else stumbles across this.

  1. In your initialization, you want to create a debug draw object (GLESDebugDraw comes with Cocos2d+Box2d).
  2. Set the flags to specify what you want drawn (shapes, center of gravity, joints, etc.).
  3. Assign it to your world object.

b2Draw *debugDraw = new GLESDebugDraw(PTM_RATIO);

debugDraw->SetFlags(GLESDebugDraw::e_shapeBit);

_world->SetDebugDraw(debugDraw);

Then, the trick is that you need to override ccLayer's draw method and call:

_world->DrawDebugData();

It must be in the draw method otherwise this won't work. I initially tried to put it in my own scheduled method (where I call _world->step()) and this did not work.

0
votes

IN The coco2dx v2.2 this is done as

//Write this in init()

_debugDraw = new GLESDebugDraw(PTM_RATIO);
  _world->SetDebugDraw(_debugDraw);


  uint32 flags = 0;
  flags += b2Draw::e_shapeBit;
  flags += b2Draw::e_jointBit;
  flags += b2Draw::e_aabbBit;
  flags += b2Draw::e_pairBit;
  flags += b2Draw::e_centerOfMassBit;
  _debugDraw->SetFlags(flags);

//////////////////////////////////////////////////

void HelloWorld::draw() {

CCLayer::draw();

if CC_ENABLE_BOX2D_INTEGRATION

ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );

kmGLPushMatrix();

_world->DrawDebugData();

kmGLPopMatrix();

endif

}

Check in Application.mk file if there is

APP_CPPFLAGS := -frtti -DCC_ENABLE_CHIPMUNK_INTEGRATION=1 -DCOCOS2D_DEBUG=1

then replace it with

APP_CPPFLAGS := -frtti -DCC_ENABLE_BOX2D_INTEGRATION=1 -DCOCOS2D_DEBUG=1