How Infinite Mass Works in Matter.js

In Matter.js, assigning an infinite mass to a rigid body alters how it interacts with forces, velocities, and collisions within the 2D physics simulation. This article explains how infinite mass is calculated under the hood, how it influences collision responses with dynamic bodies, the functional distinction between static bodies and dynamic bodies with infinite mass, and how to properly manipulate these objects in your simulation.

The Mathematical Mechanism: Inverse Mass

Physics engines like Matter.js avoid dividing by zero during collision resolution by calculating with inverse mass (\(1 / \text{mass}\)) rather than mass directly.

When a body is assigned infinite mass (mass: Infinity):

Similarly, infinite rotational inertia (inertia: Infinity) sets inverseInertia to 0, preventing angular acceleration regardless of the torque applied.

Collision Dynamics

When a standard dynamic body collides with an infinite-mass body:

Infinite Mass vs. Static Bodies

While static bodies (isStatic: true) utilize infinite mass, there is an important behavioral difference between a static body and a non-static body explicitly given infinite mass:

  1. Static Bodies (isStatic: true): Matter.js automatically sets mass: Infinity, inverseMass: 0, inertia: Infinity, and inverseInertia: 0. Additionally, the engine removes the body from regular position integration, optimizing performance and collision detection. Static bodies are strictly intended to remain stationary (such as floors, walls, and terrain).

  2. Dynamic Bodies with Infinite Mass: If a body has isStatic: false but its mass is set to Infinity, it functions essentially as a kinematic body. It still processes velocity vectors and moves through the world if given an initial linear or angular velocity, but it will completely disregard external collisions and forces. It cannot be stopped, deflected, or slowed down by any other body in the simulation.

Manual Movement and Teleportation

Because infinite-mass bodies do not respond to Body.applyForce, their motion must be controlled directly: