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
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.
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
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