[Share] Box2d debug drawing for 2.0.1 (2012-06-29)

i can add a sprite in the layer, but not in the level, which is kinda annoying coz i want to take care of the debug draing in the level, not in the layer. does this mean i have to move all my physics etc to the layer??

making the level a layer as well makes it possible to add a sprit5e top the level as well. he debugdrawing still doesnt work tho :s

@hans hans

I guess position of your box2d object is the issue.
any position value in for Box2d suppose to be divided by PTM_RATIO which is 32
it’s Pixel To Meter value.

you can declare PTM_RATIO in you .ccp file as
#define PTM_RATIO 32

so your method myBodyDef.position.Set(0, 20); will be myBodyDef.position.Set(0, 20/ PTM_RATIO);

so in all position.Set() all values will be divided by PTM_RATIO

Hope this solve your issue.

Paresh Indulkar wrote:

@hans hans
>
I guess position of your box2d object is the issue.
any position value in for Box2d suppose to be divided by PTM_RATIO which is 32
it’s Pixel To Meter value.
>
you can declare PTM_RATIO in you .ccp file as
#define PTM_RATIO 32
>
so your method myBodyDef.position.Set(0, 20); will be myBodyDef.position.Set(0, 20/ PTM_RATIO);
>
so in all position.Set() all values will be divided by PTM_RATIO
>
Hope this solve your issue.

allthough i was guessing this might solve the issue, it didnt. anýway, debugdraw should take care of drawing to the screen (and converting ptm, etc), shouldnt it^^.

hans hans wrote:

Paresh Indulkar wrote:
> @hans hans
>
> I guess position of your box2d object is the issue.
> any position value in for Box2d suppose to be divided by PTM_RATIO which is 32
> it’s Pixel To Meter value.
>
> you can declare PTM_RATIO in you .ccp file as
> #define PTM_RATIO 32
>
> so your method myBodyDef.position.Set(0, 20); will be myBodyDef.position.Set(0, 20/ PTM_RATIO);
>
> so in all position.Set() all values will be divided by PTM_RATIO
>
> Hope this solve your issue.
>
allthough i was guessing this might solve the issue, it didnt. anýway, debugdraw should take care of drawing to the screen (and converting ptm, etc), shouldnt it^^.

my only guess is that something is wrong with the position of the view, because if have followed debugdraw and it seems to actually draw the objects. so i am stumped.

debugdraw didn’t take care of converting ptm, it just show the box2d object where they are. so if your object position is out of screen it will not be visible to you

you are using update for debugdraw, it suppose to be in draw method like this:

void HelloWorld::draw()
{
//
// IMPORTANT:
// This is only for debug purposes
// It is recommend to disable it
//
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
kmGLPushMatrix();
world->DrawDebugData();
kmGLPopMatrix();
}

override draw funtion in you .h file
virtual void draw();

hans hans wrote:

hans hans wrote:
> Paresh Indulkar wrote:
> > @hans hans
> >
> > I guess position of your box2d object is the issue.
> > any position value in for Box2d suppose to be divided by PTM_RATIO which is 32
> > it’s Pixel To Meter value.
> >
> > you can declare PTM_RATIO in you .ccp file as
> > #define PTM_RATIO 32
> >
> > so your method myBodyDef.position.Set(0, 20); will be myBodyDef.position.Set(0, 20/ PTM_RATIO);
> >
> > so in all position.Set() all values will be divided by PTM_RATIO
> >
> > Hope this solve your issue.
>
> allthough i was guessing this might solve the issue, it didnt. anýway, debugdraw should take care of drawing to the screen (and converting ptm, etc), shouldnt it^^.
>
my only guess is that something is wrong with the position of the view, because if have followed debugdraw and it seems to actually draw the objects. so i am stumped.

Paresh Indulkar wrote:

debugdraw didn’t take care of converting ptm, it just show the box2d object where they are. so if your object position is out of screen it will not be visible to you

nope. uses ptm.

Paresh Indulkar wrote:

you are using update for debugdraw, it suppose to be in draw method like this:
>
void HelloWorld::draw()
{
//
// IMPORTANT:
// This is only for debug purposes
// It is recommend to disable it
//
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
kmGLPushMatrix();
world->DrawDebugData();
kmGLPopMatrix();
}
>
override draw funtion in you .h file
virtual void draw();
>
>
hans hans wrote:
> hans hans wrote:
> > Paresh Indulkar wrote:
> > > @hans hans
> > >
> > > I guess position of your box2d object is the issue.
> > > any position value in for Box2d suppose to be divided by PTM_RATIO which is 32
> > > it’s Pixel To Meter value.
> > >
> > > you can declare PTM_RATIO in you .ccp file as
> > > #define PTM_RATIO 32
> > >
> > > so your method myBodyDef.position.Set(0, 20); will be myBodyDef.position.Set(0, 20/ PTM_RATIO);
> > >
> > > so in all position.Set() all values will be divided by PTM_RATIO
> > >
> > > Hope this solve your issue.
> >
> > allthough i was guessing this might solve the issue, it didnt. anýway, debugdraw should take care of drawing to the screen (and converting ptm, etc), shouldnt it^^.
>
> my only guess is that something is wrong with the position of the view, because if have followed debugdraw and it seems to actually draw the objects. so i am stumped.

