Hold Space / Up or hold the screen to thrust · A D / arrows or drag to steer · R new rocket · Esc exit

Premium · Level 3 Three.js Tutorial

Three.js Rapier Physics Tutorial:
Build a Rocket Lander

By Shane Brumback · · Advanced Three.js tutorial · about 45 minutes

Rigid bodies, real forces and collision events. Land a rocket on a moving pad with thrust, torque steering and wind, then watch it break into tumbling debris when you get it wrong.

SEE IT LIVE

The rocket flying behind this page is a Rapier rigid body on autopilot. Launch the full-screen demo to fly it yourself: thrust, steer against the wind and touch down softly on the moving pad.

Play the Game

Free with Google sign-in. Space, Up or hold the screen to thrust; A and D, arrows or drag to steer; Esc exits.

What Is Rapier Physics in Three.js? (Quick Definition)

Rapier is a physics engine compiled to WebAssembly. In a Three.js game, Rapier owns the motion: rigid bodies, colliders, forces and contacts. You step the Rapier world at a fixed rate, copy each body's translation() and rotation() onto its mesh, and read collision events to decide what happens, like a landing or a crash.

Introduction

Rocket Lander went live on Indie Forge Studio today with a small physics step written in plain JavaScript. In this tutorial I rebuild that rocket on Rapier: a dynamic body with two colliders, a kinematic pad that really carries it, thrust and steering as forces and torque, wind gusts, and collision events that judge every touchdown. A crash spawns a dozen physics blocks that bounce and settle.

Want the why before the how? The free article Three.js Rapier physics: landing a 3D rocket with real rigid bodies compares Rapier, cannon-es and hand-written physics.

What You'll Learn

  • How to load Rapier from a CDN with await RAPIER.init() and create a World
  • Fixed, kinematic and dynamic bodies, and when to use each
  • How to build one body from two colliders and lock it to a 2D plane
  • Thrust with addForce() and PD steering with addTorque()
  • Collision events with EventQueue and drainCollisionEvents()
  • Spawning and removing debris bodies, and a fixed-timestep loop

Prerequisites

  • Basic Three.js: scene, camera, renderer and meshes
  • Comfortable with ES modules and an importmap (no build step needed)
  • Helpful first: the free Ball vs. Wall collision events tutorial (cannon-es)

Frequently Asked Questions

How do I use Rapier physics with Three.js?

Load @dimforge/rapier3d-compat, await RAPIER.init(), create a World with gravity, and give every moving object a rigid body plus one or more colliders. Each frame, step the world at a fixed rate and copy body.translation() and body.rotation() onto the matching Three.js mesh.

Why use a fixed timestep with Rapier?

Physics behaves the same on a 60 Hz laptop and a 120 Hz phone only if every step covers the same time. The tutorial adds frame time to an accumulator and runs world.step() in 1/60 second slices, with a cap of five steps per frame so a slow frame cannot spiral.

How do I keep a Rapier body in one plane in a 3D scene?

On the RigidBodyDesc call enabledTranslations(true, true, false) and enabledRotations(false, false, true). The rocket can then move left, right, up and down and tip side to side, but never drifts toward or away from the camera.

What is a kinematic body in Rapier?

A kinematic body is moved by your code, not by forces. You call setNextKinematicTranslation() each step and Rapier works out its velocity, so dynamic bodies such as the rocket collide with it and ride on it properly. The moving landing pad is kinematic.

Is the code free?

The live demo is free with a Google sign-in. The step-by-step code breakdown, the complete file and the Launch button are for members.

