while (running)
{
static std::uint32_t simulation_time{0U};
std::uint32_t const real_time = SDL_GetTicks();
process_input();
while (simulation_time < real_time)
{
simulation_time += 16U;
update(16U);
}
render();
}
As I understand it, the inner while loop's is essentially playing "catch up" with the outer while.
For simplicity's sake, let's assume that simulation_time is incremented to 1 in the inner while (simulation_time += 1U).
Further, let us assume that simulation_time is 42 in the first iteration. Now, the inner while needs to execute 42 times until it fails the loop condition (42 < 42).On the second iteration, simulation_time is equal to 58 (42 + 16). The inner while executes (42 < 58) 16 times now.
On the third iteration, simulation_time is equal to 59 (58 + 1). The inner while executes (58 < 59) 1 time.
The real_time cannot stay the same, as it polls the number of ticks since SDL was initialized. So apparently, simulation_time is always smaller than real_time.
(If for some reason, real_time isn't incremented in an iteration, then the inner loop will not get executed. However, I don't see a case where it can happen.)
Now, inside the update function there is a position update akin to current_position += speed * 16U. Now with the above iterations:
- The first iteration, causes 42 update calls (so current_position is updated 42 times as well).
- The second iteration, causes 16 update calls.
- The third, calls update 1 time.
So we are advancing the position of something variable times. (We are also executing the inner while in variable amounts.)
Maybe I am missing something here, but why doesn't the non-constant number of updates cause jitter? I really don't understand why it works at all to be honest. I am trying to understand it fully.
You do get jitter from a changing number of sub-steps per variable step which is why you want to do things like the interpolation in Glenn’s solution. This is also why you still want to do everything in your power as a game developer to reduce the variance in real step size.
Can't the inner while take 1 ms or 1 ns for an iteration? I don't see how the inner while's execution time is at roughly 16 ms. Okay, if my number of sub-steps is wrong, then I am really missing something here. Just trying to understand what exactly is wrong with my thinking.
It is supposed to be so simple, yet I have real trouble understanding it currently. I am basically stuck now.
Real time is updated once at the start of the step then simulated time catches up in 16ms increments. So you get as many update calls as there are 16ms increments to simulated time before it exceeds real time.
This is all very simple but also pretty finicky to think through. I’d replicate the code in the language of your choice and step through it.
Well the main reason is that for either variable time slices or fixed time slices with interpolation, what the time for the simulation/interpolation you want is that for the current frame, but you don't have that until you render it. So what you do is assume that it will take about as long as the previous frame, which is a bad prediction if the frame time variance is too big. If you really want to eliminate the jitter you'd need to simulate/interpolate for a time that you are guaranteed to be able to render the frame in and then make sure that the frame is displayed when that time elapses and not before. This of course increases latency so there is always a tradeoff.
Another cause for jitter is that even measuring the time between two successive frames can be difficult with modern GPUs - the CPU time at the start of the game loop doesn't really cut it: https://medium.com/@alen.ladavac/the-elusive-frame-timing-16...