Delta time is the elapsed time since the last update – for example, the previous frame for a video game. The elapsed time can vary depending on the speed of the system (in our case computer, console, mobile device, etc.) or the work required to be processed by the code at the moment. If you make your calculations by assuming the elapsed time is constant (such as moving your character by a fix amount -5 unit per frame ), the overall effect of this situation would not be smooth. Delta timing gives you the changes per seconds (or milliseconds depending on your preference) rather than per frame. So, it is used to update the program smoothly regardless of how long it took to perform the last process.
Delta timing is very useful for video games. You can use it for smoothing out your movements and other incremental calculations. It also introduces frame independency for video games. Thanks to delta timing we can avoid slowing down or speeding up in the game depending on the complexity of the current process, we can separate rending process to another threat, and so on.
Delta time is calculated by subtracting the last saved time (the last frame/update time) from the current time:
1 2 |
deltaTime = now - previousTime previousTime = now // So, we can be ready for the next calculation |
Frame rate independency is important especially for mobile game development. Nowadays, PCs can get fixed 60 fps; however, when it comes to mobile devices, it can easily vary between 30 and 60 fps since there are too many devices (players) with different specifications.
Let’s say we have a game character moving with a speed 5.0 (1D world). If you do not use delta time and move the character as character. pos += character.speed then you get different behaviours in different frame rates since the character will move 5 pixels per frame. For example, the character will seem moving faster in 60 fps than 30 fps since it will cover more pixels in a second.
However, if you use delta time and move the character based on speed * deltaTime, then the character will always take the same amount of distance regardless of the frame rate and it will move smoothly.
1 2 3 4 5 6 7 8 9 10 11 12 |
// A move function of a game character // Assumes that the character always move in X and Y axes, // in the same directions with an amount given by SPEED member // mPosX is initially 0.f // mPosY is initially 0.f // mSpeed is assumed 5.f // pDeltaTime is the delta time void move(const float pDeltaTime) { mPosX += mSpeed * pDeltaTime; mPosY += mSpeed * pDeltaTime } |
Another usage of delta time can be counting the time. Let’s say we are writing an AI behaviour and AI should wait 3 seconds before it moves. Again, if we don’t do this based on delta time, this “3 seconds” will be different in 30 and 60 fps. However, by using delta time, we can literally have 3 seconds.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// A function that waits as LIMIT (mLimit) before calling // the move function mentioned above // mTime is initially 0.f // mLimit is assumed 3.f // pDeltaTime is the delta time bool moveBehaviour(const float pDeltaTime) { mTime += pDeltaTime; if( mTime >= mLimit) { mTime = 0.f; move(pDeltaTime); return true; } return false; } |
However, when it comes to the game physics, things may work differently and you may want to have a special type of delta time or no delta time at all. But, this article does not cover the physics part.
Another cool thing you can do based on delta timing is time scaling. By using time scaling you can have an easy and smooth slow-motion effect. If you scale your delta time by 1, it gives you the regular speed of the game. By scaling your delta time by 0, you can pause the game easily. By scaling it by 0.5, you set your game to half speed, by 2.0, you make your game twice faster and still smooth. You can consider to object-specific time scales for having better control on your game.
Clamping the frame rate can be a good idea for having a stable system even at very low or very high frame rates since these can also give you unsmooth results. Thus, you can consider to clamp your delta time between 0.033 (30 FPS -1/30) and 0.0166 (60 FPS -1/60).
You can check how to calculate delta time in C++ in this post. And please refers to the links below if you need more details:
You can also check the links below if you feel you need more info: