Multithreading Wisdom

Never try to multi-thread box2D, lest you land in the world of pain that I have just dug myself out of at... 3 AM in the morning. May I personally suggest that something of that manner be mentioned in the manual to save other developers from this hellhole? 😛

I tried to multi-thread the physics engine in my game under the misconception that it would allow for a much more accurate simulation by using another CPU core. Unfortunately this just causes extreme jerkiness problems, due to a misalignment in how the physics engine is syncing up to the rendering. Not only does this mean that different graphics objects will be updated based on different physics solutions (creating a mosaic of different physics times in the final render), but it also revealed a subtle problem in the way testbed recommends programmers to update the simulation.

After figuring out that multithreading wasn't going to work, I switched back to single threading, only to discover that the jerking issues were still present. I had been using the following code with 10 iterations and a 1/60 sec stepsize:
Code:
    while (_acc>=_dt && !_quit)
    {
      m_world->Step((float)_dt, _iters);
      _acc -= _dt;
    }

As far as I know, this is what you are supposed to do. Unfortunately, the following code is what appears to actually work:
Code:
  m_world->Step((float)deltasec, _iters);

Where deltasec is the time between the last frame rendered and the current frame. It completely eliminates all jerking.

As far as I can tell, the reason for this is that in order for a physics simulation to be smooth from a game's point of view, the physics must be updated in tandem with the graphics, otherwise an error margin builds up. I've looked through the documentation multiple times along with the comments in the testbed code itself (and searched the forums), but I cannot find anything related to this at all. This immediately brings up the question of, why hasn't anyone else had this problem? It's not much help if I came up with a solution to a problem that shouldn't exist. Note that I am rendering my game using directX, not openGL, although this really shouldn't make any difference. Also, its definitely not a scale issue (my scale is 180 pixels per meter, thus the object I was testing with was about 1 meter wide and 0.5 meters tall).

Does anyone have any idea why this works and why doing it the way your supposed to doesn't? If this is a real, reproducible issue, I would recommend investigating it further and possibly updating the docs.
Found solution to the bizzare single-threaded jerkiness.
Code:
  _acc += deltasec;
  while (_acc>=_dt*2.0 && !_quit)
  {
    m_world->Step((float)_dt, _iters);
    _acc -= _dt;
  }
  m_world->Step((float)_acc, _iters); //This syncs up the physics engine to the current frame
  _acc = 0;

where deltasec is the time between the last frame and current frame. This code and therefore be simplified to:
Code:
  double max = _dt*2.0;
  while (deltasec>=max && !_quit)
  {
    m_world->Step((float)_dt, _iters);
    deltasec -= _dt;
  }
  m_world->Step((float)deltasec, _iters); //This syncs up the physics engine to the current frame


Thus, multithreading may still be possible, but only if this compensating factor is used before a frame is rendered, and a subsequent freeze-frame of the entire physics engine state is taken.

EDIT: Updated code
Yeah, that's the standard way to fix your timestep. Some go further and interpolate between frames if it's needed.

The major problem with doing asynchronous updates is actually with input: your game won't feel right unless input, and differences made by user input, match up with rendering.
Hangar wrote:
Yeah, that's the standard way to fix your timestep. Some go further and interpolate between frames if it's needed.

The major problem with doing asynchronous updates is actually with input: your game won't feel right unless input, and differences made by user input, match up with rendering.


So why is this standard method not mentioned anywhere that I've found?
blackhole wrote:
So why is this standard method not mentioned anywhere that I've found?


Instead of complaining, be happy that you may have found a shortcoming in the documentation (if it really is an issue) and make a contribution to the wiki. This is free, open source software after all.
I wasn't complaining, I was asking a simple question. Is there a reason that this hasn't been documented anywhere? I don't want to go on to the wiki and add something that might not be there for a reason.

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