← All Exercises

Clamp Fixed-Timestep Simulation Steps

basicjavascript~20min

Background

Variable frame times make physics non-deterministic. Fixed timesteps with a clamp keep the simulation stable when a frame stalls.

Problem

Given a frame delta, timestep length and a maximum step count, compute how many simulation steps to run and how much delta remains.

Input

JSON object: { deltaMs, stepMs, maxSteps }

Output

JSON object: { steps, remainingMs, clamped }

Sample tests

Sample 1

Input: {"deltaMs":50,"stepMs":16,"maxSteps":4}
Expected: {"steps":3,"remainingMs":2,"clamped":false}

Sample 2

Input: {"deltaMs":400,"stepMs":16,"maxSteps":4}
Expected: {"steps":4,"remainingMs":336,"clamped":true}

Your solution

Learning objective

Keep a game loop deterministic under frame stalls.