This is how I do it in iOS games:
// get's called approx every 60th of a second but not always
- (void)drawViewLoop
{
timeThisRound = CFAbsoluteTimeGetCurrent();
deltaTimeThisRound = timeThisRound-lastTime;
// don't let the delta get too big. Even if the fps slows way down cap delta at 1/20th of a second
if (deltaTimeThisRound > 0.05) {
deltaTimeThisRound = 0.05;
}
[self drawScene];
// present screen
[self updateScene];
lastTime = timeThisRound;
}
I usually don't let my delta get huge. Let's say some system thing happened, they put the app in the background, or whatever. If it's larger than .05 then I let things start dragging because to me that's better than having time jump forward 3 or 4 seconds and your ship blows up. Also a deltaTimeThisRound
of say 3.0 would mess up all the physics math.
Some of my animations are frame based. So no matter how fast things are progressing it will show all N number of frames of that animation. For those each time I hit drawViewLoop I increment the frame of the sprite that you see.
Some of my animations are time and percentage based. Where p % through the move is calculated by taking timeStart
of the move, duration
of the move, and timeThisRound - timeStart
to get a time from the start of the move. So p = timeThrough / duration
with some clamping and completion handling on 0 and 1.0.
Some of my animations are physics based where position += velocity * deltaTimeThisRound
. These are the ones that are effected by clamping my delta at .05.