Math.mod = (x,n) => ((x % n) + n) % n;
Math.cot = x => 1/Math.tan(x);
const degToRad = d => d * Math.PI / 180;
const radToDeg = r => r * 180 / Math.PI;
const angleWrapDeg = d => Math.mod(d+180, 360)-180;
return Math.random () * (b-a) + a;
this.headingVector = math.matrix([0,1,0]);
this.updateAttitudeVectors();
updateAttitudeVectors () {
this.pitchVector = math.cross([1,0,0], this.headingVector);
if(math.hypot(this.pitchVector) == 0) {
this.pitchVector = math.cross([0,1,0], this.headingVector);
engineThrust (speed, throttle) {
let throttleMult = throttle ** 2;
const enginePrefs = [377, 390, 403, 416, 455, 481, 520, 572, 650, 741, 845, 975, 1092, 1248, 1365, 1430, 1456, 1469, 1456, 1430, 1365, 1248, 1105, 845, 260, 65, 0];
const index = 2 * 3.6 * 0.01 * speed;
const thrust = lerp(enginePrefs[Math.floor(index)], enginePrefs[Math.ceil(index)], index - Math.floor(index));
return thrust * throttleMult;
this.speed -= (0.5 * 1.293 * this.speed ** 2 * this.dragCoeff * this.getRefArea()) / this.mass;
this.speed += this.engineThrust (this.speed, this.throttle) / this.mass;
this.x += Math.cos(this.pitch) * Math.sin(degToRad(this.heading)) * this.speed * dt;
this.y += Math.cos(this.pitch) * Math.cos(degToRad(this.heading)) * this.speed * dt;
this.altitude += Math.sin(this.pitch) * this.speed * dt;
let dist = Math.hypot(x,y,z);
let baseDist = Math.hypot(baseX, baseZ);
let azumith = Math.atan2(baseX,baseZ)
let altitude = Math.acos(baseX * x + baseZ * z);
return [azumith, altitude];
let [azumith, altitude] = this.getPosition();
let angX = azumith - degToRad(rotation);
let angY = altitude - degToRad(elevation);
if(Math.abs(angleWrapDeg(angleWrapDeg(radToDeg(azumith)) - angleWrapDeg(rotation))) > 90) return;
let screenX = Math.tan(angX) * FOV;
let screenY = Math.tan(angY) * FOV;
if(Math.abs(screenX) > width*0.6) return;
if(Math.abs(screenY) > height*0.6) return;
translate(width*0.5, height*0.5);
translate(screenX, screenY)
createCanvas(windowWidth, windowHeight);
plane = new Plane (0, 0, 1000);
grapher = new Grapher ("#fafafa", 5000)
plane.update(1/getFrameRate());
grapher.update(plane.speed);