What Is a Rigid Body in Matter.js?

This article provides a comprehensive overview of rigid bodies within the Matter.js 2D physics engine. It covers the fundamental definition of a rigid body, explains its key physical properties such as mass, friction, and restitution, distinguishes between static and dynamic types, and demonstrates how these bodies are created and manipulated to simulate realistic mechanics in web applications.

In Matter.js, a rigid body is an idealized solid physical object that does not deform, bend, or change shape under any circumstances, regardless of the forces, torques, or collisions applied to it. The distance between any two given points on the body remains constant at all times. Represented primarily by the Matter.Body module, rigid bodies serve as the foundational building blocks for simulating 2D kinematics and dynamics.

Dynamic vs. Static Rigid Bodies

Matter.js categorizes rigid bodies into two primary operational states:

Core Properties of a Rigid Body

When you instantiate a rigid body in Matter.js, it contains several distinct sets of properties that govern its behavior:

Creating and Using Rigid Bodies

Rigid bodies are typically generated using the Matter.Bodies factory module, which provides pre-configured geometric primitives such as rectangles, circles, trapezoids, and regular polygons:

// Creating a dynamic rectangular rigid body
const box = Matter.Bodies.rectangle(400, 200, 80, 80, {
    restitution: 0.8,
    friction: 0.05
});

// Creating a static ground surface
const ground = Matter.Bodies.rectangle(400, 600, 810, 60, {
    isStatic: true
});

// Adding bodies to the simulation world
Matter.Composite.add(engine.world, [box, ground]);

Once added to the simulation world, the Matter.js engine continuously resolves collisions, updates positions via numerical integration, and enforces non-penetration constraints between these rigid shapes.