Using box2d DrawDebugData with multi layer scene?

In my Game, a Scene is composed by several layers. Each layer has different camera transformations.

This way I can have a layer at z=3 (GUI), z=2 (Monsters), z=1 (scrolling background), and this 3 layers compose my whole Scene.

My render loop looks something like:

renderLayer()

applyTransformations()

renderVisibleEntities()

renderChildLayers()

end


If I call DrawDebugData() in the render loop, the whole b2world debug data will be rendered once for each layer in my scene, this generates a mess, because the "debug boxes" get duplicated, some of them get the camera transformations applied and some of them don't, etc.

What I would like to do, would be to make DrawDebugData to draw only certain debug boxes. In that way, I could call something like

b2world->DrawDebugDataForLayer(int layer_id)


and call that on each layer like :

renderLayer()

applyTransformations()

renderVisibleEntities()

//Only render my corresponding layer debug data
b2world->DrawDebugDataForLayer(layer_id)

renderChildLayers()

end


Is there a way to subclass b2World so I could add this functionality ( specific to my game ) ?

If not, what would be the best way to achieve this (Cocos2d uses a similar scene graph approach and box2d, but I'm not sure if debugDraw works in Cocos2d... )

Thanks

• AFAIK cocos2d simply sets the GL states appropriately before calling DrawDebugData and restores them afterwards. – bummzack Feb 22 '11 at 9:10
• Well, if you just call DrawDebugData, you will get several polygons on the screen (rendered at once) , it's not possible to apply different transformations to those polygons independently AFAIK. – Mr.Gando Feb 22 '11 at 20:10

Fundamentally, the right approach is to only call DrawDebugData exactly once, at the start or end of your render loop usually. Everything is coming from the same b2World - which is the same coordinate system - which should mean, the same view/projection matrix.

The point is to dump everything about the world onto the screen in as simple a way as possible - the more game logic you add to the debug data, the less useful it becomes for debugging. If your architecture makes this tricky, you should step back and look at why you're switching between projections so much in the first place.

• Hey Joe, Thanks for the answer, I'm using a Camera system using gluLookAt. If a Game Contains a GUI (which is not affected by the camera), a layer for drawing SFX and a layer for Game Entities that will fight between each other (both affected by the same camera transformations )... you would need to call DrawDebugData once per layer... Why would this be bad design ? ( gluLookAt affects GL_MODELVIEW, not the projection Matrix ) – Mr.Gando Feb 23 '11 at 1:05
• That's only the case if you're interleaving UI drawing and world drawing. If for some reason you absolutely must interleave UI and world drawing, then you can just switch back the world view/projection matrix at the end before DebugDrawing. The point is, as long as there's only one camera (= view/projection matrix), you can draw all your debug data exactly once, with that camera. – user744 Feb 23 '11 at 13:15

You are over thinking it. When will one b2World have bodies it two layers? I don't think you'll find one use case that isn't contrived. Box2D has one coordinate system. At any time world will is one way that world is represented to the player. That mean there is one projection matrix and one one layer.

What maybe misleading you is the desire to use Box2D in more than one layer. For instance you could use Box2D to do some crazy GUI effects. In this case you'll want to use two b2Worlds. You could do it with one and some clever collision masking but it's not worth the effort.

I like your approach of using layers in a scene (I use the same in my games). In my game I have a layer dedicated to DrawDebugData. I make sure the debug layer is higher than the game layer and uses the same camera view.

• Overall I agree with you, but one reason to avoid multiple worlds is that it is not possible to move non-trivial objects between worlds. gamedev.stackexchange.com/questions/3807/… – user744 Feb 23 '11 at 13:23