kOS-scripts/lib/equations.ks

67 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 apoapsis is ORBIT:APOAPSIS.
parameter periapsis is ORBIT:PERIAPSIS.
return (apoapsis + periapsis + 2*BODY:RADIUS) / 2. // Add 2x body radius because KSP measures apoapsis/periapsis from the surface
}
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 semi_major_axis is semi_major_axis().
return 2 * CONSTANT:PI * SQRT(semi_major_axis^3 / BODY:MU).
}
function orbital_period_from_ap_pe {
parameter apoapsis, periapsis.
return 2 * CONSTANT:PI * SQRT(semi_major_axis(apoapsis, periapsis)^3 / BODY:MU).
}
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, apoapsis, periapsis.
return orbital_velocity(altitude, semi_major_axis(apoapsis, periapsis)).
}
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 apoapsis is ORBIT:APOAPSIS.
parameter periapsis is ORBIT:PERIAPSIS.
2018-07-27 21:32:14 +02:00
return (apoapsis - periapsis) / (apoapsis + periapsis).
}