Game loop and time

From makifbaysal/tasktrooper · View interactive version

Game loop and time is an AI agent skill from the GitHub repository makifbaysal/tasktrooper. Use when writing anything that moves, animates, counts down or simulates - per-frame vs fixed-step callbacks per engine, the accumulator loop with interpolation, delta-scaled smoothing, pause and time scale, and determinism.

How to use this skill

Skill file

Game Loop and Time

Overview

Two clocks run in every game: the render frame (variable, as fast as the machine allows) and the simulation step (fixed, so physics and rules behave the same everywhere). Most "it moves faster on my machine", tunnelling, jitter and desync bugs come from putting work on the wrong clock or forgetting to scale by the right delta.

Core principle: Simulate on a fixed step, render with interpolation, and scale every time-based value by the delta of the clock it runs on.

Where each clock lives

| Engine | Per frame (variable) | Fixed step | Default fixed rate |

|---|---|---|---|

| Unity | Update, LateUpdate — Time.deltaTime | FixedUpdate — Time.fixedDeltaTime | 0.02 s (50 Hz) |

| Godot | _process(delta) | _physics_process(delta) | 60 ticks/s (physics/common/physics_ticks_per_second) |

| Unreal | Tick(DeltaSeconds) | physics substepping / async physics tick | project physics settings |

| Bevy | Update — Res<Time> | FixedUpdate — Res<Time> is Time<Fixed> there | 64 Hz |

| Phaser | update(time, delta) (ms) | Arcade physics' own step (fps, fixedStep) | 60 |

| PixiJS 8 | ticker.add(t => …) — t.deltaMS | your accumulator | — |

| three.js / Babylon.js | setAnimationLoop + THREE.Timer / engine.getDeltaTime() (ms) | your accumulator, or the physics plugin's | — |

| MonoGame | Update(GameTime) | IsFixedTimeStep = true (default) | 1/60 s |

| pygame | dt = clock.tick(60) / 1000 | your accumulator | — |

Use the engine's fixed callback for physics and authoritative rules. Write your own loop only where the engine has none (web renderers, pygame, custom servers).

The accumulator loop (web, pygame, servers)

const STEP = 1 / 60;
const MAX_FRAME = 0.25;           // clamp: a 3 s tab-switch must not run 180 steps
let acc = 0;
let last = performance.now();

function frame(now: number) {
  const frameTime = Math.min((now - last) / 1000, MAX_FRAME);
  last = now;
  acc += frameTime;
  while (acc >= STEP) {
    previous.copyFrom(current);   // keep the last state for interpolation
    simulate(current, STEP);      // rules + physics, fixed dt only
    acc -= STEP;
  }
  render(interpolate(previous, current, acc / STEP));
  requestAnimationFrame(frame);
}
requestAnimationFrame(frame);

Scale by delta — including smoothing

// ❌ frame-rate dependent: twice as fast at 120 FPS, crawls at 30
transform.position += velocity;
camera.position = Vector3.Lerp(camera.position, target, 0.1f);

// ✅ delta-scaled; exponential smoothing that reads the same at any frame rate
transform.position += velocity * Time.deltaTime;
float t = 1f - Mathf.Exp(-sharpness * Time.deltaTime);
camera.position = Vector3.Lerp(camera.position, target, t);
# ❌ counts frames: a 1 s cooldown is 0.42 s at 144 FPS
var frames_left := 60
func _process(_delta: float) -> void:
    frames_left -= 1

# ✅ counts seconds
var cooldown_left := 1.0
func _process(delta: float) -> void:
    cooldown_left = maxf(0.0, cooldown_left - delta)

Acceleration needs care too: velocity += accel * dt; position += velocity * dt (semi-implicit Euler) is stable at a fixed step and drifts at a variable one — another reason to simulate on the fixed clock.

Pause, slow motion and UI time

Determinism, when the design needs it

Replays, lockstep RTS and rollback netcode need the same inputs to produce the same state on every machine:

Testing time-based code

it("covers the same distance at 30 and 60 steps per second", () => {
  const a = new Mover({ speed: 5 }); for (let i = 0; i < 60; i++) a.step(1 / 60);
  const b = new Mover({ speed: 5 }); for (let i = 0; i < 30; i++) b.step(1 / 30);
  expect(a.x).toBeCloseTo(5, 5);
  expect(b.x).toBeCloseTo(a.x, 5);
});

Inject dt; never sleep in a test. Engine tests advance frames explicitly (yield return new WaitForFixedUpdate(), runner.simulate_frames(n), app.update() with a manual time strategy).

Common Mistakes

Red Flags

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.