Flying, the FPV kind

Want your game to fly like a drone with goggles on? This page shows exactly how J'aime Voler does it, with the real code from the game: a flight model small enough to drop into your own game, two ways to fly it, and the whole source free to take. Start in Level mode. Everyone does.

Try it: J'aime Voler

One island, thirteen glowing gates, and a ghost of your best lap to chase. Click inside the game, then fly with WASD (throttle and yaw) and the arrow keys (pitch and roll). Space boosts, Shift is precision. This exact game is the code this whole guide is about.

J'aime Voler cover art
▶
Load the live game

Open the game full screenRead the full sourceDownload the game

Two ways to fly: start with Level

START HERE

Level mode

The sticks tilt, the craft follows. Push a stick and the craft banks to a set angle and turns. Let go and it levels itself and holds its altitude. You steer where it points; the model handles the rest. Forgiving, calm, and the right first flight model for almost every game and almost every player.

ADVANCED

Acro mode

The sticks spin. Hold a stick and the craft keeps rotating at a set rate, flips and rolls included. Center the stick and it holds whatever angle it is at, even upside down. This is how real FPV drones fly, and it is a handful until it clicks. Offer it as the expert option: in Voler one key (X) swaps modes, so pilots graduate when they are ready.

Both modes share everything else: the same throttle, the same thrust, the same camera. In the code it is one settings flag, and the physics function branches exactly once. That is the whole trick of this guide: build Level first, then add Acro as a second branch.

How the flight model works

  1. One state, four inputs. The craft is a position, a velocity and an orientation. The pilot is four numbers from -1 to 1: throttle, yaw, pitch, roll, plus boost and precision switches. Everything else is math on those, run once per frame.
  1. Level mode turns sticks into angles. The stick picks a target bank and pitch (38 degrees at most on the Chill profile), and the craft eases toward it. Release the stick and the target is flat and level, so the craft settles on its own. Yaw just turns the heading at a capped rate.
// Level mode, from the game's physics(): sticks become angles S.heading -= degToRad(actualRate(input.yaw, P.yaw * 0.5, P.yaw, 0.3)) * rr * dt; const maxA = degToRad(P.angle) * (input.prec ? 0.6 : 1); _e.set(-input.pitch * maxA, S.heading, -input.roll * maxA, 'YXZ'); _q.setFromEuler(_e); S.q.slerp(_q, 1 - Math.exp(-dt * 9)); // ease toward the target tilt S.w.set(0, 0, 0);
  1. Acro mode turns sticks into spin rates. The same sticks now command degrees per second around each axis. The curve that shapes them is one small function: gentle near the center of the stick, fierce at the edges. Three profiles (Chill, Sport, Insane) retune the whole craft without touching code.
// the rates profiles: one table tunes the whole feel const PROFILES = [ { name: 'Chill', center: 140, max: 420, expo: 0.35, twr: 3.2, angle: 38, yaw: 160 }, { name: 'Sport', center: 200, max: 670, expo: 0.54, twr: 4.3, angle: 55, yaw: 260 }, { name: 'Insane', center: 260, max: 1000, expo: 0.62, twr: 5.8, angle: 70, yaw: 380 } ]; function actualRate(x, center, max, expo) { const a = Math.abs(x); const expoed = x * (a * a * a * expo + a * (1 - expo)); return expoed * Math.max(0, max - center) + center * x; // deg/s } // Acro mode: sticks become spin, integrated into the orientation const tx = -degToRad(actualRate(input.pitch, P.center, P.max, P.expo)) * rr; const ty = -degToRad(actualRate(input.yaw, P.center * 0.8, P.max * 0.6, P.expo)) * rr; const tz = -degToRad(actualRate(input.roll, P.center, P.max, P.expo)) * rr; S.w.x += (tx - S.w.x) * k; S.w.y += (ty - S.w.y) * k; S.w.z += (tz - S.w.z) * k; const ang = S.w.length() * dt; if (ang > 0) { _q.setFromAxisAngle(_v.copy(S.w).normalize(), ang); S.q.multiply(_q).normalize(); }
  1. Thrust points where the nose points, and hover is a fraction. Throttle maps onto the thrust-to-weight ratio, so a centered stick is exactly hover in both modes. Push up and the craft accelerates along its own up axis, which is why banking turns you. In Level mode the thrust is corrected for tilt, so turns hold their altitude instead of sliding down.
// throttle and thrust, shared by both modes const hov = 1 / P.twr, t = input.thr; let frac = t >= 0 ? hov + (1 - hov) * t : hov * (1 + t); let acc = frac * P.twr * G; if (settings.mode === 'level') acc = Math.min(acc / Math.max(up.y, 0.5), P.twr * G); // hold altitude through turns if (input.boost) acc += P.twr * G * 0.5; S.vel.addScaledVector(up, acc * dt); S.vel.y -= G * dt; // gravity always applies
  1. The camera is the craft, tilted up. FPV view puts the camera right at the craft and pitches it up by the uptilt (25 degrees in Voler). That tilt is the secret: at speed the craft pitches down to move, so a level camera would stare at the ground. The chase view is the same craft from behind, smoothed. Either way, the field of view widens a little as speed builds, which sells the speed for free.
// FPV camera: at the craft, pre-tilted up tiltQ.setFromAxisAngle(_v.set(1, 0, 0), degToRad(settings.tilt)); camera.position.copy(S.pos).add(_v.set(0, 0.03, -0.1).applyQuaternion(S.q)); camera.quaternion.copy(S.q).multiply(tiltQ);

The rules that keep it friendly

Grab the code

J'aime Voler, the whole game

One HTML file, about 82 KB: the flight model, both modes, the island, the gates, ghost laps and the scoreboard hook. The friendliest place to start is the real thing.

Read itDownload

The flight model, extracted

Just the flying: state, inputs, both modes, thrust, and the camera, pulled out of the game into one commented file (fly-core.js) that drops into any three.js project. This is the file to hand a builder who already has a game.

Read itDownload

How multiplayer works

Flying is better with company. The sister guide shows how our games do pop-in multiplayer, with the full code from E2 Pong and E2 World 2D.

Read the multiplayer guide

Build yours

  1. Drop fly-core.js into your three.js project, or start from the whole Voler file.
  2. Feed it inputs: WASD and the arrow keys are the friendly default.
  3. Start in Level mode on the Chill profile, with a ground height function for your world.
  4. Put the camera at the craft with about 25 degrees of uptilt.
  5. Add Acro as the second branch when Level feels solid, and upload your game here.

Upload your gamePlay J'aime VolerHow multiplayer works

Use this code in your own games however you like. A shout-out is nice, never required.