sry paresh. but i would like to get some qualified answers here.

I too face so many issues like this for over 20 days but after doing R&D and trying so many different things, now all set & working properly. :slight_smile:
sorry it didn’t help you out, instead of waiting for qualified answer you can keep trying by the time. All the best. :wink:

@hans hans
I would try attaching a sprite to the body and see if that is getting shown. If not, there is a serious positioning problem and, like Paresh Indulkar said, you need to fix issues with the PTM_RATIO.

Cookiebit Team wrote:
>

Hello Ville S.
>
Try to change to:
>
[…]
>
:slight_smile:

Thanks!

It works now. :slight_smile:

Jeffrey Walraven wrote:

@hans hans
I would try attaching a sprite to the body and see if that is getting shown. If not, there is a serious positioning problem and, like Paresh Indulkar said, you need to fix issues with the PTM_RATIO.

if i add a sprite to the position of the body, it is shown. however, anything else is not shown. :S

Ville S wrote:

Cookiebit Team wrote:
>
> Hello Ville S.
>
> Try to change to:
>
> […]
>
> :slight_smile:
>
Thanks!
>
It works now. :slight_smile:

bump. anyone know why my draw not working.

@hans hans

The problem seems to be related to the using of the level class. We can not find out your problem until you show and explain more code. We cannot see what you see on your end and therefore have no insight into your problem.

Thanks!

Jeffrey Walraven wrote:

@hans hans
>
The problem seems to be related to the using of the level class. We can not find out your problem until you show and explain more code. We cannot see what you see on your end and therefore have no insight into your problem.
>
Thanks!

hmm, well, i have now jumped over to using mr. nguyens debugdraw : http://www.cocos2d-x.org/boards/6/topics/23043

however, the objects are not moving on screen even though i have verified that they are moving in the physics engine. in the level::createPhysicalWorld, all i am doing is adding the code addChild(B2DebugDrawLayer::create(world, 32.0f));. I will post this there too to see if someone can help me there…

Couldn’t get it to work, so I used a contribution from vinova. Some modifications had to be made to get it to compile in xcode.
Here is the fixed forked repo:

Hope this helps someone.

How to use:
After including the main header, add the layer:
addChild(B2DebugDrawLayer::create(mB2World, PTM_RATIO), 9999);
Replace mB2World with your world

@Treon I use cocos2d-x 3.0 beta 2 and I have compilation error for your original code written in the beginnig of this thread. There are 2 errors. I have changed the call of setUniformForModelViewProjectionMatrix with setUniformsForBuiltins and got rid of the first error. But there is still one - function isetUniformLocationWith4f. What is that? Even Google does not know what is that :slight_smile:

EDIT:
Here I have found the port of your code for cocos2d-x 3.0: http://blog.csdn.net/tian2kong/article/details/20386213.

But I don’t understand why this does not draw anything. I have also changed the DRAWING code:

ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
kmGLPushMatrix();
mWorld->DrawDebugData();
kmGLPopMatrix();

to the following: (I don’t know if this is correct)

GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION);
kmGLPushMatrix();
mWorld->DrawDebugData();
kmGLPopMatrix();

And I call this in the update method of my main Layer where I draw my sprites. Now I don’t know why it does not draw anything… Please help

I have the same problem and I dont Know what to do.

Can you tell me where exacly I have to add the statement
addChild(B2DebugDrawLayer(world,PTM_RATIO), 9999);
I get an error
Error    1    error C2664: ‘void cocos2d::Node::addChild(cocos2d::Node *,int)’ : cannot convert parameter 1 from ‘B2DebugDrawLayer’ to ‘cocos2d::Node *’    d:\programs\cocos2d-x-3.1\tools\projects\testbox2d\classes\helloworldscene.cpp    62    1    TestBox2D

@hpikas here is my solution. You just add child the B2DebugDrawLayer and that’s it. See the attached files. I have lost lots of time on this… I don’t want you to lose it too :smile:.Box2dDebugDrawForCocos2d-x-3.1.zip (2.8 KB)