WASD / arrows drive · Space drift · R flips the car back · drag to orbit · Esc exits
↻Rotate your device to landscape to drive.

Three.js Vehicle Physics:
Build a GTA-Style Car You Can Drive

By Shane Brumback · · Three.js tutorial

Premium three js vehicle physics tutorial: build a GTA style car with the Rapier vehicle controller, suspension, wheel friction and drifting in a small downtown city. Live demo.

Three.js Rapier Vehicle Physics Game Development
See it live

LIVE DEMO

The muscle car behind this page is the finished file running live. Launch the full-screen demo to drive it through the downtown streets, drift it with the handbrake and knock the barrels and cones around.

Free: sign in free with Google. WASD or arrows drive, Space drifts, R flips the car back, drag or one finger orbits; on phones turn to landscape and use the twin-stick controls. Esc exits.

What Is Three JS Vehicle Physics? (Quick Definition)

Three js vehicle physics means letting a physics engine move your car while Three.js draws it. With Rapier's vehicle controller, the chassis is one dynamic rigid body and each wheel is a raycast with a spring, damping and tyre friction. You set engine force, brakes and steering per wheel, step the world, and copy the chassis pose and wheel positions into your meshes.

Introduction

In this tutorial we build a GTA style car you can actually drive: a 1971 muscle car with real weight, soft suspension that leans and dives, rear-wheel drive and a handbrake drift, in a small downtown with buildings, sidewalks, barrels, cones and jersey barriers. If you want the why before the how, read the free article Three.js Car Physics: Why GTA-Style Driving Feels So Good first. It is the same car and the same numbers.

It is also a practical lesson in using real downloaded models in a threejs car game: splitting wheels that came joined, placing a building pack on city blocks, and turning a road mesh into physics with invisible walls generated by code instead of by hand.

What You'll Learn

  • How the Rapier vehicle controller works: raycast wheels, suspension rest length, stiffness, damping and friction slip
  • How to give a car weight: mass, a low ballast collider and springs placed so the wheels land where the model drew them
  • Rear-wheel drive, braking into reverse, speed-sensitive steering and a handbrake drift
  • How to split one four-wheel mesh into four wheels that steer and roll
  • How to scan a road mesh with rays to build ground, invisible walls and street props automatically
  • A chase camera with drag and touch orbit, plus the site-wide twin-stick controls for phones

Prerequisites

Facts

  • Libraries: three.js r160 and @dimforge/rapier3d-compat 0.14.0, both from a CDN. No build step.
  • The car weighs about 1,150 kg, is rear-wheel drive with 3,000 N per rear wheel, and reaches about 82 km/h after 5 seconds on flat ground.
  • Suspension: 0.3 m rest length, stiffness 30 per kg, about 8 cm of sag at rest, 0.25 m of travel.
  • Grip: front 2.6, rear 3.2, rear 1.3 with the handbrake on. Steering lock goes from 0.6 to 0.18 radians as speed rises.
  • The map is scanned with a ray every 2 m (about 78,000 rays) and closed in with about 800 merged invisible wall boxes.
  • The three models download as about 7.4 MB of meshopt-compressed GLB.

Rapier Vehicle Controller vs Wheel Bodies

You can build a car from four cylinder bodies and joints, but they catch on the triangle edges of a road mesh and wobble at speed. The rapier vehicle controller is a raycast vehicle: each wheel is a ray with a spring and a tyre model. It is cheaper, steadier on bumpy city geometry and much easier to tune, which is why most arcade and GTA-like driving uses this approach.

Three JS Car Physics That Feels Heavy

Weight is mostly suspension. Rapier multiplies spring force by the chassis mass, so a stiffness of 30 with four wheels sags 9.81 / (4 x 30), about 8 cm. The code uses that number to hang each spring so the wheels sit exactly where the model drew them, then damps the springs a little under critical so the car dives, squats and rolls and then settles.

