How to Make a Body Static in Matter.js
In Matter.js, static bodies are immovable physical objects that do not respond to forces, collisions, or gravity, making them ideal for ground planes, platforms, and boundary walls. This guide covers how to define a body as static during its initial creation and how to toggle an existing dynamic body to static at runtime using the engine's built-in methods.
Method 1: Set Static During Initialization
The most common way to make a body static is to specify the
isStatic property inside the options object when creating
the shape with the Matter.Bodies module.
const { Bodies } = Matter;
// Create a static rectangular floor
const ground = Bodies.rectangle(400, 600, 810, 60, {
isStatic: true
});
// Add the body to your world
Composite.add(engine.world, ground);By passing { isStatic: true }, Matter.js automatically
sets the body's mass and inertia to infinity and zero-out its
velocities.
Method 2: Set Static Dynamically After Creation
If a body is already created as a dynamic object and needs to become
static later—such as an obstacle freezing in place—use the
Body.setStatic() method provided by the
Matter.Body module.
const { Body, Bodies } = Matter;
// Create a dynamic body (isStatic defaults to false)
const box = Bodies.rectangle(400, 200, 80, 80);
Composite.add(engine.world, box);
// Later in your code, make the box static
Body.setStatic(box, true);To make a static body dynamic again, pass false as the
second argument:
// Revert the body back to dynamic
Body.setStatic(box, false);Important Consideration
Avoid setting the property directly via
body.isStatic = true. Mutating the property directly
bypasses internal calculations for mass, inverse mass, inertia, and
velocity. Always use Matter.Body.setStatic(body, true) to
ensure the physics engine accurately updates the simulation state.