What it does
body: {} on a spawn makes an object a body: a box the size of its sprite by default, or a circle, an upright capsule,
a polygon (concave outlines are triangulated) or a compound of shapes. Each fixed step the solver moves the bodies and
writes x, y and rot back, so the object draws where the physics put it and tag collision rules still see it.
Velocity is a property: you set hero.body.vx to run and call impulse to jump. Gravity is on by default, 900 units
per second squared downwards; set it to zero for space.
The solver is substepped, four times a fixed step by default, so stacks settle without jittering and joints do not
stretch. Bodies that stay under 3 units a second for half a second go to sleep until something touches them. A body
knows whether it is grounded, which sides are blocked, and what it is touching, with the sustained load in
multiples of its own weight, so a platformer's jump check and a puzzle's "is this being crushed" are both one line.
Joints take world points and hand back a live joint rather than an id. Its length, min, max, hertz and
damping are read by the solver every step, so a grapple that reels in, a suspension that stiffens and a winch that
pays out are one number changing each frame, with no teardown and no visible jolt. Effectors add fields: wind over an
area, a gravity well that pulls or repels, and buoyant water whose density is measured against each body's own, so one
pool floats some things high, some low and sinks the rest.
Features
- Shapes — boxes, circles, upright capsules for characters, convex and concave polygons, and compounds.
- Body types — dynamic, static, and kinematic bodies you move yourself, which carry whatever stands on them.
- Platformer surfaces — one-way platforms in any of four directions, and conveyors whose
surfaceSpeedcan change while things ride them. - Six joints —
pin(a hinge with limits and a motor),rope(a rod, a rope with slack or a spring),slider,wheel(sprung suspension in one joint),weldandgrab(the mouse joint). - Contacts you can read —
grounded,blockedper side,touchingwith impulse, approach speed and load, and aphysics.contactsignal for thuds and damage. - Effectors — area forces such as wind, point forces that attract or repel, and buoyancy with flow and drag.
- Chain terrain —
physics.chain(points)turns a polyline into static ground with no notches at the joins. - Filters, sensors and bullets — 32-bit categories and masks, sensors that report and never push, and
bulletbodies that are swept so they do not pass through thin walls. - Arcade movement — per-body thrust, drag towards zero and speed caps, for ships and top-down movers.
- World bounds and culling — walls or bouncing edges round the world, and a cull rect that removes bodies leaving it and reports where they went.
- Raycasts — the nearest body along a ray, with the hit point and normal.
Good for
- Platformers — capsule characters that glide over seams, one-way ledges, moving lifts and conveyors.
- Physics puzzles — stacks, seesaws, hinged doors and things to crush or topple, with load readings to judge them.
- Bat-and-ball and pinball games — bouncing balls, world edges that bounce on three sides, and hinged flippers driven by motors.
- Ragdolls and swinging — hinges with limits, ropes with slack and grapples that reel.
- Space and top-down games — gravity off, thrust and drag on each body, and gravity wells.
- Water levels — buoyant pools with a current, where heavy things sink and light ones float.
Make it yours
The pack options are gravity ({ x: 0, y: 900 } by default), substeps (4, or 6 to 8 for taller stacks) and
cull, a rect round the level beyond which bodies are removed. Gravity can change at any time with
game.physics.gravity, and most body fields are live: velocity, thrust, drag, speed caps, restitution, friction,
gravity scale and a conveyor's surface speed.
Two different uses: a platformer with gravity down, a capsule hero with fixedRotation, one-way ledges, a kinematic
lift, and collision categories that let enemies overlap the hero for damage rules while both land on the walls; and a
top-down space game with gravity at zero, ships with drag and speed caps, and point effectors pulling them towards
planets.
A quick look
Ground, a crate, a bouncing ball, a concave rock and a capsule hero who runs and jumps:
game.spawn({ id: 'ground', x: 320, y: 350, width: 640, height: 20, body: { type: 'static' } });
game.spawn({ id: 'crate', x, y, width: 32, height: 32, body: {} }); // a box the sprite's size
game.spawn({ id: 'ball', x, y, width: 20, height: 20, body: { shape: 'circle', restitution: 0.6 } });
game.spawn({ id: 'rock', x, y, body: { shape: { kind: 'polygon', points: [[-30,-30],[0,-30],[0,0],[30,0],[30,30],[-30,30]] } } });
game.spawn({ id: 'hero', x, y, width: 24, height: 32, body: { shape: 'capsule', fixedRotation: true, friction: 0.2 } });
hero.body.vx = dir * 160; // velocity is a property
if (hero.body.grounded && jump) hero.body.impulse(0, -hero.body.mass * 420);
Works with
collision— required: overlap rules by tag work on bodies, and they run after the physics.tiles-2d— a tilemap's solid tiles become merged static bodies, with slopes and one-way tiles.grid-move— drives body velocity for four-way lane movement, so walls stay solid.feelandaudio— readphysics.contactfor impacts and thuds.physics-rope— ropes with weight whose ends can be tied to these bodies.physics-box2d— when you want Box2D's own behaviour instead; compose one or the other.
Good to know
- Writing
x,yorroton a body is a teleport, adopted next step. Never usecollide.solid()on bodies: the solver owns their separation. - It is tuned for boxes of 10 to 200 units and speeds under about 3000 units a second; bigger or faster needs
bulletor more substeps. force()lasts one 60 Hz frame; for sustained thrust useaxanday.- Contacts beyond 512 a frame are dropped from the signal, though
touchingstill sees them all. - For Box2D's exact stacking and continuous collision use
physics-box2d, and for 3D,physics-3d.






