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

╄→尐↘猪︶ㄣ 提交于 2019-12-04 06:02:18

问题


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?


回答1:


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




回答2:


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




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/22282391/how-to-enable-box2d-debug-draw-with-coco2d-x-3-0-beta-2

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