kOS-scripts/lib/orbital_equations.ks

72 lines
2.2 KiB
Plaintext
Raw Normal View History

@LAZYGLOBAL OFF.
2018-07-27 21:32:14 +02:00
function semi_major_axis {
//
// Semi-major axis
// Based on:
// https://en.wikipedia.org/wiki/Semi-major_and_semi-minor_axes
// http://www.orbiterwiki.org/wiki/Front_Cover_Equations
//
parameter ap is APOAPSIS.
parameter pe is PERIAPSIS.
2018-07-27 21:32:14 +02:00
return (ap + pe + 2*BODY:radius) / 2. // Add 2x body radius because KSP measures apoapsis/periapsis from the surface
2018-07-27 21:32:14 +02:00
}
function orbital_period {
//
// The orbital period.
// Note that for all ellipses with a given semi-major axis, the orbital period is the same, disregarding their eccentricity.
// Based on:
// https://en.wikipedia.org/wiki/Semi-major_and_semi-minor_axes#Astronomy
// http://www.orbiterwiki.org/wiki/Front_Cover_Equations
//
parameter sme is semi_major_axis().
2018-07-27 21:32:14 +02:00
return 2 * CONSTANT:PI * sqrt(sme^3 / BODY:mu).
2018-07-27 21:32:14 +02:00
}
function orbital_period_from_ap_pe {
parameter ap, pe.
return orbital_period(semi_major_axis(ap, pe)).
2018-07-27 21:32:14 +02:00
}
function orbital_velocity {
2018-07-27 21:32:14 +02:00
//
// The required velocity to maintain orbit at provided altitude.
// Based on:
// https://en.wikipedia.org/wiki/Orbital_speed#Precise_orbital_speed
// http://www.orbiterwiki.org/wiki/Front_Cover_Equations
// https://en.wikipedia.org/wiki/Vis-viva_equation
parameter altitude.
parameter sma is semi_major_axis().
2018-07-27 21:32:14 +02:00
return sqrt(BODY:mu * ((2 / (altitude+BODY:radius)) - (1 / sma))). // add body radius because KSP measures altitude from the surface
2018-07-27 21:32:14 +02:00
}
function orbital_velocity_from_ap_pe {
parameter altitude, ap, pe.
return orbital_velocity(altitude, semi_major_axis(ap, pe)).
2018-07-27 21:32:14 +02:00
}
2019-02-08 23:18:34 +01:00
function orbital_velocity_circular {
parameter altitude.
return orbital_velocity_from_ap_pe(altitude, altitude, altitude).
}
2018-07-27 21:32:14 +02:00
function orbital_eccentricity {
//
// The orbital eccentricity, i.e. the ratio by which the orbit deviates from a perfect circle, where e=0 is circular.
// Based on:
// https://en.wikipedia.org/wiki/Orbital_eccentricity#Calculation
// http://www.orbiterwiki.org/wiki/Front_Cover_Equations
parameter ap is APOAPSIS.
parameter pe is PERIAPSIS.
return (ap - pe) / (ap + pe).
}