gizgame/flight
Real-Earth International Standard Atmosphere (ISA), 6-DoF aerodynamic flight physics, canonical aircraft catalog (shared by the flight and airline modes), and WGS84/ILS navigation.
import * as Flight from "gizgame/flight";Types
IsaState
type IsaState = { tempC: f64; pressureHpa: f64; densityKgM3: f64; soundSpeedMs: f64; }International Standard Atmosphere (ISA) state at a geometric altitude.
tempC:f64pressureHpa:f64densityKgM3:f64soundSpeedMs:f64
AircraftSpec
type AircraftSpec = { id: string; name: string; category: string; emptyKg: f64; maxFuelKg: f64; maxThrustN: f64; wingAreaM2: f64; aspectRatio: f64; cd0: f64; clMax: f64; maxMach: f64; cruiseKt: f64; rangeKm: i64; seats: i64; priceMillions: i64; fuelBurnKgPerSec: f64; }Shared aircraft aerodynamic and commercial specification (one copy for the flight and airline modes).
id:stringname:stringcategory:stringemptyKg:f64maxFuelKg:f64maxThrustN:f64wingAreaM2:f64aspectRatio:f64cd0:f64clMax:f64maxMach:f64cruiseKt:f64rangeKm:i64seats:i64priceMillions:i64fuelBurnKgPerSec:f64
FlightState
type FlightState = { lat: f64; lon: f64; altitudeM: f64; groundElevM: f64; headingDeg: f64; pitchDeg: f64; rollDeg: f64; airspeedMs: f64; groundSpeedMs: f64; verticalSpeedMs: f64; aoaDeg: f64; mach: f64; fuelKg: f64; throttle: f64; flaps: f64; gearDown: bool; stalled: bool; onGround: bool; crashed: bool; distanceFlownKm: f64; }Live 6-DoF aerodynamic flight telemetry anchored to real WGS84 coordinates and altitude.
lat:f64lon:f64altitudeM:f64groundElevM:f64headingDeg:f64pitchDeg:f64rollDeg:f64airspeedMs:f64groundSpeedMs:f64verticalSpeedMs:f64aoaDeg:f64mach:f64fuelKg:f64throttle:f64flaps:f64gearDown:boolstalled:boolonGround:boolcrashed:booldistanceFlownKm:f64
FlightControl
type FlightControl = { throttle: f64; pitchCmd: f64; rollCmd: f64; flaps: f64; gearDown: bool; brake: bool; }Pilot control inputs for a flight simulation step.
throttle:f64pitchCmd:f64rollCmd:f64flaps:f64gearDown:boolbrake:bool
IlsGuidance
type IlsGuidance = { airportCode: string; airportName: string; distKm: f64; bearingDeg: f64; localizerErrDeg: f64; glideslopeErrDeg: f64; targetAltM: f64; captured: bool; }Great-circle VOR/GPS and 3-degree ILS glideslope guidance toward a destination airport.
airportCode:stringairportName:stringdistKm:f64bearingDeg:f64localizerErrDeg:f64glideslopeErrDeg:f64targetAltM:f64captured:bool
FlightSim
class FlightSim { spec: AircraftSpec; state: FlightState; constructor(aircraftId: string, lat: f64, lon: f64, groundElevM: f64, headingDeg: f64 = 90.0, airborne: bool = false) { this.spec = aircraftById(aircraftId); const alt = airborne ? Math.max(groundElevM + 1500.0, 3000.0): groundElevM; const spd = airborne ? this.spec.cruiseKt*0.514444*0.75: 0.0; this.state = { lat, lon, altitudeM: alt, groundElevM, headingDeg: (headingDeg % 360.0 + 360.0) % 360.0, pitchDeg: 0.0, rollDeg: 0.0, airspeedMs: spd, groundSpeedMs: spd, verticalSpeedMs: 0.0, aoaDeg: 0.0, mach: spd/340.29, fuelKg: this.spec.maxFuelKg*0.85, throttle: airborne ? 0.72: 0.0, flaps: airborne ? 0.0: 0.25, gearDown: !airborne, stalled: false, onGround: !airborne, crashed: false, distanceFlownKm: 0.0 }; } step(dt: f64, ctrl: FlightControl, groundElevM: f64, month: i64 = 6): void { if (this.state.crashed || dt <= 0.0) { return; } const s = this.spec; let st = this.state; st.groundElevM = groundElevM; st.throttle = Math.max(0.0, Math.min(1.0, ctrl.throttle)); st.flaps = Math.max(0.0, Math.min(1.0, ctrl.flaps)); st.gearDown = ctrl.gearDown; const isa = isaAtmosphere(st.altitudeM); const clim = earthClimate(st.lat, st.lon, groundElevM, month); const g = 9.80665; const massKg = s.emptyKg + Math.max(0.0, st.fuelKg); const weightN = massKg*g; const maxPitch = st.onGround ? 14.0: 28.0; const targetPitch = ctrl.pitchCmd*maxPitch; const targetRoll = st.onGround ? 0.0: ctrl.rollCmd*45.0; st.pitchDeg + = (targetPitch - st.pitchDeg)*Math.min(1.0, dt*2.5); st.rollDeg + = (targetRoll - st.rollDeg)*Math.min(1.0, dt*3.0); const flightPathDeg = st.airspeedMs> 10.0 ? Math.atan2(st.verticalSpeedMs, Math.max(1.0, st.airspeedMs))*180.0/Math.PI: 0.0; st.aoaDeg = Math.max(-12.0, Math.min(24.0, st.pitchDeg - flightPathDeg)); const clSlope = 0.095; const clRaw = (st.aoaDeg + 2.2 + st.flaps*5.5)*clSlope; const clMaxEff = s.clMax + st.flaps*0.65; st.stalled = !st.onGround && (st.aoaDeg> 16.5 || (st.airspeedMs> 1.0 && clRaw> clMaxEff)); const cl = st.stalled ? clMaxEff*0.58: Math.max(-0.8, Math.min(clMaxEff, clRaw)); const q = 0.5*isa.densityKgM3*st.airspeedMs*st.airspeedMs; const liftN = q*s.wingAreaM2*cl; const inducedK = 1.0/(Math.PI*0.80*s.aspectRatio); const cdExtra = st.flaps*0.045 + (st.gearDown ? 0.025: 0.0) + (st.stalled ? 0.14: 0.0); const cd = s.cd0 + cdExtra + inducedK*cl*cl; const dragN = q*s.wingAreaM2*cd; const rhoRatio = isa.densityKgM3/1.225; const hasFuel = st.fuelKg> 0.0; const thrustN = hasFuel ? s.maxThrustN*st.throttle*Math.pow(Math.max(0.08, rhoRatio), 0.70): 0.0; if (hasFuel) { st.fuelKg = Math.max(0.0, st.fuelKg - s.fuelBurnKgPerSec*Math.max(0.08, st.throttle)*dt); } const pitchRad = st.pitchDeg*Math.PI/180.0; const rollRad = st.rollDeg*Math.PI/180.0; const rollingDragN = st.onGround ? weightN*(ctrl.brake ? 0.35: 0.02): 0.0; const accelX = (thrustN - dragN - rollingDragN - weightN*Math.sin(pitchRad))/massKg; const maxAirspeed = isa.soundSpeedMs*s.maxMach; st.airspeedMs = Math.max(0.0, Math.min(maxAirspeed, st.airspeedMs + accelX*dt)); st.mach = st.airspeedMs/Math.max(100.0, isa.soundSpeedMs); if (st.onGround) { if (liftN> weightN*1.02 && st.pitchDeg> 3.0) { st.onGround = false; st.verticalSpeedMs = Math.max(2.0, (liftN - weightN)/massKg*dt + st.airspeedMs*Math.sin(pitchRad)*0.35); } else { st.verticalSpeedMs = 0.0; st.altitudeM = groundElevM; } } else { const verticalLiftN = liftN*Math.cos(rollRad) + thrustN*Math.sin(pitchRad); const accelZ = (verticalLiftN - weightN)/massKg; st.verticalSpeedMs = Math.max(-45.0, Math.min(45.0, st.verticalSpeedMs + accelZ*dt)); st.altitudeM + = st.verticalSpeedMs*dt; if (st.altitudeM <= groundElevM) { st.altitudeM = groundElevM; if (st.gearDown && st.verticalSpeedMs> -6.5 && Math.abs(st.rollDeg) <12.0) { st.onGround = true; st.verticalSpeedMs = 0.0; st.rollDeg = 0.0; } else { st.crashed = true; st.airspeedMs = 0.0; st.verticalSpeedMs = 0.0; } } } if (!st.onGround && st.airspeedMs> 15.0) { const turnRateRad = (g*Math.tan(rollRad))/st.airspeedMs; st.headingDeg = (st.headingDeg + turnRateRad*180.0/Math.PI*dt + 360.0) % 360.0; } const jetBlend = Math.smoothstep(2000.0, 10500.0, st.altitudeM); const windUms = (clim.windU + (clim.jetU - clim.windU)*jetBlend)*0.514444; const windVms = (clim.windV + (clim.jetV - clim.windV)*jetBlend)*0.514444; const hdgRad = st.headingDeg*Math.PI/180.0; const vx = st.airspeedMs*Math.sin(hdgRad) + (st.onGround ? 0.0: windUms); const vy = st.airspeedMs*Math.cos(hdgRad) + (st.onGround ? 0.0: windVms); st.groundSpeedMs = Math.hypot(vx, vy); const stepKm = (st.groundSpeedMs*dt)/1000.0; if (stepKm> 1.0e-6) { const trackDeg = (Math.atan2(vx, vy)*180.0/Math.PI + 360.0) % 360.0; const nextGeo = destinationPoint({ lat: st.lat, lon: st.lon }, trackDeg, stepKm); st.lat = nextGeo.lat; st.lon = nextGeo.lon; st.distanceFlownKm + = stepKm; } this.state = st; } guidanceTo(destLat: f64, destLon: f64, destElevM: f64, code: string, name: string): IlsGuidance { const cur: GeoPoint = { lat: this.state.lat, lon: this.state.lon }; const dst: GeoPoint = { lat: destLat, lon: destLon }; const distKm = haversineKm(cur, dst); const bearingDeg = initialBearingDeg(cur, dst); let locErr = bearingDeg - this.state.headingDeg; while (locErr> 180.0) { locErr - = 360.0; } while (locErr <-180.0) { locErr + = 360.0; } const targetAltM = destElevM + Math.min(3000.0, distKm*52.407); const altErrM = this.state.altitudeM - targetAltM; const gsErrDeg = distKm> 0.1 ? Math.atan2(altErrM, distKm*1000.0)*180.0/Math.PI: 0.0; const captured = distKm <= 25.0 && Math.abs(locErr) <= 8.0 && Math.abs(gsErrDeg) <= 1.5; return { airportCode: code, airportName: name, distKm, bearingDeg, localizerErrDeg: locErr, glideslopeErrDeg: gsErrDeg, targetAltM, captured }; } }Real-Earth 6-DoF aerodynamic flight simulator reading ISA atmosphere and real Earth winds aloft.
spec:M224.AircraftSpecstate:M224.FlightState
Functions
isaAtmosphere
function isaAtmosphere(altitudeM: f64, surfaceTempOffsetC: f64 = 0.0): IsaStateComputes exact ISA temperature (C), pressure (hPa), air density (kg/m^3), and speed of sound (m/s) at ALTITUDEM.
Effects: fail.
aircraftCatalog
function aircraftCatalog(): Vec<AircraftSpec>Canonical aircraft catalog covering general aviation, commercial airliners, fighters, and supersonic jets.
Effects: fail, alloc.
aircraftById
function aircraftById(id: string): AircraftSpecFinds an aircraft specification by ID, defaulting to Aero 320neo.
Effects: fail, alloc.
constructor
constructor(aircraftId: string, lat: f64, lon: f64, groundElevM: f64, headingDeg: f64 = 90.0, airborne: bool = false)The aircraft AIRCRAFTID at LAT and LON over ground of GROUNDELEVM meters, on HEADINGDEG; AIRBORNE starts it in cruise.
Effects: fail, write, alloc.
Methods
FlightSim.step
step(dt: f64, ctrl: FlightControl, groundElevM: f64, month: i64 = 6): voidAdvances 6-DoF aerodynamics, ISA atmosphere, jet-stream wind drift, fuel burn, and WGS84 position by DT seconds.
Effects: fail, read, write, alloc.
FlightSim.guidanceTo
guidanceTo(destLat: f64, destLon: f64, destElevM: f64, code: string, name: string): IlsGuidanceComputes VOR/GPS great-circle bearing and 3-degree ILS glideslope guidance toward an airport.
Effects: fail, read.