Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Orbital Flight Simulator
A complete 2D orbital flight simulator in plain HTML/CSS/JavaScript — no external libraries, no build step. Newtonian gravity, RK4 integration, trajectory prediction, and a space station to rendezvous with and dock to.
Run
Open index.html in a modern browser, or serve the folder:
cd orbital-flight-simulator
python3 -m http.server 8000
# then open http://localhost:8000
(Opening the file directly also works; there are no fetch/module imports.)
Play
Pick a scenario from the menu:
- Surface Launch — liftoff from the planet; establish a bound orbit, then dock.
- Low Orbit — you're already orbiting; rendezvous with the station.
- Station Rendezvous — elliptical orbit below the station; match it and dock.
Controls
| Key | Action |
|---|---|
W / ↑ / Space |
Thrust (hold) |
A / ← |
Rotate left |
D / → |
Rotate right |
1–4 |
Time warp 1x / 2x / 5x / 10x |
T |
Toggle predicted path |
P / Esc |
Pause |
R |
Restart scenario |
H |
Tutorial |
+ / - / wheel |
Zoom |
Touch controls appear automatically on coarse-pointer devices.
How to fly
- The cyan arrow is your velocity (prograde). Burn along it to raise your orbit, burn opposite (retrograde) to lower it.
- The dashed line is your predicted coast path, computed with the exact same integrator as the simulation: green = bound orbit, orange = escape, red = crash.
- The HUD shows altitude, speed, fuel, and orbital elements (periapsis, apoapsis, eccentricity, period).
- Time acceleration auto-limits to 1x near the surface and near the station so you can dock precisely.
- Docking: get inside the station's blue ring with low relative velocity.
- Landing (launch scenario): slow vertical speed, low sideways speed, belly toward the surface.
Architecture
index.html page shell, HUD, overlays, script tags
css/style.css dark space theme, HUD/overlay/touch styling
js/physics.js pure physics core (no DOM) — see below
js/render.js canvas renderer (stars, planet, ship, station, path)
js/controls.js keyboard + touch input -> { thrust, torque }
js/ui.js DOM HUD + overlays (menu, tutorial, pause, end)
js/game.js fixed-timestep loop, camera, time warp, scenarios,
docking/landing/crash, exhaust particles
js/main.js boot(): wires everything, starts the RAF loop
tests/test.js physics unit tests (plain Node)
tests/smoke.js headless DOM-stub smoke test (plain Node)
Physics (js/physics.js)
- Point-mass Newtonian gravity:
a = -mu * r_vec / |r|^3,mu = 3e8, planet radius1000(meters, seconds). - RK4 integration on
(pos, vel)with a fixed timestep of1/120 s. The ship angle (thrust direction) is held constant within a substep. - Rotation is integrated with semi-implicit Euler (decoupled from orbital motion), with a clamped angular velocity and damping.
- The station coasts on the same RK4 integrator (gravity only).
- Orbital elements (specific energy, eccentricity vector, periapsis, apoapsis, period) are derived from the state vector each frame.
- Trajectory prediction re-runs the identical RK4 integrator in coast mode, so the predicted path always matches what actually happens.
Frame-rate independence
The game loop is a fixed-timestep accumulator: each animation frame advances
the simulation in whole 1/120 s steps (scaled by the time-warp factor), so
physics is identical regardless of display refresh rate. Verified by tests.
Tests
No dependencies — plain Node:
node tests/test.js # physics unit tests
node tests/smoke.js # headless boot + interaction smoke test
tests/test.js covers gravity magnitude/direction, energy conservation over
10 orbits, frame-rate independence (identical chunking and substep
tolerance), fuel consumption, surface collision (crash vs. landing vs.
misaligned), trajectory-prediction accuracy against actual coasting, orbit
classification (circular / elliptical / escape), and station orbit stability.
tests/smoke.js stubs a minimal DOM/canvas, boots the real application, and
pumps the requestAnimationFrame loop manually to verify: boot, scenario load,
fuel burn under thrust, pause/resume, reset, zoom, time-warp, trajectory
prediction, crash, and docking.
Tuning
All gameplay constants live in js/physics.js (createWorld/createShip
defaults) and js/game.js (SCENARIOS). Change mu, planetRadius,
thrustAccel, fuelRate, dockDistance, dockSpeed, etc. to re-balance.