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

纵饮孤独 提交于 2019-12-02 11:15:38

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

You CANNOT bring bodies to front. if you want to see your bodies behind your sprites you can set their opacity as sprite->setOpacity(100).

i use the following code to draw

void GameLayer:: setPhysics() {
b2Vec2 gravity = b2Vec2(0.0f,0.0f);// Initializing World
world = new b2World(gravity);

m_debugDraw = new GLESDebugDraw( PTM_RATIO );
world->SetDebugDraw(m_debugDraw);

world->SetAllowSleeping(false);

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);

contactListener = new MyContactListener(this);
world->SetContactListener(contactListener);
}

which is same as yours.... can you please xplain your problem a bit more

For Debug Draw You Need Two File

File Download Link

After that

Inclue that file

and create a Vaiable in .h file

GLESDebugDraw *debugDraw;

and in .ccp file

b2Vec2 gravity;
gravity.Set(0.0f, -9.8f);
this->world = new b2World(gravity);

this->debugDraw = new GLESDebugDraw(PTM_RATIO);
this->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;
this->debugDraw->SetFlags (flags);

in . h file

void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags);

in .cpp

void HelloWorld::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
    Layer::draw(renderer, transform, flags);
    Director* director = Director::getInstance();
    GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION );
    director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
    world->DrawDebugData();
    director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
}

NOW Enjoy Debug draw

You could simply add another layer above your current layer.

And you can add the code you post here in the up layer then everything should be working.

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