How It Works

  1. Imports, Scene, Camera and Renderer — In plain words: we load three.js and Rapier from a CDN and set up a camera and a full-page canvas to draw on.
  2. Lights, Stars and the Moon Surface — In plain words: we light the scene, scatter stars and lay down a grey moon floor with a few craters so motion is easy to read.
  3. Start Rapier and Create the World — In plain words: we wait for Rapier's WebAssembly to load, make a physics world with low moon-like gravity, and add a solid floor collider.
  4. A Moving Landing Pad — In plain words: the pad is a kinematic body: our code moves it, and Rapier makes everything else respect it.
  5. The Rocket Body and Colliders — In plain words: the rocket is one dynamic body with a capsule for the hull and a flat box for the feet, locked so it only moves and tips in the screen's plane.
  6. Thrust and Steering as Forces — In plain words: every physics step we clear old forces, push the rocket along its nose when the engine is on, and twist it gently toward the angle the player wants.
  7. Wind Gusts — In plain words: every few seconds the wind picks a new strength and direction and eases toward it, pushing the rocket sideways.
  8. Input and Autopilot — In plain words: keyboard, mouse or touch fly the rocket; when nobody touches anything for eight seconds an autopilot takes over and lands it.
  9. Collision Events Decide Land or Crash — In plain words: Rapier tells us the moment the rocket touches something, and we check speed, drift and tilt to decide if that was a landing.
  10. Landing, Crash Debris and Respawn — In plain words: a good landing scores and refuels; a crash hides the rocket and throws a dozen small physics blocks around, then a new rocket drops in.
  11. Fixed-Timestep Loop, Camera and HUD — In plain words: physics runs at a steady 60 steps per second whatever the screen's frame rate, then we copy positions onto the meshes, move the camera and update the readout.

That's the shape of the build. The detailed code breakdown for every step and the full copy-paste file are below. Members unlock them.

How To Use It

  • Fly it badly on purpose. Come in fast or tipped and watch the collision event turn the rocket into debris.
  • Tune the feel. Change gravity, thrust and the steering torque numbers to make it floaty or twitchy.
  • Reuse the pattern. Body plus colliders, forces each step, events after the step: the same loop works for cars, balls or falling crates.

READ THE FREE ARTICLE

The article explains what Rapier adds over hand-written physics, how it compares with cannon-es, and how it ties to Super Soldier Battle. It's free to read.

Low-poly white and cyan rocket firing its engine above a glowing landing pad on a dark moon surface, rendered in Three.js Read the Article

Play It and Keep Learning

Play Rocket Lander free in your browser, or play Super Soldier Battle, my Three.js shooter. Related: walls that stop bullets with BVH raycasts and the rocket engine particle system.

Need Three.js physics or game work? I take freelance and contract work: contact me.

Play the Game

Three.js Physics & Game Tutorials

More collision, physics and game-building tutorials and articles for Three.js.

Rocket Lander: Free Three.js 3D Rocket Landing Game
ArticleArcade

Rocket Lander: Free Three.js 3D Rocket Landing Game

Rocket Lander is a free Three.js 3D browser game. Hold to fire the thruster, drag to steer and land a low-poly rocket softly on a moving pad. Built with WebGL.

Ball vs. Wall: 3D Collision Events in Three.js
TutorialPhysics

Ball vs. Wall: 3D Collision Events in Three.js

Free Level 1 Three.js tutorial: a ball rolls and flies into a wall of cubes with cannon-es physics, cubes flash new colors on every collision event, then the wall rebuilds and it happens again. Live demo and copy-paste code.

Walls That Stop Bullets in Three.js — Box Colliders & BVH Raycasts
TutorialFPS

Walls That Stop Bullets in Three.js — Box Colliders & BVH Raycasts

Learn how Super Soldier Battle makes walls block bullets and players in Three.js: invisible box colliders, three-mesh-bvh raycasts, and push-out collision.

Three.js Rocket Engine Particle System
TutorialParticles

Three.js Rocket Engine Particle System

Free Three.js tutorial: build a rocket engine particle system with a shader flame cone, glowing exhaust sparks, smoke, heat haze, and a ground dust ring — the same plume used on the Super Soldier Battle gunship. Live demo, step-by-step code, copy-paste source.

Balloon Guard: Free Three.js 3D Shield Physics Game
ArticleArcade

Balloon Guard: Free Three.js 3D Shield Physics Game

Balloon Guard is a free Three.js 3D browser game. Drag a glowing shield to knock falling blocks and spikes away so your balloon keeps rising. Built with WebGL.

Many Fires, Two Draw Calls — GPU Flames, Embers & Smoke in Three.js
TutorialParticles

Many Fires, Two Draw Calls — GPU Flames, Embers & Smoke in Three.js

Learn how Super Soldier Battle draws all its ruin fires in two draw calls: vertex-shader particles, distance fade, frustum culling and no PointLights.