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

Done: Issue 37.
If you read Glenn's stuff, you may already understand why you can never perform replays that don't use snapshots, but if you don't fix your framerate, you won't even get reasonably good looking replays. You can couple or decouple the frequency of physics updates from your game framerate easily, but I tried using variable steps before in Box2D and two of my own physics engines and you will almost always just make debugging harder and make issues be able to occur that only happen during a frame spike (when the next physics step will have a large delta used and cause tunnelling and other artifacts).
I'm new here, but I;ve been writing physics code for quite a few years now, specificaly for physics games.

I've noticed a few people have complained about forces being reset to between multiple calls of the simulation step. I have a question for you; why is this an issue? It's not just the body simulation that has to be stable, with a fixed error, but also the code that runs on-top of it that plugs forces into the simulation.

A good example of this is code for calculating tire forces in a vehicle simulation. Or even suspension force calculation (without using constraints) or even just aero-dynamic drag!

The real solution for me, is not just to call the simulation multiple times, but also the game-play code. So instead of;

Code:
poll input

call game code and plug forces into sim

for num steps
    step simulation
end for

render


I perform the following, which I might add, manages to be deterministic without the need for snapshots.

Code:

for number of steps needed for this frame
    poll input
    call game code and plug forces into sim
    step simulation
end for

perform interpolation for rendering (currrent and next physics state)

render



While not as good as a multi-threaded approach to managing game-state and rendering, it does a very good job of keeping a game playable when frame rates are extremely varaible or very low. In fact, because the physics is fixed at a given step, changes in frame rate are hardly noticable for most people.
rusty wrote:
I've noticed a few people have complained about forces being reset to between multiple calls of the simulation step. I have a question for you; why is this an issue? It's not just the body simulation that has to be stable, with a fixed error, but also the code that runs on-top of it that plugs forces into the simulation.

A good example of this is code for calculating tire forces in a vehicle simulation. Or even suspension force calculation (without using constraints) or even just aero-dynamic drag!

The real solution for me, is not just to call the simulation multiple times, but also the game-play code.


Calling all game-play code is wrong, as you will perform a lot unneeded/unwanted calculations (e.g. AI tasks, graphics related jobs, and so on).

