This is the Source Code for the Coriolis Model on the page How Airplanes correct for the Coriolis Effect.
#INCLUDE JsGraphX3D.inc
#INCLUDE ControlPanel.inc
#INCLUDE EarthMap.inc
#INCLUDE MouseWheel.inc
<jscript>
var Pi = Math.PI;
var Pi2 = Pi / 2;
var MaxZoom = 50;
var MinZoom = 1;
var MaxVAng = 88;
var MinVAng = -88;
var MinObjSpeed = 0;
var MaxObjSpeed = 1500;
var MaxTrackDistance = 4 * Pi * 6371000; // 2 orbit
var MinForcedOrbitalSpeed = 25; // to restrict number of earth rotations in ballistic flight
var MinForcedObjSpeed = 25; // dito for controlled flight
var SpaceGridRadiusFactor = 1.15;
var CompResolution = Pi / 180e4; // defines the delta central angle for velocity/accel computations
function ToRad( d ) { return d * Pi / 180; }
function ToDeg( d ) { return d * 180 / Pi; }
function Limit1( x ) { return x < -1 ? -1 : x > 1 ? 1 : x; }
var abs = Math.abs;
var sin = Math.sin;
var cos = Math.cos;
var asin = Math.asin;
var acos = Math.acos;
var V2 = JsgVect2;
var V3 = JsgVect3;
var M3 = JsgMat3;
NumFormatter.TableLike = false; // no fractal grouping below 4 digits
function Format( num, units, digits ) {
units = xDefStr( units, '' );
digits = xDefNum( digits, 4 );
return NumFormatter.Format( num, 'std', digits, units );
}
function FormatF( num, units, digits ) {
units = xDefStr( units, '' );
digits = xDefNum( digits, 4 );
return NumFormatter.Format( num, 'fix0', digits, units );
}
function FormatDMS( num ) {
return NumFormatter.Format( num, 'dms', 4 );
}
/////////////////////////////////////////////////////////////////////
var CoriolisModel = {
// viewing parameters
HAngSpace: 0,
VAngSpace: 15,
HAngEarth: 0,
VAngEarth: 15,
Zoom: 1.4,
Scale: 5,
// flying object parameters
FlightType: 0, // 0 -> ballistic, 1 -> controlled
ObjSpeed: 230, // m/s
StartLat: 45, // all aangles in degrees
StartLong: 0,
StartHeading: 180,
TrackDistance: 2000000,
// some settings
MinAngleDelta: 0.25, // resolution for central angle on track calculations
CameraDist: 63710000, // distance of virtual camera from earths center
Altitude: 10000, // height of tracks above surface
EnableEarthGrid: true,
EnableSpaceGrid: true,
EnableAxes: true,
EnableData: true,
Debug: false,
// some private values
TripTime: 0,
EarthRotAtTime: 0,
OrbitalSpeed: 0,
SpeedForced: false, // if orbital/controlled speed has been forced to minimum speed
OrbitalHeading: 0,
CompResolution: CompResolution,
R: 6371000, // m, earth radius
Rh: 6371000,
Omega: 2 * Pi / (24 * 3600), // 1/s, earth rotation rate
Graph: null,
MouseIsDown: false,
MouseLastPosX: 0,
MouseLastPosY: 0,
MouseMoveDistance: 0,
MouseInSpaceViewport: true,
Init: function( graph ) {
EarthMap.Radius = this.R;
EarthMap.SetLandColor( 'Antarctica', '#ddd', '#bbb' );
this.Graph = graph;
this.AddEventListeners();
this.Update();
},
AddEventListeners: function() {
var me = this;
addWheelListener( xElement('CoriolisModelCanvas'), function(e){ me.OnMouseScroll(e); } );
this.Graph.AddEventHandler( 'mousedown', function(e){ me.OnMouseDown(e); } );
this.Graph.AddEventHandler( 'touchstart', function(e){ me.OnTouchStart(e); } );
this.Graph.AddEventHandler( 'mouseup', function(e){ me.OnMouseUp(e); } );
this.Graph.AddEventHandler( 'touchend', function(e){ me.OnTouchEnd(e); } );
this.Graph.AddEventHandler( 'mousemove', function(e){ me.OnMouseMove(e); } );
this.Graph.AddEventHandler( 'touchmove', function(e){ me.OnTouchMove(e) } );
this.Graph.AddEventHandler( 'mouseleave', function(e){ me.OnMouseLeave(e); } );
this.Graph.AddEventHandler( 'touchcancel', function(e){ me.OnTouchCancel(e); } );
// disable context menu
this.Graph.Canvas.oncontextmenu = function(){return false;};
},
OnMouseScroll: function( event ) {
if (event.shiftKey) {
if (event.deltaY < 0) {
this.Zoom *= 1.1;
} else {
this.Zoom /= 1.1;
}
this.Update( 'Zoom' );
} else {
var factor = 1.1;
if (event.ctrlKey) factor = 1.6;
if (event.altKey) factor = 1.01;
if (event.deltaY < 0) {
this.Zoom *= factor;
} else {
this.Zoom /= factor;
}
this.Update( 'Zoom' );
}
event.preventDefault();
return true;
},
GetTouchOffsetPos: function( event ) {
// calculates the touch position with respect to the target element of the event.
// this corresponds to the offsetX, offsetY values of mouse events.
// event is a native browser event (not wraped by X-Module)
var rect = event.target.getBoundingClientRect();
var x = event.targetTouches[0].pageX - rect.left;
var y = event.targetTouches[0].pageY - rect.top;
return [ x, y ];
},
OnTouchStart: function( event ) {
var originalEvent = event.event;
if (!originalEvent.touches) {
this.MouseIsDown = false;
return true;
}
var pos = this.GetTouchOffsetPos( originalEvent );
return this.StartTracking( event, pos );
},
OnMouseDown: function( event ) {
return this.StartTracking( event, [ event.offsetX, event.offsetY ] );
},
StartTracking: function( event, pos ) {
event.PreventDefault();
this.MouseIsDown = true;
this.MouseLastPosX = pos[0];
this.MouseLastPosY = pos[1];
this.MouseMoveDistance = 0;
if (pos[0] < this.Graph.VpWidth/2) {
this.MouseInSpaceViewport = true;
} else {
this.MouseInSpaceViewport = false;
}
return true;
},
OnMouseUp: function( event ) {
if (!this.MouseIsDown) return true;
this.MouseIsDown = false;
event.PreventDefault();
// if mouse was not moved signifantly fire click event
if (this.MouseMoveDistance <= this.MouseMaxClickMove) {
this.OnClick( event );
}
return true;
},
OnTouchEnd: function( event ) {
if (!this.MouseIsDown) return true;
this.MouseIsDown = false;
event.PreventDefault();
return true;
},
OnMouseLeave: function( event ) {
this.MouseIsDown = false;
return true;
},
OnTouchCancel: function( event ) {
this.MouseIsDown = false;
return true;
},
OnMouseMove: function ( event ) {
if (!this.MouseIsDown) {
return true;
}
return this.ContinueTracking( event, [ event.offsetX, event.offsetY ] );
},
OnTouchMove: function ( event ) {
var originalEvent = event.event;
if (!this.MouseIsDown || !originalEvent.touches) {
this.MouseIsDown = false;
return true;
}
var pos = this.GetTouchOffsetPos( originalEvent );
return this.ContinueTracking( event, pos );
},
ContinueTracking: function( event, pos ) {
event.PreventDefault();
var moveX = pos[0] - this.MouseLastPosX;
var moveY = pos[1] - this.MouseLastPosY;
if (moveX == 0 && moveY == 0) {
return true;
}
this.MouseMoveDistance += abs(moveX) + abs(moveY);
this.MouseLastPosX = pos[0];
this.MouseLastPosY = pos[1];
// change camera angle
var incrementBoost = 0.45;
if (event.ctrlKey) incrementBoost *= 5;
if (event.altKey) incrementBoost /= 10;
var control = '';
if (this.MouseInSpaceViewport || event.shiftKey) {
this.HAngSpace -= incrementBoost * moveX / this.Zoom;
this.VAngSpace += incrementBoost * moveY / this.Zoom;
control += 'VAngSpace,HAngSpace,';
}
if (!this.MouseInSpaceViewport || event.shiftKey) {
this.HAngEarth -= incrementBoost * moveX / this.Zoom;
this.VAngEarth += incrementBoost * moveY / this.Zoom;
control += 'VAngEarth,HAngEarth';
}
this.Update( control );
return true;
},
OnClick: function( event ) {
},
Update: function( controller ) {
if (this.Debug) {
this.CompResolution = Pi / 72;
} else {
this.CompResolution = CompResolution;
}
if (ControlPanels.MatchesField( controller, 'Zoom' )) {
if (this.Zoom > MaxZoom) {
this.Zoom = MaxZoom;
} else if (this.Zoom < MinZoom) {
this.Zoom = MinZoom;
}
}
if (ControlPanels.MatchesField( controller, 'HAngSpace,VAngSpace' )) {
if (this.VAngSpace < MinVAng) {
this.VAngSpace = MinVAng;
} else if (this.VAngSpace > MaxVAng) {
this.VAngSpace = MaxVAng;
}
if (this.HAngSpace > 180) {
this.HAngSpace -= 360;
} else if (this.HAngSpace <= -180) {
this.HAngSpace += 360;
}
}
if (ControlPanels.MatchesField( controller, 'HAngEarth,VAngEarth' )) {
if (this.VAngEarth < MinVAng) {
this.VAngEarth = MinVAng;
} else if (this.VAngEarth > MaxVAng) {
this.VAngEarth = MaxVAng;
}
if (this.HAngEarth > 180) {
this.HAngEarth -= 360;
} else if (this.HAngEarth <= -180) {
this.HAngEarth += 360;
}
}
// calculations
this.Rh = this.R + this.Altitude;
// calculate orbital velocity from airplane velocity plus tangential speed of earth surface at takeoff
this.SpeedForced = false;
if (this.FlightType == 0) {
var heading = ToRad( this.StartHeading );
var tangentialSpeedEarth = this.Omega * this.R * cos( ToRad(this.StartLat) );
var airplaneVelocity = [ this.ObjSpeed * sin( heading ), this.ObjSpeed * cos( heading ) ];
var orbitalVelocity = V2.Add( [ tangentialSpeedEarth, 0 ], airplaneVelocity );
this.OrbitalSpeed = V2.Length( orbitalVelocity );
if (this.OrbitalSpeed < MinForcedOrbitalSpeed) {
this.SpeedForced = true;
if (this.OrbitalSpeed < 0.1) {
orbitalVeloctity = [ -1, 0 ];
}
orbitalVelocity = V2.Scale( V2.Norm( orbitalVelocity ), MinForcedOrbitalSpeed );
this.OrbitalSpeed = V2.Length( orbitalVelocity );
}
this.OrbitalHeading = Pi - V2.Angle( [ 0, -1 ], V2.Norm( orbitalVelocity ) );
this.TripTime = this.TrackDistance / this.OrbitalSpeed;
this.EarthRotAtTime = ToDeg( this.Omega * this.TripTime );
} else {
if (this.ObjSpeed < MinForcedObjSpeed) {
this.ObjSpeed = MinForcedObjSpeed;
this.SpeedForced = true;
}
this.TripTime = this.TrackDistance / this.ObjSpeed;
this.EarthRotAtTime = ToDeg( this.Omega * this.TripTime );
}
ControlPanels.Update();
this.Draw();
},
PolarToCartesic: function( phi, lambda, len ) {
// phi = latitude in raad, lambda = longitude in rad
var r = len * cos( phi );
var x = r * cos( lambda );
var y = r * sin( lambda );
var z = len * sin( phi );
return [x, y, z ];
},
CartesicToPolar: function( coord ) {
// returns [ Lat, Lon ] object, angles in rad
var lat = 0;
var long = 0;
var vectXY = [ coord[0], coord[1], 0 ];
if (V3.Length(vectXY) == 0) {
// coord is up or down, so long is undefined -> set long = 0
lat = (coord[2] >= 0) ? Pi2 : -Pi2;
} else {
// assert V3.Length(coord) > 0, so Norm returns no null vector
var vectXYNorm = V3.Norm( vectXY );
var coordNorm = V3.Norm( coord );
lat = Pi2 - acos( Limit1( V3.ScalarProd( [0,0,1], coordNorm ) ) );
long = acos( Limit1( V3.ScalarProd( [1,0,0], vectXYNorm ) ) );
if (vectXYNorm[1] < 0) long *= -1;
}
return [ lat, long ];
},
Draw: function() {
var g = this.Graph;
g.Reset3D();
g.SetAngleMeasure( 'deg' );
this.DrawSpaceView( g );
this.DrawEarthView( g );
// label flight type
g.SetViewport();
g.SetTextAttrS( 'Arial', 16, 'black', 'bS', 8 );
var oldTrans = g.SelectTrans( 'viewport' );
g.SetAreaAttr( 'white', 'white', 1 );
if (this.FlightType == 0) {
var txt = 'Ballistic Flight (Orbit), no Friction: Showing Coriolis Effect (black Line)';
g.TextBox( txt, g.VpWidth/2, g.VpHeight, 3 );
g.Text( txt, g.VpWidth/2, g.VpHeight );
if (this.EnableData) {
var time = this.TrackDistance / this.OrbitalSpeed / 3600;
txt = 'Time ' + NumFormatter.Format( time, 'hms', 4 );
g.SetTextAttrS( 'Arial', 14, 'black', 'WS', 8 );
g.TextBox( txt, 0, g.VpHeight, 3 );
g.Text( txt, 0, g.VpHeight );
}
} else {
var txt = 'Controlled Flight: Banking counters Coriolis Effect to maintain Straight Flight';
g.TextBox( txt, g.VpWidth/2, g.VpHeight, 3 );
g.Text( txt, g.VpWidth/2, g.VpHeight );
if (this.EnableData) {
var time = this.TrackDistance / this.ObjSpeed / 3600;
txt = 'Time ' + NumFormatter.Format( time, 'hms', 4 );
g.SetTextAttrS( 'Arial', 14, 'black', 'WS', 8 );
g.TextBox( txt, 0, g.VpHeight, 3 );
g.Text( txt, 0, g.VpHeight );
}
}
g.SelectTrans( oldTrans );
},
DrawSpaceView: function( g ) {
// calculate scene size and setup virtual 3D camera
var alpha = acos( EarthMap.Radius / this.CameraDist );
var scale = this.Scale * EarthMap.Radius / cos( Math.PI/2 - alpha );
g.SetCameraScale( scale );
g.SetCameraView( [0,0,0], this.HAngSpace, this.VAngSpace, this.CameraDist );
g.SetCameraZoom( this.Zoom );
// set left viewport and map camera to this viewport
// camera clipping clips graphic that extends behind the camera sensor plane
g.SetViewport( 0, 0, g.CanvasWidth/2, g.CanvasHeight, false, true );
g.SetViewportRel( 0, 0, 2, 0 );
g.SetWindowToCameraScreen();
g.SetCameraClipping( 0.001 );
if (this.EnableAxes) {
// set coordinate system rotation and draw it
g.TransRotateVect3D( [0,0,1], -this.StartLong );
this.DrawCoordinateSystem( g );
}
// set earth rotation and draw earth
g.TransRotateVect3D( [0,0,1], this.EarthRotAtTime - this.StartLong );
this.DrawEarth( g );
if (this.EnableSpaceGrid) {
g.TransRotateVect3D( [0,0,1], -this.StartLong );
g.SetAreaAttr( 'blue', 'blue', 1 );
EarthMap.Radius *= SpaceGridRadiusFactor;
EarthMap.SetClipPlaneOffset( g, this.CompSpaceGridClipPlaneOffset( g, EarthMap.Radius ) );
this.DrawEarthGrid( g, 0.5 );
EarthMap.SetClipPlaneOffset( g, 0 );
g.TransRotateVect3D( [0,0,1], -this.StartLong );
this.DrawEarthGrid( g );
EarthMap.Radius = this.R;
}
if (this.EnableEarthGrid) {
g.TransRotateVect3D( [0,0,1], this.EarthRotAtTime - this.StartLong );
g.SetAreaAttr( 'black', 'black', 1 );
this.DrawEarthGrid( g );
}
this.DrawSpaceFlightPath( g );
g.ResetTrans3D();
// label viewport
g.SetTextAttrS( 'Arial', 20, 'black', 'bNW', 8 );
var oldTrans = g.SelectTrans( 'viewport' );
g.Text( 'Inertial Reference Frame Space', g.ScalePix(2), 0 );
g.SelectTrans( oldTrans );
g.SetClipping();
},
DrawEarthView: function( g ) {
// calculate scene size and setup virtual 3D camera
var alpha = acos( EarthMap.Radius / this.CameraDist );
var scale = this.Scale * EarthMap.Radius / cos( Math.PI/2 - alpha );
g.SetCameraScale( scale );
g.SetCameraView( [0,0,0], this.HAngEarth, this.VAngEarth, this.CameraDist );
g.SetCameraZoom( this.Zoom );
// set left viewport and map camera to this viewport
// camera clipping clips graphic that extends behind the camera sensor plane
g.SetViewport( g.CanvasWidth/2, 0, g.CanvasWidth/2, g.CanvasHeight, false, true );
g.SetViewportRel( 2, 0, 0, 0 );
g.SetWindowToCameraScreen();
g.SetCameraClipping( 0.001 );
if (this.EnableAxes) {
// set coordinate system rotation and draw it
g.TransRotateVect3D( [0,0,1], -(this.EarthRotAtTime + this.StartLong) );
this.DrawCoordinateSystem( g );
}
// set earth rotation and draw earth
g.TransRotateVect3D( [0,0,1], -this.StartLong );
this.DrawEarth( g );
if (this.EnableEarthGrid) {
g.TransRotateVect3D( [0,0,1], -this.StartLong );
g.SetAreaAttr( 'black', 'black', 1 );
this.DrawEarthGrid( g );
}
this.DrawEarthFlightPath( g );
g.ResetTrans3D();
// label viewport
g.SetTextAttrS( 'Arial', 20, 'black', 'bNW', 8 );
var oldTrans = g.SelectTrans( 'viewport' );
g.Text( 'Rotating Reference Frame Earth', g.ScalePix(2), 0 );
g.SelectTrans( oldTrans );
g.SetClipping();
},
DrawEarth: function( g ) {
EarthMap.Trans = g.Trans3D;
g.ResetTrans3D();
g.SetAlpha( 0.5 );
EarthMap.DrawGlobe( g );
},
DrawEarthGrid: function( g, alphaFactor ) {
alphaFactor = xDefNum( alphaFactor, 1 );
EarthMap.Trans = g.Trans3D;
g.ResetTrans3D();
g.SetAlpha( 0.25 * alphaFactor );
EarthMap.DrawGlobeGrid( g );
g.SetAlpha( 0.5 * alphaFactor );
EarthMap.DrawGlobeEquator( g, 0 );
EarthMap.DrawGlobeMeridian( g, 0 );
g.SetAlpha( 1 );
},
DrawSpaceFlightPath: function( g ) {
// set clipping plane to hide path behind earth
this.SetEarthClipPlane( g );
if (this.FlightType == 0) {
this.DrawBallisticSpaceFlightPath( g );
} else {
this.DrawControledSpaceFlightPath( g );
}
g.ResetTrans3D();
g.DeleteClipPlanes();
},
DrawEarthFlightPath: function( g ) {
// set clipping plane to hide path behind earth
this.SetEarthClipPlane( g );
if (this.FlightType == 0) {
this.DrawBallisticEarthFlightPath( g );
} else {
this.DrawControledEarthFlightPath( g );
}
g.ResetTrans3D();
g.DeleteClipPlanes();
},
DrawBallisticSpaceFlightPath: function( g ) {
this.DrawBallisticFlightPath( g, 0, 1, 0.4, false );
},
DrawBallisticEarthFlightPath: function( g ) {
this.DrawBallisticFlightPath( g, ToRad( this.EarthRotAtTime ), 0.3, 1, true );
},
DrawBallisticFlightPath: function( g, earthRot, orbitAlpha, trackAlpha, isEarthView ) {
var heading = this.OrbitalHeading;
var lat = ToRad( this.StartLat );
var long = ToRad( this.StartLong );
var transMat = M3.RotatingZ( -earthRot, M3.RotatingY( -lat, M3.RotatingX( -heading ) ) );
// draw orbit
g.NewPoly3D();
var deltaAng = ToRad( this.MinAngleDelta );
var limitAng = 2 * Pi + deltaAng / 2;
for (var ang = 0; ang < limitAng; ang += deltaAng) {
g.AddPointToPoly3D( this.GetPointOnTrack( ang, 0, transMat, this.Rh ) );
}
this.DrawPoly( g, 'red', 2, orbitAlpha/4 );
// draw ballistic path on orbit
g.NewPoly3D();
var limitAng = this.TrackDistance / this.R;
for (var ang = 0; ang < limitAng; ang += deltaAng) {
g.AddPointToPoly3D( this.GetPointOnTrack( ang, 0, transMat, this.Rh ) );
}
g.AddPointToPoly3D( this.GetPointOnTrack( limitAng, 0, transMat, this.Rh ) );
this.DrawPoly( g, 'red', 2, orbitAlpha );
// draw ballistic path on earth
g.NewPoly3D();
var oro = this.Omega * this.R / this.OrbitalSpeed;
var lastPos = null;
var dist = 0;
for (var ang = 0; ang < limitAng; ang += deltaAng) {
var rot = (limitAng-ang) * oro;
var pos = this.GetPointOnTrack( ang, rot, transMat, this.Rh );
if (lastPos) {
dist += V3.Length( V3.Sub( pos, lastPos ) );
}
lastPos = pos;
g.AddPointToPoly3D( pos );
}
var pos = this.GetPointOnTrack( limitAng, 0, transMat, this.Rh );
if (lastPos) {
dist += V3.Length( V3.Sub( pos, lastPos ) );
}
g.AddPointToPoly3D( pos );
this.DrawPoly( g, 'black', 2, trackAlpha );
// draw markers
var rot = limitAng * oro;
this.DrawMarker( g, this.GetPointOnTrack( 0, rot, transMat, this.Rh ), 'Square', 4, 'green' );
g.SetAlpha( orbitAlpha );
this.DrawMarker( g, this.GetPointOnTrack( 0, 0, transMat, this.Rh ), 'Square', 4, 'red' );
g.SetAlpha( 1 );
if (isEarthView) {
var time = this.TrackDistance / this.OrbitalSpeed;
var pAim = this.CompAimPos( time, this.Rh );
g.SetAlpha( 0.3 );
this.DrawMarker( g, pAim, 'Circle', 6, 'blue' );
g.SetAlpha( 1 );
}
this.DrawMarker( g, this.GetPointOnTrack( limitAng, 0, transMat, this.Rh ), 'Circle', 6, 'blue' );
// draw values
if (this.EnableData) {
if (isEarthView) {
// lat, long and distance in earth coord system
var latLong = this.CartesicToPolar( this.GetPointOnTrack( limitAng, 0, transMat, this.R ) );
var txt = 'Earth Lat ' + FormatDMS( ToDeg(latLong[0]) )
+ ' | Long ' + FormatDMS( ToDeg(latLong[1]) + this.StartLong )
+ ' | Distance ' + FormatF( dist/1000, ' km', 1 );
this.DrawText( g, txt, true );
// heading and speed
var ds = this.CompResolution;
var dt = ds * this.R / this.OrbitalSpeed;
var p0 = this.GetPointOnTrack( limitAng-ds, -(limitAng-ds)*oro+earthRot, transMat, this.R );
var p1 = this.GetPointOnTrack( limitAng, -limitAng*oro+earthRot, transMat, this.R );
var p2 = this.GetPointOnTrack( limitAng+ds, -(limitAng+ds)*oro+earthRot, transMat, this.R );
var dir = V3.Norm( V3.Sub( p2, p1 ) );
var up = V3.Norm( p1 );
var left = V3.Norm( V3.Mult( up, dir ) );
var dir = V3.Norm( V3.Mult( left, up ) ); // correct dir to be level at P1
var east = V3.Norm( V3.Mult( [0,0,1], up ) );
var north = V3.Norm( V3.Mult( up, east ) );
if (this.Debug) {
g.SetMarkerAttr( 'Circle', 6, 'red', 'red', 1 );
g.Marker3D( p0, 3 );
g.Marker3D( p1, 3 );
g.Marker3D( p2, 3 );
}
var bulletHeading = V3.Angle( dir, north );
if (V3.ScalarProd( dir, east ) < 0) bulletHeading = 2*Pi - bulletHeading;
var speed = V3.Length( V3.Sub( p2, p1 ) ) / dt;
var headingStr = FormatF( ToDeg(bulletHeading), '°', 2 );
if (speed < 1e-5) headingStr = 'undefined';
var forced = this.SpeedForced ? ' (forced)' : '';
txt = 'Speed ' + FormatF( speed, ' m/s', 2 ) + forced
+ ' | Heading ' + headingStr + ' (' + this.GetCompassLabel(ToDeg(bulletHeading)) + ')';
this.DrawText( g, txt );
// acceleration
var accel = V3.Scale( V3.Sub( V3.Sub( p2, p1 ), V3.Sub( p1, p0 ) ), 1/(dt*dt) );
var accDir = V3.ScalarProd( dir, accel );
var accLeft = V3.ScalarProd( left, accel );
var accUp = V3.ScalarProd( up, accel );
var lr = ' | Left ';
var lrSign = 1;
if (accLeft < 0.00005) {
lr = ' | Right ';
lrSign = -1;
}
txt = 'Accel Forward ' + FormatF( accDir )
+ lr + FormatF( lrSign * accLeft )
+ ' | Down ' + FormatF( -accUp ) + ' m/s^2';
this.DrawText( g, txt );
// course deviation and horizontal coriolis component
var coriolisAccel = 2 * speed * this.Omega * sin( latLong[0] );
clr = 'Right ';
clrSign = 1;
if (coriolisAccel <= -0.00005) {
clr = 'Left ';
clrSign = -1;
}
var deviation = this.CompCourseDeviation( p1, time ) / 1e3;
var digits = 3;
if (deviation >= 100) digits--;
if (deviation >= 1000) digits--;
var devStr = FormatF( deviation, ' km', digits );
txt = 'Deviation ' + devStr
+ ' | Coriolis Accel ' + clr + FormatF( clrSign * coriolisAccel, ' m/s^2', 4 );
g.SetTextColor( 'blue' );
this.DrawText( g, txt );
} else {
// lat, long and distance in space coord system
var latLong = this.CartesicToPolar( this.GetPointOnTrack( limitAng, 0, transMat, this.R ) );
var txt = 'Space Lat ' + FormatDMS( ToDeg(latLong[0]) )
+ ' | Long ' + FormatDMS( ToDeg(latLong[1]) + this.StartLong )
+ ' | Distance ' + FormatF( this.TrackDistance/1000, ' km', 1 );
this.DrawText( g, txt, true );
// heading and speed
var ds = this.CompResolution;
var dt = ds * this.R / this.OrbitalSpeed;
var p0 = this.GetPointOnTrack( limitAng-ds, 0, transMat, this.R );
var p1 = this.GetPointOnTrack( limitAng, 0, transMat, this.R );
var p2 = this.GetPointOnTrack( limitAng+ds, 0, transMat, this.R );
var dir = V3.Norm( V3.Sub( p2, p1 ) );
var up = V3.Norm( p1 );
var left = V3.Norm( V3.Mult( up, dir ) );
var dir = V3.Norm( V3.Mult( left, up ) ); // correct dir to be level at P1
var east = V3.Norm( V3.Mult( [0,0,1], up ) );
var north = V3.Norm( V3.Mult( up, east ) );
if (this.Debug) {
g.SetMarkerAttr( 'Circle', 6, 'red', 'red', 1 );
g.Marker3D( p0, 3 );
g.Marker3D( p1, 3 );
g.Marker3D( p2, 3 );
}
var bulletHeading = V3.Angle( dir, north );
if (V3.ScalarProd( dir, east ) < 0) bulletHeading = 2*Pi - bulletHeading;
var forced = this.SpeedForced ? ' (forced)' : '';
txt = 'Speed ' + FormatF( this.OrbitalSpeed, ' m/s', 2 ) + forced
+ ' | Heading ' + FormatF( ToDeg(bulletHeading), '°', 2 )
+ ' (' + this.GetCompassLabel(ToDeg(bulletHeading)) + ')';
this.DrawText( g, txt );
// acceleration
var accel = V3.Scale( V3.Sub( V3.Sub( p2, p1 ), V3.Sub( p1, p0 ) ), 1/(dt*dt) );
var accDir = V3.ScalarProd( dir, accel );
var accLeft = V3.ScalarProd( left, accel );
var accUp = V3.ScalarProd( up, accel );
var lr = ' | Left ';
var lrSign = 1;
if (accLeft < 0.00005) {
lr = ' | Right ';
lrSign = -1;
}
txt = 'Accel Forward ' + FormatF( accDir )
+ lr + FormatF( lrSign * accLeft )
+ ' | Down ' + FormatF( -accUp ) + ' m/s^2';
this.DrawText( g, txt );
}
}
},
DrawControledSpaceFlightPath: function( g ) {
this.DrawControledFlightPath( g, ToRad( this.EarthRotAtTime ), 1, 0.4, false );
},
DrawControledEarthFlightPath: function( g ) {
this.DrawControledFlightPath( g, 0, 0.3, 1, true );
},
DrawControledFlightPath: function( g, earthRot, orbitAlpha, trackAlpha, isEarthView ) {
var heading = ToRad(this.StartHeading);
var lat = ToRad( this.StartLat );
var long = ToRad( this.StartLong );
var transMat = M3.RotatingZ( earthRot, M3.RotatingY( -lat, M3.RotatingX( -heading ) ) );
// draw space path
g.NewPoly3D();
var deltaAng = ToRad( this.MinAngleDelta );
var limitAng = this.TrackDistance / this.R;
var oro = this.Omega * this.R / this.ObjSpeed;
var lastPos = null;
var dist = 0;
for (var ang = 0; ang < limitAng; ang += deltaAng) {
var rot = (limitAng - ang) * oro;
var pos = this.GetPointOnTrack( ang, -rot, transMat, this.Rh );
if (lastPos) {
dist += V3.Length( V3.Sub( pos, lastPos ) );
}
lastPos = pos;
g.AddPointToPoly3D( pos );
}
var pos = this.GetPointOnTrack( ang, -rot, transMat, this.Rh );
if (lastPos) {
dist += V3.Length( V3.Sub( pos, lastPos ) );
}
g.AddPointToPoly3D( this.GetPointOnTrack( limitAng, 0, transMat, this.Rh ) );
this.DrawPoly( g, 'red', 2, orbitAlpha );
// draw earth path
g.NewPoly3D();
for (var ang = 0; ang < limitAng; ang += deltaAng) {
g.AddPointToPoly3D( this.GetPointOnTrack( ang, 0, transMat, this.Rh ) );
}
g.AddPointToPoly3D( this.GetPointOnTrack( limitAng, 0, transMat, this.Rh ) );
this.DrawPoly( g, 'black', 2, trackAlpha );
// draw markers
this.DrawMarker( g, this.GetPointOnTrack( 0, 0, transMat, this.Rh ), 'Square', 4, 'green' );
g.SetAlpha( orbitAlpha );
var rot = (this.TrackDistance / this.ObjSpeed) * this.Omega;
this.DrawMarker( g, this.GetPointOnTrack( 0, -rot, transMat, this.Rh ), 'Square', 4, 'red' );
g.SetAlpha( 1 );
// draw stylistic plane. we need to calculate the heading from 2 nearby positions
var ds = this.CompResolution;
var p1 = this.GetPointOnTrack( limitAng, 0, transMat, this.Rh );
var p2 = this.GetPointOnTrack( limitAng+ds, 0, transMat, this.Rh );
var dir = V3.Norm( V3.Sub( p2, p1 ) );
var up = V3.Norm( p1 );
var left = V3.Norm( V3.Mult( up, dir ) );
var dir = V3.Norm( V3.Mult( left, up ) ); // correct dir to be level at P1
var fuselageLen2 = 35e4 / this.Zoom;
var wingSpan2 = 50e4 / this.Zoom;
var tailWingSize = 20e4 / this.Zoom;
g.SetLineAttr( 'blue', 3 );
g.Line3D(
V3.Add( p1, V3.Scale( dir, fuselageLen2 ) ),
V3.Add( p1, V3.Scale( dir, -fuselageLen2 ) ) );
g.Line3D(
V3.Add( p1, V3.Scale( left, wingSpan2 ) ),
V3.Add( p1, V3.Scale( left, -wingSpan2 ) ) );
var tailPos = V3.Add( p1, V3.Scale( dir, -fuselageLen2 ) );
g.Line3D(
V3.Add( tailPos, V3.Scale( left, tailWingSize ) ),
V3.Add( tailPos, V3.Scale( left, -tailWingSize ) ) );
g.Line3D(
tailPos,
V3.Add( tailPos, V3.Scale( up, 1.5*tailWingSize ) ) );
// draw values
if (this.EnableData) {
if (isEarthView) {
// lat, long and distance in earth coord system
var latLong = this.CartesicToPolar( this.GetPointOnTrack( limitAng, 0, transMat, this.R ) );
var txt = 'Earth Lat ' + FormatDMS( ToDeg(latLong[0]) )
+ ' | Long ' + FormatDMS( ToDeg(latLong[1]) + this.StartLong )
+ ' | Distance ' + FormatF( this.TrackDistance/1000, ' km', 1 );
this.DrawText( g, txt, true );
// heading and speed
var ds = this.CompResolution;
var dt = ds * this.R / this.ObjSpeed;
var p0 = this.GetPointOnTrack( limitAng-ds, 0, transMat, this.R );
var p1 = this.GetPointOnTrack( limitAng, 0, transMat, this.R );
var p2 = this.GetPointOnTrack( limitAng+ds, 0, transMat, this.R );
var dir = V3.Norm( V3.Sub( p2, p1 ) );
var up = V3.Norm( p1 );
var left = V3.Norm( V3.Mult( up, dir ) );
var dir = V3.Norm( V3.Mult( left, up ) ); // correct dir to be level at p1
var east = V3.Norm( V3.Mult( [0,0,1], up ) );
var north = V3.Norm( V3.Mult( up, east ) );
if (this.Debug) {
g.SetMarkerAttr( 'Circle', 6, 'red', 'red', 1 );
g.Marker3D( p0, 3 );
g.Marker3D( p1, 3 );
g.Marker3D( p2, 3 );
}
var planeHeading = V3.Angle( dir, north );
if (V3.ScalarProd( dir, east ) < 0) planeHeading = 2*Pi - planeHeading;
var headingStr = FormatF( ToDeg(planeHeading), '°', 2 );
if (this.ObjSpeed < 1e-5) headingStr = 'undefined';
var forced = this.SpeedForced ? ' (forced)' : '';
txt = 'Speed ' + FormatF( this.ObjSpeed, ' m/s', 2 ) + forced
+ ' | Heading ' + headingStr
+ ' (' + this.GetCompassLabel(ToDeg(planeHeading)) + ')';
this.DrawText( g, txt );
// acceleration
var accel = V3.Scale( V3.Sub( V3.Sub( p2, p1 ), V3.Sub( p1, p0 ) ), 1/(dt*dt) );
var accDir = V3.ScalarProd( dir, accel );
var accLeft = V3.ScalarProd( left, accel );
var accUp = V3.ScalarProd( up, accel );
var lr = ' | Left ';
var lrSign = 1;
if (accLeft < 0.00005) {
lr = ' | Right ';
lrSign = -1;
}
txt = 'Accel Forward ' + FormatF( accDir )
+ lr + FormatF( lrSign * accLeft )
+ ' | Down ' + FormatF( -accUp ) + ' m/s^2';
this.DrawText( g, txt );
var coriolisAccel = 2 * this.ObjSpeed * this.Omega * sin( latLong[0] );
clr = 'Right ';
clrSign = 1;
if (coriolisAccel <= -0.00005) {
clr = 'Left ';
clrSign = -1;
}
var bankAngle = -ToDeg( Math.atan( coriolisAccel / 9.806 ) );
var lr = 'Right ';
var lrSign = 1;
if (bankAngle <= -0.00005) {
lr = 'Left ';
lrSign = -1;
}
txt = 'Bank Angle ' + lr + FormatF( lrSign * bankAngle, '°', 4 )
+ ' | Coriolis Accel ' + clr + FormatF( clrSign * coriolisAccel, ' m/s^2', 4 );
g.SetTextColor( 'darkgreen' );
this.DrawText( g, txt );
} else {
// lat, long and distance in space coord system
var latLong = this.CartesicToPolar( this.GetPointOnTrack( limitAng, 0, transMat, this.R ) );
var txt = 'Space Lat ' + FormatDMS( ToDeg(latLong[0]) )
+ ' | Long ' + FormatDMS( ToDeg(latLong[1]) + this.StartLong )
+ ' | Distance ' + FormatF( dist/1000, ' km', 1 );
this.DrawText( g, txt, true );
// heading and speed
var ds = this.CompResolution;
var dt = ds * this.R / this.ObjSpeed;
var rot = (limitAng - ds) * oro;
var p0 = this.GetPointOnTrack( limitAng-ds, -earthRot+rot, transMat, this.R );
var rot = limitAng * oro;
var p1 = this.GetPointOnTrack( limitAng, -earthRot+rot, transMat, this.R );
var rot = (limitAng + ds) * oro;
var p2 = this.GetPointOnTrack( limitAng+ds, -earthRot+rot, transMat, this.R );
var dir = V3.Norm( V3.Sub( p2, p1 ) );
var up = V3.Norm( p1 );
var left = V3.Norm( V3.Mult( up, dir ) );
var dir = V3.Norm( V3.Mult( left, up ) ); // correct dir to be level at p1
var east = V3.Norm( V3.Mult( [0,0,1], up ) );
var north = V3.Norm( V3.Mult( up, east ) );
if (this.Debug) {
g.SetMarkerAttr( 'Circle', 6, 'red', 'red', 1 );
g.Marker3D( p0, 3 );
g.Marker3D( p1, 3 );
g.Marker3D( p2, 3 );
}
var planeHeading = V3.Angle( dir, north );
if (V3.ScalarProd( dir, east ) < 0) planeHeading = 2*Pi - planeHeading;
var speed = V3.Length( V3.Sub( p2, p1 ) ) / dt;
var forced = this.SpeedForced ? ' (forced)' : '';
txt = 'Speed ' + FormatF( speed, ' m/s', 2 ) + forced
+ ' | Heading ' + FormatF( ToDeg(planeHeading), '°', 2 )
+ ' (' + this.GetCompassLabel(ToDeg(planeHeading)) + ')';
this.DrawText( g, txt );
// acceleration
var accel = V3.Scale( V3.Sub( V3.Sub( p2, p1 ), V3.Sub( p1, p0 ) ), 1/(dt*dt) );
var accDir = V3.ScalarProd( dir, accel );
var accLeft = V3.ScalarProd( left, accel );
var accUp = V3.ScalarProd( up, accel );
var lr = ' | Left ';
var lrSign = 1;
if (accLeft < 0.00005) {
lr = ' | Right ';
lrSign = -1;
}
txt = 'Accel Forward ' + FormatF( accDir )
+ lr + FormatF( lrSign * accLeft )
+ ' | Down ' + FormatF( -accUp ) + ' m/s^2';
this.DrawText( g, txt );
}
}
},
GetPointOnTrack: function( angle, rot, transMat, radius ) {
var rotMat = rot != 0 ? M3.RotatingZ( rot, transMat ) : transMat;
var p = [ radius * cos(angle), 0, radius * sin(angle) ];
var pTrans = M3.Trans( rotMat, p );
return pTrans;
},
DrawPoly: function( g, color, width, alpha ) {
g.SetAlpha( alpha );
g.SetLineAttr( color, width );
g.DrawPoly3D( 1 )
g.SetAlpha( 1 );
},
DrawMarker: function( g, pos, symbol, size, color ) {
g.SetMarkerAttr( symbol, size, color, color, 1 );
g.Marker3D( pos, 3 );
},
DrawText: function( g, text, init ) {
var color = g.TextColor;
if (init) {
this.TextPosX = g.ScalePix(8);
this.TextPosY = g.ScalePix(40);
this.TextLineHeight = g.ScalePix(19);
color = 'black';
}
var oldTrans = g.SelectTrans( 'viewport' );
g.SetTextAttrS( 'Arial', 14, color, 'NW', 2 );
g.SetAreaAttr( 'white', 'white', 1 );
g.TextBox( text, this.TextPosX, this.TextPosY, 3 );
g.Text( text, this.TextPosX, this.TextPosY );
this.TextPosY += this.TextLineHeight;
g.SelectTrans( oldTrans );
},
SetEarthClipPlane: function( g ) {
// set clipping plane to hide path behind earth
var n = V3.Norm( V3.Sub( g.Camera.CamPos, [0,0,0] ) );
var r = V3.Norm( V3.Mult( [0,0,1], n ) );
var u = V3.Norm( V3.Mult( n, r ) );
g.AddClipPlane( [0,0,0], r, u );
},
DrawEarthMask: function( g ) {
// Draws the eath as a white circle to clip things behind it when drawing the earth transparent
var n = V3.Norm( V3.Sub( g.Camera.CamPos, [0,0,0] ) );
var r = V3.Norm( V3.Mult( [0,0,1], n ) );
var u = V3.Norm( V3.Mult( n, r ) );
g.SetPlane( [0,0,0], r, u );
g.SetAreaAttr( 'white', 'white', 1 );
g.CircleOnPlane( 0, 0, this.R, 3 );
},
CompSpaceGridClipPlaneOffset: function( g, spaceGridR ) {
// set clipping plane to hide space grid behind earth
var camDist = V3.Length( V3.Sub( g.Camera.CamPos, [0,0,0] ) );
var R2 = this.R * this.R;
var planeOffset = ( Math.sqrt( spaceGridR*spaceGridR - R2 ) * Math.sqrt( camDist*camDist - R2 ) - R2 ) / camDist;
return -planeOffset;
},
DrawCoordinateSystem: function( g ) {
var zOffset = -10200000;
var axisLen = 1800000;
var textOffset = 1.2 * axisLen;
g.SetAreaAttr( 'blue', 'blue', 1 );
g.SetMarkerSymbol( 'Arrow1' );
g.SetMarkerSize( 8 );
g.Arrow3D( 0, 0, zOffset, axisLen, 0, zOffset );
g.Arrow3D( 0, 0, zOffset, 0, axisLen, zOffset );
g.Arrow3D( 0, 0, zOffset, 0, 0, axisLen+zOffset );
g.SetTextAttrS( 'Arial', 12, 'black' );
g.Text3D( 'x', [ textOffset, 0, zOffset ] );
g.Text3D( 'y', [ 0, textOffset, zOffset ] );
g.Text3D( 'z', [ 0, 0, textOffset+zOffset ] );
g.ResetTrans3D();
if (g.Camera.CamPos[2] > 0) {
this.DrawEarthMask( g );
}
},
GetCompassLabel: function( heading ) {
// heading in degrees
var labels = [ 'N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N' ];
// adjust heading
var h = heading + 360/32;
var i = Math.trunc( h / (360/16) );
return labels[i];
},
CompCourseDeviation: function( objPos, time ) {
// uses additionally StartLat, StartLong, StartHeading and ObjSpeed
var aimPos = this.CompAimPos( time, this.R );
var dist = V3.Length( V3.Sub( objPos, aimPos ) );
// correct line of sight dist to corresponding great circle distance
var gcDeviation = 2 * this.R * asin( Limit1( dist / (2*this.R) ) );
return gcDeviation;
},
CompAimPos: function( time, radius ) {
// computes the position the object would be without Coriolis Effect
// calculate local coordinate system at start position
var startPos = this.PolarToCartesic( ToRad(this.StartLat), ToRad(this.StartLong)*0, this.R );
var up = V3.Norm( startPos );
var east = V3.Norm( V3.Mult( [0,0,1], up ) );
if (V3.Length(east) == 0) {
// at one if the poles
east = [0,1,0];
}
var north = V3.Norm( V3.Mult( up, east ) );
var heading = ToRad( this.StartHeading );
var dir = V3.Add( V3.Scale( north, cos(heading) ), V3.Scale( east, sin(heading) ) );
var gcNormal = V3.Norm( V3.Mult( up, dir ) );
var gcAngle = time * this.ObjSpeed / this.R;
var aimPos = V3.Scale( V3.Add( V3.Scale( up, cos(gcAngle) ), V3.Scale( dir, sin(gcAngle) ) ), radius );
return aimPos;
},
};
///////////////////////////////////////////////////////////////
var CoriolisGraph = NewGraphX3D( {
Id: 'CoriolisModelCanvas',
Width: '100%',
Height: '75%',
DrawFunc: function() { CoriolisModel.Draw(); },
AutoReset: true,
AutoClear: true,
AutoScalePix: true
} );
xOnLoad( function() { CoriolisModel.Init( CoriolisGraph ); } );
</jscript>
{{scroll}}
<jscript>
////////////////////////////////////////////////////////////////
ControlPanels.NewSliderPanel( {
Name: 'CoriolisModelSliders',
ModelRef: 'CoriolisModel',
OnModelChange: function( field ) { CoriolisModel.Update( field ); },
NCols: 1,
ValuePos: 'left',
Format: 'fix0',
Digits: 2,
PanelFormat: 'InputMediumWidth'
} ).AddRadiobuttonField( {
Name: 'FlightType',
ColSpan: 3,
ValueType: 'int',
NCols: 2,
Items: [
{
Text: 'Ballistic, no Friction',
Value: 0
}, {
Text: 'Controlled by Bank Angle',
Value: 1
}
]
} ).AddValueSliderField( {
Name: 'TrackDistance',
Label: 'Distance',
Color: 'black',
Min: 0,
Max: MaxTrackDistance,
LowerLimit: 0,
UpperLimit: 10 * MaxTrackDistance,
SnapTo: MaxTrackDistance / 2,
Mult: 1000,
Units: 'km',
Inc: 100,
} ).AddValueSliderField( {
Name: 'ObjSpeed',
Label: 'Speed',
Color: 'red',
Min: MinObjSpeed,
Max: MaxObjSpeed,
LowerLimit: MinObjSpeed,
UpperLimit: 10000,
SnapTo: 230,
Units: 'm/s',
Inc: 10,
} ).AddValueSliderField( {
Name: 'StartHeading',
Color: 'blue',
Min: 0,
Max: 360,
LowerLimit: 0,
UpperLimit: 360,
SnapTo: 180,
Units: '°',
Inc: 15,
} ).AddValueSliderField( {
Name: 'StartLat',
Color: 'green',
Min: -90,
Max: 90,
LowerLimit: -90,
UpperLimit: 90,
SnapTo: 0,
Units: '°',
Inc: 5,
} ).AddValueSliderField( {
Name: 'StartLong',
Color: 'green',
Min: -180,
Max: 180,
LowerLimit: -180,
UpperLimit: 180,
SnapTo: 0,
Units: '°',
Inc: 5,
} ).AddCheckboxField( {
Name: 'Display',
NCols: 4,
ColSpan: 3,
Items: [
{
Name: 'EnableData',
Text: 'Data',
}, {
Name: 'EnableSpaceGrid',
Text: 'Space Grid',
}, {
Name: 'EnableEarthGrid',
Text: 'Earth Grid',
}, {
Name: 'EnableAxes',
Text: 'Axes',
/*
}, {
Name: 'Debug',
Text: 'Debug',
*/
}
]
} ).Render();
</jscript>