SnutiToday’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).

If you print out the delta time value for each frame, it will look something like this:

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.

Testing Delta Time

To confirm that your setup is working properly and that your game has become “framerate independent,” just change the fps value in config.lua to 30 and 60, back and forth over several tests. Your game should appear to run at the exact same speed, except that the 30 fps tests will likely seem a bit “choppy.”

That’s it! Your game should now appear to run at the same speed no matter what the frame rate is. This technique can be used for all types of delta-related processes, so feel free to expand its functionality to suit your needs.

Source article – 

Guest Tutorial: Delta Time in Corona