Is Glenn Fiedler's "fix your timestep" feasible?

Following the well know article Fix your timestep!, and trying to reproduce the same logic used in Bullet, method btDiscreteDynamicsWorld::stepSimulation(), I think this is not possible to implement in Box2D as it resets all the applied forces ended the call to b2World::Step () (more precisely here), making following calls wrong.

Is it right? Someone have found a way to implement it?


[Edit: fixed link to Box2D source code, r25]
[Edit: fixed link to Bullet source code, r1831]
[Edit: changed the thread subject to be more precise].
I'm using a fixed timestep and it works fine. I reset the force every frame when updating all my game objects. It may not work if you're using the force value for other things than letting Box2D know what force should be applied on the object this frame.
I'm using a fixed timestep and it works fine.


If your read the posted links you will see that the problem is not about calling Step() one time with a fixed timestep value, but calling it multiple times, with the same timestep, in the same frame. E.g.:

Code: Select all

void updatePhysics (b2World * w)
{
    w->Step (1.f/120.f, 10, 10);
    // HERE THE FORCES ARE ALL RESETED
    w->Step (1.f/120.f, 10, 10);
}


As you can see, after the first call to Step(), the forces are already reseted by Box2D. And this is the problem, as the second call to Step() don't works as needed.
Ah you want to do multiple physic step per game tick. That's different than a fixed time step. In a fixed time step game loop, the logic updates are decoupled from graphic updates. In a logic update, you update every game object and then run a physic step.

Take a top down space shooter. Your ship can thrust forward. If your ship is turning and thrusting you will want to reapply the new force vector inbetween these 2 steps because it turned in the first step and the force vector also turned. Reusing the force vector would not work here because it's not pointing forward.

If you want to do multiple step per game tick, you may want to reset the force vectors anyway because your objects may have a different orientation, which would create different force vectors.
Ah you want to do multiple physic step per game tick. That's different than a fixed time step.

It's not different, is only more advanced: if you call Step() one time per frame with fixed timestep, your game will change its speed in function of the current frame-rate. E.g. if, in a single frame, your timestep is 60 Hz (16ms) but your game is going to only 30 FPS (33ms), you will loose 1 whole simulation step; your game will advance slowly. In this case you want to execute 2 physics simulation steps, each one with a fixed timestep to keep the simulation stable and synchronized with real clock. Of course only if the game is not physics-bounded.

If you want to do multiple step per game tick, you may want to reset the force vectors anyway because your objects may have a different orientation, which would create different force vectors.

It's only a matter of precision. In your case you could apply an impulse instead of a force.

The problem, using the above example (60 Hz vs 30 FSP), is that the physics system must simulate 33ms instead of the "usual" 16ms; resetting forces in between changes the simulated environment. E.g. all constant forces are lost. And what if I have to simulate more than 2 frames? Or if a want physics to run with more precision, stepping at 120Hz?

Bullet implement this logic in the correct way (forces persist between sub-steps). Box2D, unfortunately, seems to miss some "hook" to be able to implement this scheme. Or not?
You could always comment out the lines where it resets the forces, but I don't know if it will break something else.

I think you're mixing the physic step with the game step. The game loop is generally :

- Process input
- Do X number of game tick to catch up
- Render with interpolation

In the game tick, you generally have:
- Update Game Objects
- Step the physics
- Update with physics results

The physic time is usually in sync with wall clock because it's convenient. However, if you have some form of bullet time, the physics update length and the game tick length will be different. Instead of stepping 1/60 sec, you will step the physics 1/600 sec for every game tick. If you render at 30FPS, you end up doing the game tick twice per frame, advancing the game 1/30 sec and the physics 1/300 sec. That doesn't simply step the physics twice, it does the whole game object update and post-physics update twice, setting the forces along the way for the second step based on what happened in the first step. If some rocket exploded in the first step, you'll want your objects to get blown away, which requires reupdating your game objects between physics steps.
The pointed article, http://gafferongames.com/game-physics/f ... -timestep/ , explains very well what is the problem and what is the solution. While physics requires fixed time-step for numerical stability, a game usually runs with a variable (real) time-step; see the article.

If it's not possible to implement this sort of solution using the current supplied interfaces of Box2D, I think that a simple improvement (e.g. a flag to Step(), maybe with a default value to be backward-compatible?) could be done as this is an important issue.
Yeah I've read that article before and a bunch others on the subject. While it says physics need a fixed timestep and rendering can be done based on time delta, it says nothing about game logic. 😉

Anyways, there is no right or wrong way to implement the main game loop since it depends on what you want to achieve. I was just pointing out some issues with this method which would require game objects being updated every physic step, thus throwing the whole set of features out the window. Again, it depends on what you want to achieve.
Thanks Tiblanc, but I'm not interested in discussing how to deal with the game loop/game logic. I'm interested in the numerical stability of the physics system in a game with variable frame rate.

I would understand if this problem could be addressed, discussing possible solutions. Of course if Erin is interested to modify Box2D to support this sort of feature (maybe with some contributed patch).

The problem could be solved in two ways:
  1. modify b2Body to be able to read an set the current force/torque. This is the most simple solution and allows to solve the problem outside Box2D.
  2. update Box2D b2World::Step() with a flag to know if it have to reset applied forces. This allows to perform minor physics logic (as controllers) between subsequent calls to Step() (in the same game tick).

As side note, solution 2 requires that all internals of Box2D (as collision responses) deal with impulses and not with forces, otherwise they can accumulate. From what i can see from the sources, Box2D works already with impulses, so this should not be a problem.

Solved the previous problem, the interpolation phase can be implemented outside Box2D; or Box2D can be extended (as Bullet) storing, in b2Body, the previous state (position/rotation). In this case could be implemented a new Step() method that implements the whole logic, maybe with a callback to execute custom physics controllers/logic between the single simulated steps.

Solution 1 is the most simple to implement: a couple of simple accessor methods in b2Body.

Could be Erin interested in a patch for solution 1?
Please post an issue for this here: http://code.google.com/p/box2d/issues/list

It would be easy to add a flag to the step function the prevents forces from being reset.

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