Static vs Dynamic Bodies in Matter.js Explained

In Matter.js, rigid bodies represent the physical objects that interact within a simulation, and they fall into two primary categories: static bodies and dynamic bodies. The core difference between the two lies in how they respond to forces, gravity, and collisions. While dynamic bodies move freely, respond to physics forces, and bounce off other objects, static bodies remain completely fixed in place, acting as immovable obstacles or boundaries. Understanding how to configure and utilize these two body types is fundamental to building any realistic physics simulation with the library.

What is a Dynamic Body?

By default, any rigid body created in Matter.js is a dynamic body. Dynamic bodies possess standard physical properties such as mass, density, velocity, restitution (bounciness), and friction.

Key characteristics of dynamic bodies include:

Common use cases for dynamic bodies include player-controlled characters, falling crates, bouncing balls, projectiles, or any element meant to simulate natural physical motion.

What is a Static Body?

A static body is created by explicitly setting the isStatic property to true in its configuration options. Conceptually, a static body has infinite mass and infinite inertia within the physics calculation engine.

Key characteristics of static bodies include:

Common use cases for static bodies include floors, boundary walls, non-destructible obstacles, and terrain.

Key Differences at a Glance

Feature Dynamic Bodies Static Bodies
Default State Yes (isStatic: false) No (requires isStatic: true)
Mass & Inertia Finite, calculated from size and density Infinite
Affected by Gravity Yes No
Pushed by Collisions Yes No
Primary Purpose Movable objects, physics actors Floors, walls, immovable boundaries

Code Implementation Example

Configuring whether a body is dynamic or static is handled during body initialization via the options object:

// Creating a dynamic body (default)
const dynamicBox = Matter.Bodies.rectangle(400, 200, 50, 50, {
  restitution: 0.8,
  density: 0.001
});

// Creating a static body
const staticFloor = Matter.Bodies.rectangle(400, 600, 800, 50, {
  isStatic: true
});

// Adding both to the engine world
Matter.Composite.add(engine.world, [dynamicBox, staticFloor]);

You can also dynamically change a body's type during runtime using the Body.setStatic() method:

// Convert a dynamic body into a static body
Matter.Body.setStatic(dynamicBox, true);

// Convert a static body into a dynamic body
Matter.Body.setStatic(staticFloor, false);

Performance Considerations

Static bodies offer performance advantages in Matter.js because the engine does not need to compute collision resolutions or trajectory paths between two static bodies. Keeping non-moving environment pieces strictly static reduces unnecessary collision checks and keeps simulation frame rates smooth.