[SDL] Delta time?

David Olofson david at olofson.net
Sat Oct 13 17:27:32 PDT 2007


On Sunday 14 October 2007, Mike Powell wrote:
> I think David is completely correct. However, I want to emphasis one
> point: 
> 
> "it's easier and safer to just run the game logic at a fixed frame
> rate." 
> 
> Notice the "game logic" point. You don't have to RENDER at a fixed
> frame rate, so long as the game logic is at a fixed rate.

Good point! This may not be all that obvious - and there are indeed 
some games that are simply locked at a fixed logic *and* rendering 
frame rate.


> The way I typically do it is with something like:
> 
> for (int i = 0; i < deltaTime; i++) {
>     updateGameLogic();
> }
> 
> Thus, the game logic gets called once per millisecond (assuming
> deltaTime is in milliseconds), regardless of what frame rate you're
> rendering at. Of course, you can adjust this easily to run once
> every 3 milliseconds, or 10 milliseconds, or whatever value you
> like, to speed things up, though as the increments get larger the
> results will get less precise.

This loss of precision (and the resulting unsmooth movement) can be 
fixed by means of interpolation.

What I do in Kobo Deluxe and Fixed Rate Pig is basically what you do 
above, but I also keep the resulting object (and map, in the case of 
Kobo) coordinates from previous logic frame.

When rendering, I interpolate as needed to "morph" the scene into a 
state corresponding to the logic time of the frame to render. (Well, 
minus one logic frame, actually, as future game logic states are not 
available, obviously. Use a higher logic frame rate to reduce 
latency, or perhaps try extrapolation/prediction.)


//David Olofson - Programmer, Composer, Open Source Advocate

.-------  http://olofson.net - Games, SDL examples  -------.
|        http://zeespace.net - 2.5D rendering engine       |
|       http://audiality.org - Music/audio engine          |
|     http://eel.olofson.net - Real time scripting         |
'--  http://www.reologica.se - Rheology instrumentation  --'


More information about the SDL mailing list