Box2d debug drawing for cocos2d-x 3.0

Box2d debug drawing for cocos2d-x 3.0
0

#1

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.


#2

#4

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


#5

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


#6

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


#7

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


#8

@saffronstate can you give some details What u have done


#9

@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!


#10

@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?


#11

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


#12

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.


#13

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.


Box2d debug draw?
#14

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


#15

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


#16

contact me ! I will help you throught teamviewer.


#17

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.


#18

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