How It Works

  1. Imports, settings and renderer. Load three.js, OrbitControls, GLTFLoader, Sky, the meshopt decoder and Rapier, and set every driving number in one place.
  2. Sunset sky, sun and fog. Add the Sky shader, a low warm sun with soft shadows, a hemisphere fill light and warm fog.
  3. Physics world, the roads and the downtown buildings. Start Rapier, load the three GLB models, turn the road meshes into one trimesh collider and move each building onto a city block.
  4. Scan the streets: ground fill and invisible walls. Cast a ray down every 2 m to map the roads, flood-fill ground heights, and turn the empty cells that touch the road into invisible wall boxes.
  5. Street props: barrels, cones, jersey barriers and rubble. Scatter knockable barrels and cones, fixed jersey barriers and decorative rubble along the sidewalk edges and across road exits.
  6. The car: model, four separate wheels, and the chassis body. Split the single four-wheel mesh into four wheel meshes, put each in a steering pivot, and give the car a dynamic body with a low ballast collider.
  7. Raycast vehicle: suspension, tyre grip and steering. Create the Rapier vehicle controller and add four wheels with spring, damping, travel and friction settings.
  8. Driving input: keyboard and the mobile twin-stick. Map WASD, the arrows, Space and R, and wire the site-wide twin-stick controls for phones.
  9. Engine, brakes, steering and the handbrake drift. Each physics step, turn the input into engine force, brakes, speed-sensitive steering and the handbrake drift settings for every wheel.
  10. HUD: speedometer and drift meter. Show km/h, mph, drive or reverse, and a drift readout based on how fast the car is moving sideways.
  11. Camera: auto-orbit behind the page, chase cam and drag orbit in the demo. Slowly orbit the parked car behind the page text, then in the demo follow the car and ease back behind it after you drag to orbit.
  12. Main loop, resize and Live Demo hooks. Step the physics at a fixed 60 Hz, copy the chassis, wheels and props into their meshes, move the sun and camera, and expose enter and exit for the Live Demo button.

The detailed code breakdown and the full copy-paste file are below. Members unlock them. Every step is an exact excerpt of the full file, and the full file is the same code as the live demo.

How To Use It

  • Start with ENGINE_FORCE, FRONT_GRIP and REAR_GRIP. Drop the rear grip below the front and the car starts to oversteer under power; raise it and the car pushes wide.
  • Change SUSP_STIFF to 15 for a floaty old sedan or 60 for a stiff sports car. The wheels stay in place because the spring position is worked out from SUSP_SAG.
  • Make the drift easier or harder with DRIFT_GRIP and DRIFT_SIDE.
  • To use your own car, point its nose down +X, make sure the wheel mesh name contains "Wheel", and the split code finds the four corners for you.

Frequently Asked Questions

How do I add vehicle physics in Three.js?

Use Three.js to draw the car and a physics engine to move it. With Rapier you create a dynamic rigid body for the chassis, call world.createVehicleController(chassis), add four raycast wheels with suspension and friction settings, and every frame set engine force, brakes and steering, call updateVehicle and copy the results into your meshes.

Is the full source free?

No. This is a premium Level 3 tutorial. The live demo is free: sign in free with Google to launch it. Members unlock the step-by-step code, the full file and the Launch button.

Can I use the Rapier vehicle controller with my own car model?

Yes. Point the car's nose down +X (or change the forward axis), measure each wheel's centre and radius, and split the wheels into their own meshes if the model joins them. The tutorial does that split in code by sorting each triangle into a corner.

Why does the car drift when I hold Space?

Space is the handbrake. It brakes the rear wheels and lowers their grip and sideways stiffness, so the rear slides out. Steer into the slide and let go to get the grip back.

Does the demo work on phones?

Yes. Turn your phone to landscape: the left stick is gas and brake, the right stick steers the wheels, DRIFT and FLIP buttons sit on the right, and the camera stays behind the car. The city models are about 7 MB, so the first load takes a few seconds on mobile data.

Related Tutorials and Articles

READ THE FREE ARTICLE

Want the why before the how? Read Three.js Car Physics: Why GTA-Style Driving Feels So Good, then go drift in Drift Dash.

3D Model Credits