Using Matter.js Physics in Phaser 3
Yes, you can use Matter.js with Phaser 3, and it is natively integrated directly into the framework. Phaser 3 includes an optimized version of the Matter.js 2D physics engine out of the box, allowing developers to implement advanced simulations with rigid bodies, constraints, composite shapes, and realistic mass without installing external plugins. This guide explains how Phaser 3 integrates Matter.js, how to enable it in your configuration, and when to use it over alternative physics engines.
Native Integration
Phaser 3 provides first-class support for Matter.js alongside its lightweight Arcade Physics system. You do not need to import or install Matter.js separately. Phaser wraps Matter.js's core functions into simple, game-oriented APIs exposed through scene managers and factory methods, making advanced physics accessible while retaining the full depth of Matter's native engine.
How to Enable Matter.js in Phaser 3
To use Matter.js, you specify it as the default physics engine in your game configuration object.
const config = {
type: Phaser.AUTO,
width: 800,
height: 600,
physics: {
default: 'matter',
matter: {
gravity: { y: 1 },
debug: true
}
},
scene: [ MyScene ]
};
const game = new Phaser.Game(config);Setting debug: true draws outlines around collision
boundaries, vectors, and velocity lines, which is essential for
diagnosing complex collisions during development.
Adding Matter Objects in Scenes
Once enabled, Matter.js is accessible in your scenes via
this.matter. You can create standard physics bodies,
sprites, or composite objects:
- Physics Sprites: Use
this.matter.add.sprite(x, y, 'texture')to create a game object that behaves like a rigid body. - Static and Dynamic Bodies: You can create simple
shapes like rectangles, circles, or polygons using
this.matter.add.rectangle()orthis.matter.add.circle(). - World Boundaries: By default, Matter creates world
walls based on game boundaries, or you can configure custom bounds using
this.matter.world.setBounds().
Key Features of Matter.js in Phaser 3
- Complex and Concave Polygons: Unlike Arcade Physics, which relies primarily on axis-aligned bounding boxes (AABB) and circles, Matter.js supports arbitrary polygons, multi-body convex decompositions, and concave shapes.
- Physics Constraints and Springs: You can easily
create joints, ropes, springs, and ragdoll systems using
this.matter.add.constraint(). - Compound Bodies: Multiple fixtures can be combined into a single rigid body, enabling vehicles with distinct wheels or characters with separate hitboxes.
- Realistic Physical Properties: Bodies support properties like mass, density, restitution (bounciness), static and dynamic friction, and inertia.
Matter.js vs. Arcade Physics
Choose Arcade Physics if you are building fast-paced 2D platformers, shoot-'em-ups, or games that require simple rectangular collision detection without realistic mass or momentum. Arcade Physics is significantly faster and uses fewer CPU resources.
Choose Matter.js if your game relies on realistic physical interactions, such as tumbling objects, stacking puzzles, rolling wheels, swinging ropes, or custom polygon-based terrain.