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:
- Dynamic Bodies: These bodies have finite mass and are fully influenced by the simulation's physical forces, including gravity, applied impulses, and collision responses. They move, rotate, and accelerate dynamically based on Newton's laws of motion.
- Static Bodies: Configured by setting the
isStatic: trueproperty, static bodies have infinite mass and inertia. They are unaffected by gravity, forces, or collisions with dynamic bodies, remaining locked in place. They are typically used to create immovable boundaries, floors, platforms, and obstacles.
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:
- Spatial Properties:
position: A vector{x, y}representing the body's center of mass in 2D space.angle: The orientation of the body in radians.velocityandangularVelocity: The current rates of linear and rotational displacement.
- Mass and Inertia:
mass: Determines resistance to linear acceleration. Matter.js automatically computes this based on the body's area and density.density: The mass per unit area.inertia: Determines resistance to rotational acceleration (torque).
- Material and Surface Behaviors:
restitution: The elasticity or "bounciness" of the body, defined on a scale from0(completely inelastic) to1(completely elastic).friction: The kinetic and static friction coefficient determining resistance when sliding against other bodies.frictionAir: Linear drag that slows the body down as it moves through space, simulating air resistance.
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.