[SDL] Delta time?

David Olofson david at olofson.net
Fri Oct 12 17:01:59 PDT 2007


On Saturday 13 October 2007, L-28C wrote:
[...]
> In doLogic(), what I had done while capping FPS was add 0.05f to the 
> ball's speed (acceleration) and multiply by 0.99f (deceleration).
> 
> But how would I modify those values to work with my delta?

It's easy (well, almost) as long as things are moving at constant 
speeds (just scale the velocities), but acceleration and jerk (or 
jolt, or "change in acceleration") require more maths for correct 
results. For speed and acceleration, you need this to update your 
position each frame:
	position += velocity * delta +
			acceleration * delta*delta / 2;

That's the easy part! The real problem is events, such as collisions 
and various game logic events that affect object motion. Collisions 
and time based events can nominally occur at any time - usually 
somewhere in between the points in logic time where you perform the 
updates. At very low frame rates, you may even fail to detect 
collisions, and responses may be totally off, unless you deal with 
this somehow.

Indeed, in most cases you'll be fine with good approximations 
(Runge-Kutta, for example), but if you want it deterministic to the 
point where you can record a demo, control/decision input only, and 
play it back correctly at any other frame rate, you may be in for 
some serious math exercises.

So, what do you do? For most applications, whether you want to keep 
your game logic nice and simple, or you want totally deterministic 
behavior, or both, I believe the most sensible solution is to use a 
fixed "virtual" logic frame rate along with interpolation, sometimes 
referred to as "tweening".

I've posted on this before (search the archive for "fixed logic frame 
rate" and stuff like that), Kobo Deluxe uses it (to keep the original 
XKobo logic running at the fixed rate it was designed for), and Fixed 
Rate Pig over at olofson.net is a small but playable game that is 
perhaps somewhat easier to read and understand.

Someone posted a link to a very nice paper on this, but I can't seem 
to find it now...


//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