We are going to explain delta time, how it's used for frame-rate independence, show a code example, and discuss problems of not using it.
Delta time (often denoted as dt) is the time elapsed between the current frame and the previous frame (or a fixed time step). It's usually measured in seconds.
In a game loop, the game logic (like movement, physics, etc.) should be updated based on time, not per frame. If we update by a fixed amount per frame, then the speed of the game will depend on the frame rate. For example, if we move an object 5 units per frame, then at 60 FPS it moves 300 units per second, but at 30 FPS it moves 150 units per second. This is not desirable.
To make the game frame-rate independent, we multiply our per-frame changes by delta time. So if we want an object to move at 100 units per second, we do:
position += speed * delta_time;
Now, regardless of frame rate (if we measure delta time in seconds), the object will move 100 units every second.
Example code (in a pseudo-code style, similar to many game engines):
float lastFrameTime = 0.0f;
while (game_is_running) {
float currentTime = get_current_time(); // in seconds
float deltaTime = currentTime - lastFrameTime;
lastFrameTime = currentTime;
// Update game logic using deltaTime
update_game_logic(deltaTime);
// Render
render();
}
In update_game_logic(deltaTime), we might have:
void update_game_logic(float dt) {
// Move player at 5 units per second
player.position.x += player.velocity.x * dt;
player.position.y += player.velocity.y * dt;
}
Alternatively, if we have an object that should move at a constant speed in a direction, we can do:
void update(float dt) {
transform.position += direction * speed * dt;
}
Problems if we don't use delta time in a variable frame rate game:
- Inconsistent game speed: The game will run faster on high FPS and slower on low FPS. This can break the intended experience.
- Physics and collision detection may become unstable because the time step is not constant. Many physics engines require a fixed time step for stability, but if we don't use delta time and the frame rate varies, then the time step varies, which can cause jitter, tunneling, or even crashes in extreme cases.
- Networked multiplayer games may have issues because the state updates are tied to frame rate, making it hard to synchronize.
However, note that sometimes we use a fixed time step for the game logic update (especially for physics) and then use interpolation for rendering. But the principle of using time to scale movement remains.
Let me write a simple example in a more concrete way, say in C++ (but the concept is the same in any language):
#include <chrono>
class Game {
private:
bool running;
float playerX;
float playerSpeed; // units per second
public:
void run() {
running = true;
playerX = 0.0f;
playerSpeed = 5.0f; // 5 units per second
auto lastTime = std::chrono::high_resolution_clock::now();
while (running) {
auto currentTime = std::chrono::high_resolution_clock::now();
std::chrono::duration<float> delta = currentTime - lastTime;
lastTime = currentTime;
float deltaTime = delta.count();
update(deltaTime);
render();
}
}
void update(float dt) {
// Move the player
playerX += playerSpeed * dt;
}
void render() {
// Drawing code here
}
};
In this example, playerX is updated by playerSpeed * dt, so if dt is 0.0167 seconds (about 60 FPS), then the player moves 0.0835 units per frame. If the frame rate drops to 30 FPS (dt ~ 0.0333), then the player moves 0.1665 units per frame, but over one second the movement is the same (5 units).
Without delta time, we would have:
playerX += playerSpeed; // assuming playerSpeed is per frame, not per second
Then at 60 FPS, player moves 560 = 300 units per second, and at 30 FPS, 530 = 150 units per second.
That's the core of delta time.
Delta Time in Game Loops
What is Delta Time?
Delta time (dt) is the time elapsed (in seconds) between the current frame and the previous frame. It represents how long each iteration of the game loop takes to execute.
Typical values:
- 60 FPS →
dt ≈ 0.0167 seconds
- 30 FPS →
dt ≈ 0.0333 seconds
- Variable FPS →
dt changes every frame
How It Makes Games Frame-Rate Independent
Instead of updating game state by fixed amounts per frame (e.g., position += 5), we scale updates by dt:
Frame-dependent (BAD):
position += 5; // Moves 5 units per FRAME
- At 60 FPS: 300 units/second
- At 30 FPS: 150 units/second ❌
Frame-independent (GOOD):
position += speed * dt; // Moves `speed` units per SECOND
- At any FPS: moves exactly
speed units/second ✅
Code Example: Position Update with Delta Time
// Game loop structure (simplified)
float lastTime = 0.0f;
while (gameRunning) {
float currentTime = getCurrentTimeInSeconds(); // e.g., glfwGetTime()
float deltaTime = currentTime - lastTime;
lastTime = currentTime;
// Update game logic using deltaTime
updateGame(deltaTime);
// Render
render();
}
// Update function
void updateGame(float dt) {
// Object moving at 10 units/second to the right
player.x += player.speed * dt; // 10 * dt
// Rotation at 90 degrees/second
player.rotation += player.angularSpeed * dt; // 90 * dt
// Physics (if using simple Euler integration)
player.velocity += acceleration * dt;
player.position += player.velocity * dt;
}
Problems Without Delta Time (Variable Frame Rate)
1. Inconsistent Game Speed
- Game runs faster on high-FPS systems (powerful PCs)
- Game runs slower on low-FPS systems (older hardware)
- Breaks intended pacing (e.g., race car feels sluggish on 30 FPS but hyperspeed at 144 FPS)
2. Physics Instability
- Physics engines (Box2D, Bullet) require stable time steps
- Variable
dt causes:
- Jittery collisions
- Tunneling (objects pass through each other)
- Explosions or "spinning" behavior
- Non-reproducible results (hard to debug)
3. Desynchronized Systems
- Animation playback speeds vary
- Timer-based events (cooldowns, buffs) trigger at wrong moments
- Networked multiplayer: clients update at different rates → desync
4. Unfair Advantage
- In competitive games, players with higher FPS gain:
- Smoother controls
- Faster game simulation
- Better reaction times
Best Practices & Advanced Notes
Fixed Timestep for Physics
For deterministic physics, use a fixed timestep (e.g., 1/60 sec) and accumulate dt:
float accumulator = 0.0f;
const float fixedDt = 1.0f / 60.0f; // 60 Hz physics
while (running) {
float frameDt = getDeltaTime();
accumulator += frameDt;
while (accumulator >= fixedDt) {
updatePhysics(fixedDt); // Always 0.0167s
accumulator -= fixedDt;
}
// Interpolate rendering between physics states
float alpha = accumulator / fixedDt;
render(alpha);
}
Common Pitfalls
dt Spikes: If a frame takes unusually long (e.g., 0.5s), clamping dt prevents "teleporting":
dt = min(dt, 0.1f); // Cap at 100ms
- Using
dt inaudio: Audio systems often need exact timing—use separate audio clocks.
- Mobile/VSync: Mobile devices use variable refresh rates; always use
dt.
Summary
- Delta time = time since last frame
- Multiply movement/physics by
dt for frame-rate independence
- Without
dt: game speed varies with hardware, physics breaks, multiplayer desyncs
- For physics: use fixed timestep with interpolation
- Always measure
dt from real time, not frame counters
By using delta time correctly, you ensure your game runs consistently at the same speed on a toaster and a supercomputer—critical for fair gameplay, reliable physics, and professional polish.