Matter.js Body timeScale Property Explained

The timeScale property on a specific body in Matter.js is a local modifier that controls the speed of simulation for that individual physics body relative to the rest of the world. While the engine typically updates all bodies uniformly, setting a unique timeScale on a single body allows developers to create localized slow-motion, fast-forward, or freeze effects without altering the global physics timing. This article explains how the body.timeScale property works, how to implement it, and the technical considerations to keep in mind.

How body.timeScale Works

In Matter.js, every rigid body created via Matter.Bodies or Matter.Body.create inherits a set of default properties, including timeScale. By default, this value is set to 1, meaning the body updates at normal speed synchronized with the physics engine.

When you modify this property, it scales the integration step for that specific body:

Code Example

You can set the timeScale during body instantiation or adjust it dynamically during runtime:

// Setting timeScale at creation
const slowMovingBox = Matter.Bodies.rectangle(400, 200, 80, 80, {
    timeScale: 0.5
});

// Modifying timeScale dynamically
Matter.Body.set(slowMovingBox, 'timeScale', 0.2);

// Or direct assignment
slowMovingBox.timeScale = 1.5;

Global vs. Local Time Scaling

Matter.js provides two levels of temporal control:

  1. engine.timing.timeScale: Affects the entire simulation globally. Changing this slows down or speeds up every body, constraint, and collision resolution simultaneously.
  2. body.timeScale: Affects only the specified body's linear and angular velocity integration. Other bodies in the scene continue to update at their standard rates.

When both are used, the effects compound. For example, if engine.timing.timeScale is 0.5 and a body’s timeScale is 0.5, that specific body effectively updates at 0.25 times the normal speed.

Limitations and Considerations

While body.timeScale is useful for visual effects like "bullet time" on a player character or creating low-gravity-style floating objects, it has limitations: