Game physics for browser games. Fixed timestep, semi-implicit Euler integration, gravity, velocity and acceleration, friction and drag that don't depend on frame rate, momentum and mass, impulses and knockback, bounce (restitution), collision response (separate and slide, resolve per axis), slopes, one-way platforms, springs, ropes with Verlet, avoiding tunneling, and when to use a physics engi...
Download skill (.zip) Files on GitHubGame physics
Most games need arcade physics (you control it), not realistic physics. Use an engine only for stacking, rolling,
ragdolls and joints. Drop-in helpers: assets/physics.js (tested). Collision detection is in collision-detection.
The basics
// fixed step dt = 1/60 (game-scaffolding loop). Semi-implicit Euler: velocity first, then position.
body.vx += body.ax * dt; body.vy += (body.ay + GRAVITY) * dt;
body.vx *= Math.exp(-body.drag * dt); // frame-rate independent drag (not *= 0.98)
body.x += body.vx * dt; body.y += body.vy * dt;
- Always a fixed step for physics. Variable dt makes jumps different heights at different frame rates.
- Units: pixels (or meters) per second. Pick a scale once (e.g. 32 px = 1 m) and keep it.
- Clamp fall speed (terminal velocity) and cap dt (0.25 s max per frame) so a tab switch doesn’t explode things.
Recipes (in physics.js)
| Need | Function |
|---|---|
| Approach a target speed (accel / friction) | approach(v, target, rate * dt) |
| Smooth follow, frame-rate independent | damp(a, b, sharpness, dt) |
| Impulse / knockback scaled by mass | applyImpulse(body, ix, iy) → v += i / mass |
| Move with tiles, sliding along walls | moveAndSlide(body, dx, dy, isSolid, tile) resolves X then Y, sets onGround, onWall |
| Bounce two circles with mass and restitution | resolveCircles(a, b, restitution) |
| Spring toward a point | spring(body, tx, ty, k, damping, dt) |
| Rope / cloth / soft bodies | verletStep(points, dt, gravity) + satisfyConstraints(points, sticks, iterations) |
Collision response
- Detect overlap (collision-detection).
- Separate along the smallest penetration axis (or per axis for tile games: move X, fix X, then move Y, fix Y).
- Respond: cancel velocity into the surface (slide), or reflect it × restitution (bounce):
v' = v - (1 + e)(v·n)n. - Friction on contact: reduce the tangential velocity.
One-way platforms: collide only if falling (vy > 0) and the body’s bottom was above the platform top last step.
Drop through: ignore that platform for 0.2 s while pressing down + jump.
Slopes: for tile games, store a height function per slope tile and snap the body’s bottom to it when walking; keep
onGround while going downhill (snap down up to ~4 px).
Tunneling (going through walls)
Fast objects skip thin walls. Fix with (in order of simplicity): cap speed below the tile size per step, substeps
(n = ceil(speed * dt / (size / 2))), swept AABB or raycasts for bullets (collision-detection), or thicker colliders.
Engines
| Engine | For |
|---|---|
| Matter.js | 2D, easy, stacking and joints; fine for a few hundred bodies |
| Planck.js (Box2D port) | 2D, more stable, better for many contacts |
| Rapier (2D/3D, WASM) | Fast, deterministic; used by Bash’s game-kit-3d |
| Phaser Arcade / Matter | If already in Phaser |
With an engine: step it at a fixed rate, keep game rules outside callbacks (queue events, apply after the step), and use simple shapes.
Feel checklist
- Same jump height at 30, 60 and 144 fps (lock the step and test with a throttled frame rate)
- No jitter on the ground (don’t apply gravity into the floor and then bounce; set
vy = 0on landing) - No sticking to walls when holding toward them in mid-air
- Fast objects never pass through walls