Matter.js Inverse Mass Property Explained

In Matter.js, the inverseMass property represents the mathematical reciprocal of an object's mass (\(1 / \text{mass}\)). Internally, the physics engine relies on this property to optimize computational performance during iterative physics calculations, handle immovable or static bodies without encountering division-by-zero errors, and efficiently resolve collision impulses between interacting bodies.

Handling Static and Immovable Bodies

In physics simulations, static or fixed bodies (such as floors, walls, and immovable obstacles) behave as if they have infinite mass because no amount of applied force can move them. Computing with infinity in software often leads to arithmetic overflows or division-by-zero errors. By using inverse mass, Matter.js represents infinite mass simply as 0. When calculating movement or velocity changes for a static object, multiplying any impulse or force by an inverseMass of 0 cleanly yields zero acceleration without requiring special conditional checks.

Impulse Resolution in Collisions

When two rigid bodies collide, Matter.js calculates an impulse—a sudden change in momentum—to separate them and alter their velocities. The standard formula for collision impulse requires dividing by the sum of the inverse masses of the two interacting bodies:

\[\text{Impulse} \propto \frac{1}{\text{inverseMass}_A + \text{inverseMass}_B}\]

By precomputing and storing inverseMass on each body, the engine can execute these impulse calculations directly during the collision resolution phase without needing to compute reciprocal values for every contact point in every frame.

Computational Performance Optimization

Division is a significantly more CPU-intensive operation than multiplication in floating-point arithmetic. Physics engines execute constraint and collision solving loops dozens or hundreds of times per second across numerous bodies. By caching \(1 / \text{mass}\) as inverseMass, Matter.js converts repeated division operations into faster multiplication operations (\(F \times \text{inverseMass}\) instead of \(F / \text{mass}\)) during its iterative solver loops.

Constraint and Position Correction

Matter.js uses constraints to simulate springs, joints, and distance limits between objects. When a constraint is violated, the engine distributes the corrective displacement between the connected bodies proportional to their masses. Bodies with larger mass move less, while lighter bodies move more. The engine uses inverseMass as a weighting factor to determine exactly what fraction of the positional correction each body must absorb.