Matter.js Engine Time Scaling Explained

This article explores how Matter.js manages simulation speed using its internal time-scaling mechanics. In physics simulations, altering the passage of time is essential for effects like slow-motion, fast-forwarding, or smooth pausing. Matter.js accomplishes this primarily through the timeScale property located on the engine's timing object, scaling the discrete time step applied during each physics calculation cycle without breaking the underlying integration logic.

The Core Mechanism: engine.timing.timeScale

Matter.js controls simulation speed via the engine.timing.timeScale property. By default, this value is set to 1.

When Engine.update(engine, delta) runs on every frame, Matter.js calculates the effective time step by multiplying the frame's elapsed time (delta) by engine.timing.timeScale. The Verlet integration steps that determine body velocities, accelerations, and constraint resolutions then execute using this scaled delta value.

Practical Implementation

Setting the time scale is directly handled through the engine instance:

// Create an engine
const engine = Matter.Engine.create();

// Slow down time by half
engine.timing.timeScale = 0.5;

// Speed up time
engine.timing.timeScale = 1.5;

// Reset to standard speed
engine.timing.timeScale = 1.0;

Physical Stability and Tunneling

Altering timeScale directly impacts the numerical stability of the Verlet integrator:

Time Scaling vs. Frame Rate

Matter.js separates frame delta management from the physical simulation rate. If you are using Matter.Runner, it automatically measures actual elapsed time and passes it into Engine.update. The timeScale multiplier is applied internally after this measurement. Consequently, fluctuating display refresh rates (e.g., 60Hz vs. 144Hz) are compensated for by the runner, ensuring that engine.timing.timeScale remains a uniform scalar independent of hardware performance.