Why you need delta time while developing games

Contents

Framerate and problem with machine speed

Every machine has a different speed, which will affect the game’s framerate. On faster devices like desktop computers, the frequency of rendering calls is much higher than on mobile devices like smartphones and tablets. If game object movements are simply updated with each call to the render method without accounting for the update frequency, the animation will vary from device to device, ultimately degrading the user experience and affecting the gameplay.

A simple example

To illustrate the problem, take a look at this code snippet which draws a scene with a spaceship. The spaceship moves from left to right and its horizontal position is updated with every frame.


    public void render() {
        // Clear the screen
        ScreenUtils.clear(1, 1, 1, 1);
        // Increase spaceship x position, make it move horizontally
        shipX += SHIP_SPEED;
        // Draw ship with new shipX value
        batch.begin();
        batch.draw(shipTexture, shipX, 0, SHIP_WIDTH, SHIP_HEIGHT);
        batch.end();
    }

If you test the code, it work just fine and the ship will move on as expected. The ship speed is controlled by SHIP_SPEED constant and to make it move faster or slower you can simply change SHIP_SPEED value.

The hidden culprit is that on different devices with varying computing speeds and power, the call rate of the render method will differ. On a faster machine, the call rate of render may be twice as high as the rate on a slower machine, resulting in the shipX value is updated twice as fast with the same SHIP_SPEED value.

What is delta time

Delta time is the time difference between two consecutive render calls. Essentially, it’s the time a machine takes to render one frame before moving on to the next. Delta time varies between devices with different speed and also varies on each render call. A frame with a smaller number of objects will take less time to render than a frame with a larger number of objects and vice versa.

Because delta time is a relative value that depends on machine speed and computing power, we can use it as a factor to synchronize animation across different devices.

Let’s modify the code above to take into account the delta time.


    public void render() {
        // Clear the screen
        ScreenUtils.clear(1, 1, 1, 1);
        // Obtain delta time
        float delta = Gdx.graphics.getDeltaTime();
        // Increase spaceship x position, make it move horizontally
        shipX += SHIP_SPEED * delta;
        // Draw ship with new shipX value
        batch.begin();
        batch.draw(shipTexture, shipX, 0, SHIP_WIDTH, SHIP_HEIGHT);
        batch.end();
    }

Notice the amount of added value for shipX is now proportional to delta time. On a machine with a high updating frequency, the delta time is small; hence, shipX increases at a smaller pace but with higher frequency.

In contrast, a slower machine takes longer to render the frame, which leads to a larger delta time value. shipX will increase at a larger pace but slower frequency. The difference in updating frequency is compensated for by delta time, which ultimately synchronizes the animation between different devices.

Conclusion

Many devices differ in their computing power, which directly impacts the gameplay and animation you develop for games on those devices. To resolve this issue, apply delta time, a relative value that helps synchronize animations and ultimately creates smoother gameplay.