Why Use a Capsule Collider in Matter.js Platformers
Using a capsule-shaped collider instead of a standard rectangle significantly improves the movement, stability, and overall feel of a 2D platformer character in Matter.js. While rectangular bodies are intuitive to set up, their sharp lower corners introduce friction artifacts, edge-catching issues, and uneven movement over varied terrain. Replacing a rectangle with a capsule—or a rounded compound body—eliminates common physics glitches like snagging on tile seams, enables natural slope climbing, and provides smoother interactions with ledges.
Eliminating Snagging on Tile Seams
In tile-based platformers, floors and walls are typically constructed from adjacent rectangular colliders placed side by side. When a rectangular character slides across a perfectly flat floor composed of multiple tiles, its bottom-leading corner will frequently collide with the microscopic edge variations or internal vertices of the adjoining tiles. This phenomenon, often called "ghost collision" or snagging, causes the character to suddenly stop, bounce, or lose forward momentum.
A capsule shape replaces the sharp bottom corners with a smooth semicircle. Because the leading edge slopes upward, it naturally glides over seams between adjacent colliders, ensuring seamless movement across modular surfaces without requiring you to merge your ground geometry into a single continuous polygon.
Smoother Slope and Stair Navigation
Rectangular colliders struggle with vertical changes in geometry. When a rectangular character approaches a ramp, the bottom corner strikes the incline at a flat angle, which acts as a wall and completely halts horizontal velocity unless high force or complex custom movement logic is applied. Navigating small steps or stairs presents the exact same issue.
The rounded bottom of a capsule acts as a built-in step-up mechanism. When the circular base touches an incline or small step, the contact normal pushes the body both backward and upward. This redirection of force allows the character to naturally pop up and roll over minor height variations, drastically reducing the amount of manual velocity correction code you need to write.
Preventing Unwanted Ledge Catches
Precision jumping is a fundamental mechanic in platformers. When a rectangular character jumps close to a vertical wall or the corner of a floating platform, the character's top corners can hook onto the platform's bottom edge, killing vertical momentum. Similarly, when dropping off a ledge, a rectangle’s rear corner can drag against the corner of the platform, throwing off the intended arc of the jump.
A capsule's curved top and bottom edges deflect away from corners rather than snagging on them. If a player slightly clips the bottom corner of a platform while jumping upward, the rounded dome nudges the character smoothly outward, resulting in more forgiving and satisfying movement mechanics.
Implementing a Capsule in Matter.js
Matter.js does not include a native primitive named
capsule, but you can easily construct one using a compound
body. You combine a central rectangle with two circles positioned at the
top and bottom:
const playerWidth = 30;
const playerHeight = 60;
const radius = playerWidth / 2;
// Create the core components
const rect = Matter.Bodies.rectangle(x, y, playerWidth, playerHeight - playerWidth);
const topCircle = Matter.Bodies.circle(x, y - (playerHeight - playerWidth) / 2, radius);
const bottomCircle = Matter.Bodies.circle(x, y + (playerHeight - playerWidth) / 2, radius);
// Combine into a single capsule body
const player = Matter.Body.create({
parts: [rect, topCircle, bottomCircle],
inertia: Infinity, // Lock rotation to keep the capsule upright
friction: 0.002
});Setting inertia: Infinity is crucial. It locks the
character’s rotation so the capsule does not topple over while moving,
preserving the benefits of the rounded base while keeping the player
upright at all times.