Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:






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 :




   //Only render my corresponding layer debug data



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


share|improve this question
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
up vote 2 down vote accepted

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.

share|improve this answer
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.

share|improve this answer
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.… – user744 Feb 23 '11 at 13:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.