How to Keep an Object Upright in Matter.js
This article explains how to use constraints in Matter.js to stabilize a rigid body and maintain an upright orientation. Because Matter.js relies on distance-based constraints rather than dedicated angular spring joints, keeping a moving object upright requires applying restoring torque using attachment offsets or dynamically updating anchor points. Below, you will learn the most effective methods to construct and configure these constraints to prevent unwanted tipping.
The Restoring Torque Method (Dynamic Overhead Anchor)
The most flexible way to keep a moving body upright using constraints is to create an artificial restoring torque. By attaching a constraint to the top of the body and continuously positioning its anchor directly above the body in the world space, the constraint acts like a virtual balloon or stabilizing tether.
1. Create the Body and Constraint
Create your target body and a constraint whose pointA is
offset toward the top of the body. Set the world anchor
(pointB) above the body's starting location.
const { Bodies, Constraint, Composite, Events } = Matter;
// Create the main body
const body = Bodies.rectangle(400, 300, 50, 100, {
density: 0.001
});
// Create an upright stabilizer constraint
const uprightConstraint = Constraint.create({
bodyA: body,
pointA: { x: 0, y: -50 }, // Top of the body
pointB: { x: 400, y: 200 }, // Floating anchor point above
stiffness: 0.08, // Strength of the pull
damping: 0.05, // Reduces oscillation
render: { visible: false } // Hide in debug views if desired
});
Composite.add(engine.world, [body, uprightConstraint]);2. Update the Anchor Point Dynamically
Because the object needs to move horizontally and vertically through
your world, update pointB on each engine tick using the
beforeUpdate event. This keeps the anchor directly above
the body's current horizontal position.
Events.on(engine, 'beforeUpdate', () => {
// Keep the anchor directly above the body's center of mass
const verticalOffset = 100;
uprightConstraint.pointB = {
x: body.position.x,
y: body.position.y - verticalOffset
};
});When the body tilts, pointA swings away from alignment
with pointB. The tension in the constraint pulls
pointA back toward the center line, generating a corrective
rotational torque that restores an upright posture.
The Dual-Constraint Method (Fixed Orientation)
If your body should be tethered to a moving platform or another rigid body without rotating at all, you can use two parallel constraints. A single distance constraint allows rotation around the connection point, but two spaced constraints mechanically eliminate angular motion.
// Connect body to a carrier body using two parallel constraints
const constraintLeft = Constraint.create({
bodyA: carrierBody,
bodyB: uprightBody,
pointA: { x: -20, y: 0 },
pointB: { x: -20, y: 0 },
stiffness: 1
});
const constraintRight = Constraint.create({
bodyA: carrierBody,
bodyB: uprightBody,
pointA: { x: 20, y: 0 },
pointB: { x: 20, y: 0 },
stiffness: 1
});
Composite.add(engine.world, [constraintLeft, constraintRight]);Because both points are locked at fixed distances, the connected body cannot tilt relative to the anchor body.
Tuning Tips for Stability
- Stiffness: Higher values (e.g.,
0.1to0.3) snap the body upright quickly, while lower values allow a natural, gentle tilt during acceleration. - Damping: Always pair moderate stiffness with
damping (between
0.02and0.1) to stop the object from continuously swinging like a pendulum after impacts. - Attachment Height: Moving
pointAfurther upward along the local Y-axis increases the mechanical leverage of the restoring torque.