Search code examples
debuggingbox2dcocos2d-xcocos2d-x-3.0

How to enable Box2d debug draw with Coco2d-x 3.0 beta 2


How I should enable debug draw with Cocos2d-x 3.0? There are some codes that do that with cocos2d-x 2.0 but they don't even compile on Cocos2d-x. Unfortunately I am new to both cocos2d-x and Box2d and I don't know how to port them. That would be great if you would share the method how you draw shapes.

EDIT:

I have found this:

http://blog.csdn.net/tian2kong/article/details/20386213

I have overridden the draw method of my applications main Layer like this:

void HelloWorld::draw()
{
    GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION);
    kmGLPushMatrix();
    m_world->drawDebugData();
    kmGLPopMatrix();
}

And also have done this:

GLESDebugDraw *debugDraw = new GLESDebugDraw(PTM_RATIO);
m_world.SetDebugDraw(debugDraw);
uint32 flags = 0;
flags += b2Draw::e_shapeBit;
/*
flags += b2Draw::e_jointBit;
flags += b2Draw::e_centerOfMassBit;
flags += b2Draw::e_aabbBit;
flags += b2Draw::e_pairBit;
*/
debugDraw->SetFlags(flags);

And it worked! But when I addChild a sprite, the shapes stay blow my sprites. How to bring them front?


Solution

  • Open spotlight and search for GLES-Render.cpp. This file will be there in a path in the cocos2dx folder. Open the enclosing folder and drag the files GLES-Render.h and GLES-Render.cpp to the project. Then add the following code in the init method

    b2Draw *m_debugDraw = new  GLESDebugDraw(PTM_RATIO);
    uint32 flags = 0;
    flags += b2Draw::e_shapeBit;
    flags += b2Draw::e_jointBit;
    flags += b2Draw::e_aabbBit;
    flags += b2Draw::e_pairBit;
    flags += b2Draw::e_centerOfMassBit;
    m_debugDraw->SetFlags(flags);
    H_world->SetDebugDraw(m_debugDraw); 
    

    Then add the draw method.

    void HelloWorld::draw()
    {
    kmGLPushMatrix();
    H_world->DrawDebugData();
    kmGLPopMatrix();
    }
    

    Don't forget to include GLES-Render.h