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):
- Its
inverseMassproperty becomes0. - Acceleration is calculated using Newton's second law in the form \(a = F \times \text{inverseMass}\).
- Because
inverseMassis zero, any force applied to the body yields zero acceleration (\(a = 0\)).
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:
- Zero Momentum Transfer: The infinite-mass body absorbs impulses without altering its own velocity or trajectory. It acts as an unyielding boundary.
- Full Rebound on Dynamic Bodies: The dynamic body receives the entire relative impulse of the collision. Its rebound velocity is determined solely by the combined restitution (bounciness) and friction coefficients defined between the two colliding fixtures.
- Non-Reciprocal Pushing: An infinite-mass body will push dynamic bodies out of its path effortlessly without slowing down, behaving like an unstoppable object.
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:
Static Bodies (
isStatic: true): Matter.js automatically setsmass: Infinity,inverseMass: 0,inertia: Infinity, andinverseInertia: 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).Dynamic Bodies with Infinite Mass: If a body has
isStatic: falsebut its mass is set toInfinity, 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:
- Setting Velocity: You can assign linear movement
using
Body.setVelocity(body, { x, y }). The body will move at this constant speed, bulldozing through any dynamic obstacles. - Setting Position: You can directly update the
location using
Body.setPosition(body, { x, y }). This teleports the body to the target coordinates, resolving overlaps with dynamic bodies by forcefully ejecting them in the next engine tick. - Angular Velocity: Rotation can be driven manually
via
Body.setAngularVelocity(body, velocity)orBody.setAngle(body, angle).