Three.js Flight Simulator (Arcade Flight Model)

Fly a light aircraft over procedural mountains in Three.js with a simple 6-DoF flight model of thrust, lift from angle of attack, drag, stall and gravity, a chase camera, a ring course down a river valley and an autopilot.

Key topics: three.js flight simulator, flight physics, airplane game, angle of attack, chase camera, webgl game

What this code does

- Flight Model: the nose rotates by control rates scaled by airspeed, then thrust, lift (∝ v² × angle of attack), drag and gravity are integrated each sub-step.
- Stall: above about 20° angle of attack lift collapses, so pull too hard at low speed and the nose drops.
- Weathervaning: the velocity slowly aligns with the nose, standing in for the stabilising effect of the tail.
- Autopilot: banks into turns toward the next ring, adding back-pressure to hold altitude in the bank.
- World: an fbm + ridge-noise heightmap with a carved river valley, vertex-coloured by altitude and hidden in distance fog.
- Controls: W/S pitch, A/D roll, Q/E rudder, Shift/Ctrl throttle; the GUI toggles autopilot, wing levelling, fog, ring count and restart.

Continue Learning

JavaScript Implementation

ES6+
// Arcade flight model: rotate by control rates, then integrate thrust + lift + drag + gravity
const fwd = new THREE.Vector3(0, 0, -1).applyQuaternion(q), up = new THREE.Vector3(0, 1, 0).applyQuaternion(q)
q.multiply(new THREE.Quaternion().setFromEuler(new THREE.Euler(-pitch * dt, yaw * 0.4 * dt, roll * 2 * dt, 'YXZ')))
const v = vel.length(), aoa = Math.asin(-vel.clone().normalize().dot(up))
const lift = up.multiplyScalar(v * v * (0.0042 + aoa * 0.04))      // more speed or angle → more lift
const drag = vel.clone().multiplyScalar(-v * 0.0042)
vel.add(fwd.clone().multiplyScalar(throttle * 26).add(lift).add(drag).add(new THREE.Vector3(0, -9.8, 0)).multiplyScalar(dt))
pos.addScaledVector(vel, dt)
camera.position.lerp(new THREE.Vector3(0, 6, 26).applyQuaternion(q).add(pos), 0.1)  // chase cam