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:
As far as I know, this is what you are supposed to do. Unfortunately, the following code is what appears to actually work:
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.
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;
}
{
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.