iPhone debugdraw cocos2d

Hi,
I've posted this over on cocos2d as well, but most folks over there use Chipmunk2d so I reckoned that this forum may be more appropriate.

I'm making an attempt to use Box2d with cocos2d, and I've got
everything working except the debug drawing. I can add bodies to my
world in a CocosNode, and simulate them (printing out their position
so i know they're moving). However, for some reason I can't get them
to show up.

I took the GLES-Render.mm from the Box2d project and added it to mine
and set the worlds debug draw object
Code:
   
debugDraw = new GLESDebugDraw();
world->SetDebugDraw(debugDraw);
debugDraw->SetFlags(debugDraw->e_shapeBit | debugDraw->e_aabbBit);
//debugDraw->SetFlags(0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 |
0x0020 | 0x0040 | 0x0080);


And when I step through world->Step(), I follow it all the way
to through and it appears to be calling all the right functions -
finds the bodies and shapes, then calls DrawPolygon, or DrawCircle
etc. But alas, no debug drawing shows up. In my game loop method, If I
update a sprite's position to a box2d body's position I can see it moving in
the simulation and colliding with (invisible) shapes that I've placed in the world, it is just the debug drawing that is non-visible.

I have a feeling I'm just missing an opengl call perhaps, but I really
have no idea. I've noticed a few differences between the way cocos2d sets up the projection matrix and viewport, and I've tried matching those calls (glOrthof, etc) to what is in Box2d's iphone demo. But not having any luck yet.

Any help is greatly appreciated. 🙂

Thanks,
Chris
Cocos supports separate sprite like things, yes? You might be better off drawing into a sprite once for each body, and then moving those, rather than using b2DebugDraw, which is imho more for cruder systems.
Have a look at how the chipmunk implementation does it.
Thanks Boris.
I compared some of the code in cocos2d lib to the debug draw class, and noticed that glEnableClientState was not in the debug draw class. So that was an easy fix. And I probably will use sprites ultimately, but I'd still like to be able to render the physical shapes so I can see what is going on better.

I stumbled into another problem. I've set the device in landscape mode on app launch, which calls
Code:
glTranslatef(160,240,0);
glRotatef(-90,0,0,1);
glTranslatef(-240,-160,0);


but the physics world is transposed from what I'm intending it to be, like it thinks it is still in portrait mode. Any sprites in the scene obey the rotation, go up and down with gravity, but the corresponding bodies are moving left and right.
I guess this is more of an opengl question, but any help is appreciated.
the landscape mode is just visual. they rotate the entire coordinate system by 90 degrees (swapping the viewport's x with y axis). so by translating a sprite in landscape up by one unit you're "really" moving it 1 unit to the right on the native coordinate system.

box2d does not know this. it has its own internal coordinate system which is independent from the opengl coordinate system. and this is good so - how you map the box2d coordinates onto your screen should not matter to the physics simulation.


one tipp for cocos2d: if you're using their camera-class in landscape mode watch out: some of the camera's transformation are done in the native unrotated iphone coordinate system. so it can get really confusing and annoying. (i ditched that camera and implemented my own. and for my actual project i kicked cocos2d ... it's nice for prototyping but it's a messy framework.)

and watch out: the accelerometer's axes aren't rotated too! so in landscape mode you have to read the accel's x-axis to get your landscape y-coordinates 🙂

Original thread: https://box2d.org/forum/viewtopic.php?t=2904