You have to think to each system as a discrete box that elaborate a single "tick". This is true for AI as for physics system. The "world" advance of a "tick" (that, with variable frame-rate, could be 10ms, 16ms, 30ms, ... you can't know). With ideal math, you could call Step() with the needed time-step. But we are far from ideal math, and for numerical stability you have to call Step() with a constant value (e.g. 16ms). But we have to advance of a whole "tick", e.g. 30ms, while physics had advanced of only 16ms. Remains 24ms that the physics system have to simulate to process its "tick", of course calling again Step().

The solution is two fold:
  • high level forces are applied, these must remain active for all calls of Step() belonging to the same logical tick (and these don't have to be reseted).
  • low-level forces (more fine grained, that are important for numerical stability - as suspension forces) must be adjusted to each single Step().

(the answer) High level forces are those driven by other systems, as AI. Those systems have processed its "tick", and asks to the physics system to apply a pre-computed force for the elapsed "tick". It's an implementation detail of the physics system if this happen with a single call to Step() or with multiple calls. AI don't have to know this. This is why high level forces have not to be reseted between multiple calls to Step() in the same "tick".

Low level forces are all "physics-dependent" forces, as suspension or buoyancy forces. Forces not driven by other systems, but strictly part of the physics nature of the "world". Those forces have to be adjusted at each call to Step(). My solution is to have a sort of controller objects attached to bodies, that apply impulses as needed. Those controllers runs at each physics Step().

In this way you don't execute all your game logic (AI, graphics, etc.) at each call to Step(). As said, this is wrong: AI have already terminated its work, having already processed its "tick".


Daniele
Kafu wrote:
Calling all game-play code is wrong, as you will perform a lot unneeded/unwanted calculations (e.g. AI tasks, graphics related jobs, and so on).


Why is it wrong? A.I. when written correctly doesn't even run every game frame, aside from some entity updates. It tends to be run on a "round robin" scheme with basic spline following/entity update done every step, which is pretty small computational wise. Updating state and path finding is a separate issue from entity update.

But the entity updates from the A.I. tend to be required every physics/world frame to avoid weird collision detection/constraint issues.

Quote:
My solution is to have a sort of controller objects attached to bodies, that apply impulses as needed. Those controllers runs at each physics Step().


That's pretty much what I've said! That's no different from the pseudocode that I had written, bar the detail of saying "oh, by the way, you don't HAVE to do everything but there are some tasks like input processing for A.I. that will ensure the required forces are being applied".

Quote:
In this way you don't execute all your game logic (AI, graphics, etc.) at each call to Step(). As said, this is wrong: AI have already terminated its work, having already processed its "tick".


If you had read my post, you would have seen that the graphics was not updated at the same rate as the physics/world update. It's not a "wrong" solution, it's a very good solution that I have used in production games.

My issue it this; everybody here is trying to do something with physics. My approach to this, on published games, is to make the physics the absolute deciding factor on everything else. You don't have to run the A.I. every game frame, but the basic housekeeping of entities and game logic (if you choose) tends to run at that rate, or some multiple of the physics rate.

This is also very handy for replays and variable world times (slo-mo) in that, you're rendering system can either just use the "next" world state or interpolate between the current and next (since the physics is always at or after the current render time when rendering occurs).

The approach that I'm seeing from almost all of the posters on this thread, is to just run the gameplay code at some variable rate, and then treat the physics as some separate system to be forgotten about, which for me, is just not the way to go about it.
I've only explained why resetting forces is not always desired. And I've tried to explain it following a reasoning.

If you will work with multi-threaded game engines, you will understand why each system have to be considered an independent "black box".

Your solution - of course - works. But if you think that it's the solution, then I don't understand why you have asked the question.

But I don't want to start a flame.
Quote:
If you will work with multi-threaded game engines, you will understand why each system have to be considered an independent "black box".


There have to be clearly defined interfaces, certainly. I know this because I do work with multi-threaded engines, but you still have to pick one point at which everything is synchronized. You still have to perform basic house-keeping for everything at one point/at every update.

Quote:
Your solution - of course - works. But if you think that it's the solution, then I don't understand why you have asked the question.


My solution is no different to your long, windy and condescending reply. I asked the question because I feel treating the physics as some "add-on" that needs to be advanced separate to the game world, especially when your game is largely physics based, is the wrong way to go about it.

Everything that you've said, is NO different to the solution that I not only put forward, but have used in seven released, physics based games. You'll just have to humour me, as I'm asking this from a position of experience because I'm puzzled by the approach that other people are taking.
I'm new here too, and i implemented a car physics model from scratch in the past, for a game running on DesktopPC/PocketPC: i don't know if kafu or pTymN are basically saying the same thing with different words, but i can absolutely confirm rusty is right on its prototype code because i wanted pixel-perfect ghost-cars (your own previous races) opponents on my game and this way of doing things is exactly what i came up with in order to have a perfectly deterministic physics simulation.

I've a pair of questions for you, rusty, if you still read these forums:
1. you said you didn't need snapshots, could you elaborate on this? I mean, i needed to record input events in order to reproduce the replay at a later time, all i was doing was feeding the input snapshots to the engine: what did you mean exactly?
2. did you ever needed interpolation on the state of your objects in order to avoid temporal aliasing? I tried to provoke it by intentionally injecting some "jitter" (usleep) on the loop dispatcher but i still can't notice anything wrong with the on-screen results. Can you provide me/us with some sort of method to provoke this temporal aliasing problem?
I was also thinking of scaling the frametime to do slow-motion, i need to test that: you should basically do sub-frame linear interpolation?
This might be a bit offtopic given the stuff that's been discussed in the thread already, but I've been meaning to ask some wise and knowledgeable people about this for a while...

Is there any sensible course of action that you can take if your game's framerate is bound by the physics? That is, if your physics delta is set to 1/60th of a second, but the physics calculations take longer than that, what can you do? Seems to me that you can't run the physics step twice in a frame to catch up in these cases, since that would slow the framerate even more, until the game grinds to a halt. Switching down to 1/30 and halving the number of physics updates might lighten the load, but the whole point of the excercise is to provide a constant delta to the physics, so it doesn't seem like a good idea. You could have the physics always run at 30Hz rather than 60, meaning you wouldn't have to run it every frame, but do you then run into problems with fidelity (i.e things being more likely to tunnel through geometry, collision responses being more unstable, etc)?
I just implemented a simple testbed in order to test some things: slow-motion, temporal aliasing, as well as undersampling and oversampling on both the display framerate and the physics timestep.
Indeed, a fixed timestep with subframe interpolation is a wonderful method for achieving ultra-smooth animations, especially in undersampling and oversampling cases, where the display framerate can't keep up with the physics one and the other way around: in fact, i was able to take down the physics fixed timestep from 1/60 to 1/20 without any visible jitter or stuttering, the animation was really smooth all the time.
The price you pay is obviously keeping both a previous and a current physical state at each physics substep, but the visual results are definitely worth it.

@LemonScented, i'll give you my 2 cents here, based on what i've experimented with.
You basically have to cope with out-of-sync display and physics framerates, and that's what subframe interpolation is all about: you could try to implement some sort of auto-adjustment on the physics timestep: you can continuously monitor how much time is spent performing the physics timestep and whenever it takes longer than a predefined threshold, you start to modulate it linearly toward your min. limit (say 1/20) until the timestep is again between that threshold; you'll start to modulate it linearly toward you max. limit (say 1/60) whenever the load is getting lighter than another predefined threshold (you'll want to have a "safe-zone" in which you never modulate).
Keep in mind that all of this won't give nice smooth results until you also implement subframe interpolation, in fact that's the point of modulating the physics timestep toward machine-bound acceptable limits: whenever it occurs you can be sure out-of-sync situations will be smoothly rendered, but this approach could very well cause physics instabilities, so you'll need to evaluate carefully if this approach can work for you, it all boils down on what you are doing.

Timestep modulation was working well for me with this (src) and a couple other things, but sure it may couldn't fit your needs.

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