Question about DebugDraw

So, I'm unceremoniously jury-rigging the Box2D sample code together with my own engine. Just dropping Box2D into an engine seems to be pretty straightforward, but I want the GLUI controls and all the nice debug drawing stuff in there as well, which is proving to be a bit more awkward.

My engine does all of its updating and all if its rendering in 2 seperate functions, because this seems like a reasonable thing to do. The problem I'm having is that stepping the physics engine forward also invokes the DebugDraw functions to draw the stuff. Then when it comes to render, I clear the screen, draw my scene, and swap the buffers, which means that the DebugDraw stuff always ends up on the back buffer and never gets rendered. I've temporarily fixed it by moving the physics update into the render function, which is obviously quite a silly thing to do.

My question is how did you intend the DebugDraw stuff to be used? Am I supposed to use the DebugDraw to just calculate and store a list of vertices, which then actually get drawn later in my render function? I'm quite happy to just use the immediate mode rendering stuff in the testbed code if that's possible, but it doesn't seem to be doable with things set up as they currently are. Are there plans to seperate out the DebugDraw stuff in some way so I can step the physics with one function and render the results with another, or am I just doing it wrong?
I've got a similar problem but it deals with draw order rather than update/draw. There is no way, as far as I can tell, to accurately draw the Debug visualization over the game specific visualization of the objects. If you update before you draw the game objects will be rendered over the debug layer, or masked by the debug layer if you're using the Z buffer. While if you draw before you update then the game objects will be one step behind the simulation, 1/60th of a second - I won't tell if you don't. But what if I want to keep update and draw synced correctly?

My current solution is to call Step twice. Once without SetDebugDraw set and then once during the render phase with SetDebugDraw set but a zero timestep and zero iterations. It works, but the second step has some minor effect on the deterministic behaviour of the engine. ie. the results are different if you're drawing the debug overlay, therefore I ensure that step(0, 0) is always called. Just note however that this means that the result is always incorrect because the second step actually effects the simulation.

So the code looks something like this:
Code:
void Update()
{
   world->Step(timeStep, iterations);
}

void Draw()
{
   ...Draw objects...

   if(debugDraw)
   {
      world->SetDebugDraw(debugDraw);
      world->Step(0, 0);
      world->SetDebugDraw(NULL);
   }
   else
      world->Step(0, 0);
}


It's a nasty and possibly expensive hack that will do the trick for now, but I think that a neater solution should be considered.
If you are having timing issues, you should just buffer all the draw commands. DebugDraw is only for debugging the physics simulation. You should not ship a game using DebugDraw to render your scene.
For my project, I allocate a drawlist handle with glGenLists() at the beginning of the program, surround the simulation phase with a glNewList()/glEndList() pair to catch any debug render calls, and finally call glCallList() after normal rendering to produce the debug overlay. This is especially useful when separating simulation time from rendering time (for example, Gaffer's "Fix Your Timestep").
I was having a similar problem with DebugDraw. I originally tried the empty-step() method too, but box2d's DebugDraw was out of sync with my bounding-box drawing, which created some annoying line jitter on the edges of my boxes.

My horrible solution was to change DrawDebugData() in b2World to be public and then setting the DebugDraw immediately before calling it each frame:
Code:
// Step the world. (snip)

// Draw the world. (snip)

// Draw debug lines.
if (m_collisionDebug) {
    m_world->SetDebugDraw(m_collisionDebug);
    m_world->DrawDebugData();
    m_world->SetDebugDraw(0);
}


It would be better if box2d didn't automatically draw debug data and required the DrawDebugData() function to be called manually, but this solution works for now.
I would not do it like above. Why not use SetFlags(), similar as in testbed ?
Code:
//in Application setup 
m_world->SetDebugDraw(m_collisionDebug);
m_collisionDebug->SetFlags(0);
m_debugDrawFlags = b2DebugDraw::e_shapeBit|b2DebugDraw::_jointBit;

Code:
//in Application::Step()
if ( m_collisionDebug ) m_collisionDebug->SetFlags(m_debugDrawFlags);
m_world->Step();
if ( m_collisionDebug ) m_collisionDebug->SetFlags(0);

Maybe an additional question in
Code:
void b2World::DrawDebugData()
{
  ...   
  if ( m_debugDraw->GetFlags()==0) return;
...
}

would be more performant..., but i'm not sure about this
nimodo wrote:
I would not do it like above. Why not use SetFlags(), similar as in testbed ?


Unfortunately this would not work in my situation. In my program I have desynched the physics, so that physics is sometimes called more than once per frame, and other times is not called at all in a frame. I plan on eventually moving physics to another thread, in which case I will not be calling step() in the render thread at all.

I could cache the results of DrawDebugData(), but I think it is a lot of code for debugging collisions.
I have no problem moving the debug drawing to be a manual call. Would this solve your problems?
Erin Catto wrote:
I have no problem moving the debug drawing to be a manual call. Would this solve your problems?


Yes please! 😛
It would be much easier than buffering draw calls.
Erin Catto wrote:
I have no problem moving the debug drawing to be a manual call. Would this solve your problems?


Please, but to retain compatibility with existing code it may be better if Step() continued to call DrawDebugData() if it is set, but allow users to override the stored DebugDraw class by passing it as a parameter to DrawDebugData().

With this method there would be two ways to draw debug data: using the previous SetDebugData and Step method, or not calling SetDebugData and just calling DrawDebugData at the appropriate time.

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