Quick Snips: Delta Time

This is a new series I want to work on called "Quick Snips". These are articles that are short and quick, and covers mainly the small stuff that people mostly ignore and take for granted that it is a required thing to create good games.
 

In this article, we will talk about one (slightly mysterious) variable in love.update(), known as dt or Delta Time.

What is DT?

Delta Time, also known as dt, is how much time has passed between 2 frames, hence the "delta" part of the word. Delta Time is also approximately equal to the reciprocal of the current frames per second (FPS), and consequently, FPS is approximately equal to the reciprocal of Delta Time:

dt = 1/fps
fps = 1/dt

Let's look at one way to use DT.

Counting seconds

One way to use DT is to count time, as DT multiplied by FPS is approximately equal to 1 second. You can do that by adding a global timer variable and adding the DT variable to it.

Here's an implementation of a simple timer using love.update(dt).

timer = 0
function love.update(dt)
    -- this adds dt every frame, adding 1 every second
    timer = timer + dt
    -- this checks if timer is greater than the time interval (in seconds)
    if timer >= timeInterval then
        -- subtract timer by interval
        timer = timer - timeInterval
        -- event
    end
end

You can use this to fire events, for example, fire bullets every 1 second, or reload a gun every 5 seconds.

Note: Do not declare timer inside the love.update(dt) function! That will reset the timer every frame, which is not what we wanted.

Constant speed

Another way to use DT is to make objects move at constant speed. By constant I mean that it ideally looks the same at any framerate (I said ideally because DT isn't exact, but it's okay for most purposes). You can do that by multiplying the speed by DT. This has the effect of the object moving at that speed every second.

function love.update(dt)
    -- this has the effect of moving at a set speed every second, for example, 500 pixels every second
    object.x = object.x + speed*dt
end

Constant Acceleration

Another way to use DT is to add constant acceleration. Like before, by constant I mean it looks the same at any machine, slow or fast. You can do this by adding speed by acceleration multiplied by dt.

function love.update(dt)
    speed = speed + accel*dt
    object.x = object.x + speed*dt
end

Why use Delta Time?

So, why do you want to use DT? Well, here are the 2 main reasons:

  • So that the movement of your objects looks, ideally, the same at any machine, slow or fast.
  • Because it is easy to keep track of time using DT other than LÖVE's other timing functions.

Well, that's pretty much the reasons why you would want to use DT.

Alright, I'm done with this article, I hope you learned why many lovers (pretty much every non-beginner) use DT. Any typos or suggestions? Comment here, or send me a PM in the LÖVE Forums!

Comments

Roland_Y's picture

Great.

Well, there are some snippets on the wiki, about capping/controlling the framerate. That might sound off-topic,  but anyway, I'll say it: maybe you should include a link to that wiki article ?

I'll also point out that interesting topic, which is all about messing with dt (and framerate), and also this tech demo, as well.

 

potomak's picture

I think that tying physics with frame rate is not ideal and since dt is dependent from frame rate you shouldn't integrate position or velocity using dt directly.

More details and references: http://gamedev.stackexchange.com/questions/1589/fixed-time-step-vs-variable-time-step