Phaser Box2D

A high-performance, industry-standard 2D physics engine built on Box2D v3, delivering incredibly realistic and engaging web game experiences.
This repository contains the complete Phaser Box2D library, along with:
- A Getting Started Guide
- Phaser Box2D API Documentation
- A Vite Phaser Box2D Project Template
See https://phaser.io/box2d for details about Phaser Box2D.
Need support? Join our Box2D channel in the Phaser Discord.
50+ Free Box2D Source Code Examples
We've put together a comprehensive package of over 50 Phaser Box2D examples. These showcase all kinds of features, from complete mini-games to Phaser sprite integration. You can download these for free at https://phaser.io/account/downloads#box2d (requires free Phaser account)
Why another Box2D port?
There are plenty of ports of Box2D to JavaScript, so why did we create another one? The main reason is because on August 12th 2024 Box2D version 3.0 was released. This is was a brand-new version and is the one that we converted. The others available are ports based on the old version 2.
Version 3 introduces several significant improvements that justify the upgrade. The new Soft Step Solver demonstrates markedly increased stability compared to version 2 — an improvement acknowledged by the Box2D author himself. Additional enhancements include superior precision, extensively redesigned joint mechanics, and the introduction of capsule-based physics bodies. Perhaps most significantly, the speculative collision system has been completely reconstructed, eliminating polygon gaps, minimizing performance impact, and adding Bullet physics support. The Continuous Collision Detection (CCD) implementation in version 3 is particularly refined. The lack of these substantial improvements in v2 warranted the conversion.
It was important to us that we kept our conversion of Box2D within JavaScript. Modern browser VMs are so incredibly powerful you can easily have thousands of bodies flying around your world. It also allows you to easily integrate it into any front-end bundler you like, takes up less than 70KB (min+gz), will run client-side or server-side and is easy for developers to contribute back to the source.
Does this only work with Phaser?
No. This library is entirely standalone and runs self-contained on the client or server. We include helper functions for integration with the Phaser game framework and lots of examples demonstrating use, they are optional.
Getting Started
There is an extensive Getting Started Guide.
You can find this in the getting-started folder. Please start here! There's lots of information and handy things to learn.
npm
You can download Phaser Box2D from npm:
npm i phaser-box2d
https://www.npmjs.com/package/phaser-box2d
How to run the API Docs
- If you haven't already, run
npm install.
- Then
npm run docs to launch the API Docs site server.
- Open your browser to the URL given (usually http://localhost:8000)
How to use the Vite Template
- Copy the
PhaserBox2D.js, PhaserBox2D-Debug.js and PhaserBox2D-Debug.js.map files into the vite-template/src folder.
- If you haven't already, run
npm install from within the vite-template folder. This will install both Vite and Phaser.
- Use the following npm commands:
npm run dev - to run Vite in dev/watcher mode and open your browser to the URL given.
npm run build - to make Vite create a build of the app in the dist folder.
npm run preview - to test the dist build. Open your browser to the URL given.
- Open the
src/main.js file to see Phaser and Box2D being used together in a simple example.
- By default, the code uses the Release version of Box2D. If you would like to use the Debug version so you can use the Debug Draw renderer then simply edit the
import statement in the main.js file to import PhaserBox2D-Debug.js instead of PhaserBox2D.js. Just remember to swap this back before doing a production build.
License
The Phaser Box2D library includes code that is:
Copyright 2023 Erin Catto, released under the MIT license.
Copyright 2025 Phaser Studio Inc, released under the MIT license.
The Getting Started Guide, Examples and API Documentation are Copyright 2025 Phaser Studio Inc and are not for commercial use or distribution.
Conversion Notes
This JavaScript version is a direct conversion of the C functions, so most of the documentation is the same.
The most common difference is that all C language typedef structs (e.g. b2Vec2) have been turned into JS classes.
In C they might use:
bodyDef.position = (b2Vec2){0.0f, 4.0f};
In JS we must use:
bodyDef.position = new b2Vec2(0.0, 4.0);
World
A box 2D world (b2World) is a scene in which all simulation takes place.
You can create many box 2D worlds and simulate them at different rates with separate contents. They cannot interact with each other.
// create a definition for a world using the default values
let worldDef = b2DefaultWorldDef();
// change some of the default values
worldDef.gravity = new b2Vec2(0, -10);
// create a world object and save the ID which will access it
let worldId = b2CreateWorld(worldDef);
Bodies
A world contains bodies, which are simulated 2D objects.
The code is almost identical to the world creation.
// create a definition for a body using the default values
let bodyDef = b2DefaultBodyDef();
// change some of the default values
bodyDef.type = b2BodyType.b2_dynamicBody;
bodyDef.position = new b2Vec2(0, 4);
// create the body
let bodyId = b2CreateBody(worldId, bodyDef);
Shapes
Bodies require shapes which carry information about the materials.
The code should be familiar!
// create a definition for a shape using the default values
let shapeDef = b2DefaultShapeDef();
// change some of the default values
shapeDef.density = 1.0;
shapeDef.friction = 0.3;
Polygons
We cannot create the shape yet though, we need an outline first.
// create a polygon shape using a helper function (NOTE: the parameters are half-width and half-height)
let box = b2MakeBox(1.0, 1.0);
// now we can create the shape, for the body, with the polygon
b2CreatePolygonShape(bodyId, shapeDef, box);
We have created a 2 x 2 box, made of material with density 1.0 and friction 0.3. It is at position 0, 4.
The polygon, circle, capsule, segment, or line creation will automatically add the body to the world.
Our world has a gravity acceleration of 10m/s/s downwards (close to the Earth's 9.8m/s/s), so this box will fall accordingly.
Wrappers
For the JS conversion we have supplied some wrapper functions to help with common tasks.
The code above becomes:
let world = CreateWorld({ worldDef: b2DefaultWorldDef() });
let box = CreateBoxPolygon({
worldId: world.worldId,
bodyDef: b2DefaultBodyDef(),
type: b2BodyType.b2_dynamicBody,
position: new b2Vec2(0, 4),
size: 1,
density: 1.0,
friction: 0.3
});
Each wrapper has an associated configuration object, which should integrate with your editor to provide auto-complete assistance.
If the auto-complete is not working, you can find all of these block headers in the file src/physics.js along with the code for the wrapper functions.
/**
* @typedef {Object} BoxPolygonConfig
* @property {b2WorldId} worldId - ID for the world in which to create the box.
* @property {b2BodyDef} [bodyDef] - Body definition for the box.
* @property {number} [type] - Type of the body (static, dynamic, kinematic).
* @property {b2Vec2} [position] - Position of the box's center.
* @property {b2BodyId} [bodyId] - Existing body ID if adding as a fixture.
* @property {b2ShapeDef} [shapeDef] - Shape definition for the box.
* @property {number} [density] - Density of the box.
* @property {number} [friction] - Friction of the box.
* @property {any} [color] - Custom color for the box.
* @property {b2Vec2|number} size - Size of the box (either a b2Vec2 or a single number for square).
*/
Static Objects
A static body is immobile in the world. It will be collided with according to the same rules as other bodies, but it will not move in reaction to any force.
Static bodies are often used for the ground, platforms, walls, etc.
To create a static object we follow the same procedure but specify b2BodyType.b2_staticBody instead of b2BodyType.b2_dynamicBody for the 'type' parameter.
Running the Simulation
It is necessary to call b2WorldStep frequently to perform all of the calculations and advance the simulation by one time step.
Box2D is most stable when it is called with a fixed time step (one which does not vary depending on the speed of your machine, or the amount of work which it is doing).
There is some common code we use to make sure that the fixed time step and the real time elapsed are approximately in sync. We have wrapped this code in the function WorldStep().
Notes: The fixedTimeStep defaults to 60fps (1/60), and subStepCount defaults to 4 if they are not specified. These are good values for most uses.
// step the physics simulation to advance it; Here we're using a fixed step of 30fps with two sub-steps per full step
const stepTime = WorldStep({ worldId: worldId, deltaTime: deltaTime, fixedTimeStep: 1/30, subStepCount: 2 });
The End of the World
As the official Box2D documentation says: "When you are done with the simulation, you should destroy the world".
b2DestroyWorld(worldId);
The Whole Wrapper
All functions and type comments are in the file physics.js.
Each function is exported and can be imported with (e.g.):
import { CreateWorld } from 'physics.js';