Cocos2d + Box2d - how to debug/show bodies?

微笑、不失礼 提交于 2019-12-06 15:49:45

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/12990475/cocos2d-box2d-how-to-debug-show-bodies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!