Box2d debug drawing for cocos2d-x 3.0

Guy please share a code that you use to debug draw with cocos2d-x 3.0. That would save a lot of time not only for me.

1 Like

@naghekyan Share with you my version for Cocos2d-x V3.X, goodluck! B2DebugDrawLayer.zip (2.6 KB)

And this is update GLES-Render.zip (11.6 KB) for Cocos2d-x V3.2

1 Like

try this
http://sillyatomstudios.com/blog/2014/09/27/box2d-debug-draw-in-cocos2dx-3-2/

@sillyatom @christmas Guys I tried almost everything but still no success. Please suggest if you got success

@saffronstate can you give some details What u have done

@saffronstate & sillyatom:
You guys should focus this point if you are trying use my code on cocos2d-x V3.2 or later:
With “draw” function in “.h” file, it should be :
virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
And in “.cpp” file:

void HelloWorld::draw(Renderer renderer, const Mat4 &transform, bool transformUpdated)
{
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
Director
director = Director::getInstance();
CCASSERT(nullptr != director, “Director is null when seting matrix stack”);
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
world->DrawDebugData();
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
}

Goodluck!

1 Like

@Harion @sillyatom thank for your replies and kind interest.
@Harion, I tried your solution and it worked now. Meanwhile I also tested the default Physics that comes with cocos2d-x v3.2. And currently facing the issues:

circle body position issue and collision misunderstood

In my game (earlier Obj-C + box2d) i used kinematic bodies and manually handled activities for the kinemtatic bodies. But now in cocos2d-x v3.2 i dont see a kinematic concept. is it true?

@naghekyan
if this still relevant to you this is how to do it in cocos2d-x ver 3.4
this is the Sprite where all my box2d action is happening

void BackgroundSprite::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
   
     Sprite::draw(renderer, transform, flags);
    _customCmd.init(_globalZOrder, transform, flags);
    _customCmd.func = CC_CALLBACK_0(BackgroundSprite::onDraw, this, transform, flags);
    renderer->addCommand(&_customCmd); 
    

 
}

void BackgroundSprite::onDraw(const Mat4 &transform, uint32_t flags)
{
    Director* director = Director::getInstance();
    CCASSERT(nullptr != director, "Director is null when seting matrix stack");
    director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
    director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform);
	
    GL::enableVertexAttribs( cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION );
   
    m_world->DrawDebugData();
	
	CHECK_GL_ERROR_DEBUG();
    
    director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
}

taken from the test-cpp code .

Have any of you guys been able to implement debug drawings for Cocos2d-x v 3.10?

The code samples in this book won’t do it, also tried the code posted by @Meir_yanovich, but still can’t see anything related to Box2d drawings.

Hey , hear me out . I solved this problems . Still working now at cocos2d-x 3.10
Download this :
B2Debug.zip (5.5 KB)

Then , add those code to you scene that run Box2d :

  1. Virtual draw function

void GameScene::draw(Renderer renderer, const Mat4 &transform, uint32_t flags)
{
/GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
Director
director = Director::getInstance();
CCASSERT(nullptr != director, “Director is null when seting matrix stack”);
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
world->DrawDebugData();
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
/
}

2.this shit to init()

this->addChild(B2DebugDrawLayer::create(world, SCALE_RATIO), 99);

DONE . Hope you fix this problem.

Thank you mate. GLES-Render.zip worked on 3.10 and I didnt need to add any draw function. Just the this->addChild(B2DebugDrawLayer::create(world, RATIO), 1000);

Hi There, I have download B2Debug.zip, I have added those lines to my Scene and I have put the line “this->addChild(B2DebugDrawLayer::create(world, SCALE_RATIO), 1000);” in the layer also… but still nothing is shown… Is there anything that I have missed to have the physics debug working?

My cocos version is 3.10.

Thank you!!!

contact me ! I will help you throught teamviewer.

I’ve been struggling with this for a few hours now. I’m using 3.10, and have added the necessary patches/files, but still am seeing nothing being drawn. I put some breakpoints in the DrawDebugData() method and can clearly see the right shapes trying to be drawn. I went so far as to change the radius and position to static values I know should work. Changed the colors, the layer order, everything - but still nothing is being drawn ! >.< I can’t really afford a new laptop, so I’d prefer not to throw it against a wall - but I may have to soon.

If you try the following code, seems b2debugDraw is not showing any debug draw anymore.
Any thoughts?
auto bg = cocos2d::LayerColor::create(Color4B(255, 255, 255, 255));
this->addChild(bg);
this->addChild(B2DebugDrawLayer::create(world, 32), 99);