Today’s guest tutorial comes to you courtesy of Christer Eckermann, the co-founder of Snuti, a small Norway-based indie game start-up. Snuti consists of two University graduates, Clare Falconer and Christer Eckermann, trying to mak...
Today’s guest tutorial comes to you courtesy of Christer Eckermann, the co-founder of Snuti, a small Norway-based indie game start-up. Snuti consists of two University graduates, Clare Falconer and Christer Eckermann, trying to make their independent game development dream come true. They will release their first commercial game this summer and are hoping to deliver a cute and unique experience. You can follow Snuti on Facebook and Twitter.
Making Games Run Smoothly With Delta Time
One method of animating, moving, and timing things in games is to programmatically change certain values on each frame update. However, if you update with a specific value every frame, for example, moving an object down one pixel every frame, then, with fluctuating frame rates, the speed of this action may seem inconsistent to the player. In that case, the player will experience both a choppy frame rate and a fluctuating game speed.
But if your game already runs smoothly, so why should you worry about it? Well, even if it does run smoothly, there are constant small changes in the frame rate. To compensate for this, you can use delta time. This helps ensure that your game runs more stable on older devices. More significantly, frame rate often drops noticeably when the device is displaying notifications or when your game is processing something heavy.
In my opinion, all games should use delta time, no matter the size and depth, if you want the game speed to appear consistent.
What is Delta Time?
The word delta means “change,” and thus delta time means “change in time.”
In games, delta time is a compensation value. In practical use, the delta time value is multiplied by values that are affected by time. When the frame rate is lower, affected values increase to compensate for the time gap, or decrease for fast frame rates. If your frame rate is consistently 60 frames per second, then the delta time value is 1.0, effectively enacting no change. If the frame rate drops to 30 fps, then the delta time increases to 2.0, making all values twice the size to compensate for the slower frame rate.
Where to Use Delta Time
For a moving object which changes position, rotation, or scale.
Manual countdowns in the frame update, as you may want these countdowns to reflect time instead of how many frames have passed.
Most values which are changed over time.
The Delta Time Function
The function below calculates the delta time value which you can use in your frame update function. Corona games can run at two different frames-per-second rates: 30 and 60. The function below is set up for 60 fps, but you can adjust it for a 30 fps game by changing (1000/60) to (1000/30).
local runtime = 0
local function getDeltaTime()
local temp = system.getTimer() --Get current game time in ms
local dt = (temp-runtime) / (1000/60) --60fps or 30fps as base
runtime = temp --Store game time
return dt
end
If you print out the delta time value for each frame, it will look something like this:
0.95916, 1.00638, 0.924, 0.9667, ...
The value will be very close to 1.0 when the game runs at 59-61 frames a second, but it will change more significantly if the frame rate fluctuates more severely.
Implementing Delta Time
Assuming you have the above function in your game code, implementing delta time shouldn’t change your existing code too much. However, just be careful to only multiply it by changing values, not the entire value, as illustrated in the code below.
--A box object
local box = display.newRect( 50, 0, 100, 100 )
--Frame update function
local function frameUpdate()
--Delta Time value
local dt = getDeltaTime()
--Move your box, 5 pixels with delta compensation
box:translate( 0, 5.0*dt )
--For rotation...
--INCORRECT: do not multiply with the entire value!
--box.rotation = (box.rotation+1) * dt
--CORRECT: only multiply with the changing value:
box.rotation = box.rotation + (1*dt)