Delta time is an important concept for developing frame rate-independent games. Frame rate independency is especially important 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.

Delta time (as you can understand from the name) is the elapsed time since the game’s last update (such as the elapsed time between the previous and current frame). So, it is calculated as:

DeltaTime = CurrentTime - OldTime

Depending on how you are getting the time, you may need to perform some extra operations (such as dividing the result by 1 000 000 as suggested here). Usually, the game engines calculate the delta time automatically and give you as a property. For example, you can get delta time as Time.deltaTime in Unity.

Frame rate independency is an important thing for having a proper video game. 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.

// 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 count 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.

// 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.

You can also check the links below if you feel you need more info: