Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- SimpleKSPOrbitalMechanics is a purely calculation based lua library, with some constants hardcoded for Kerbal Space Program
- -- v0.001
- -- to use, put
- -- require "SimpleKSPOrbitalMechanics"
- -- in your lua script, or run it with
- -- dofile("SimpleKSPOrbitalMechanics")
- -- Original Work Copyright 2012, Nadrek, insofar as any elements that aren't obvious, trivial, or mathematics are concerned (none of which are copyrightable)
- -- licensed under your choice of GPLv2, GPLv3, or Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) http://www.gnu.org/licenses/gpl-2.0.html or http://www.gnu.org/licenses/gpl.html or http://creativecommons.org/licenses/by-sa/3.0/
- -- KSP specific functions will have KSP in the name; other functions will not, and can be used for real life calculations as well.
- -- This script requires SimpleOrbitalMechanics to do the heavy mathematical lifting.
- -- This script contains KSP specific functions and calls useful to and reliant on Kerbal Space Program, MechJeb, and MechJeb's Autom8 module.
- require "SimpleOrbitalMechanics"
- function globalSkomKSPMainBodyResetBasedOnGuessing()
- -- prints body it guessed
- -- also sets the globalMainBody... variables to that body (or all to -1
- -- if the body is UNKNOWN)
- -- Essentially, this function, if called while in orbit around a given body (like the Mun, or Kerbin)
- -- sets some "background" variables so the rest of the functions know how to act based on what
- -- you're orbiting; you use different values for the Min and for Kerbin, because Kerbin is bigger,
- -- has more gravity, has an atmosphere, and has a much larger Hill sphere of influence.
- -- NOTE: Landed, on the launchpad, in ascent, in descent all count as being "in orbit"!
- -- no return value
- local GuessedMainBody = guessSkomKSPWhatBodyIsTheMainBodyCurrentlyBeingOrbited()
- globalSomMainBodyReset(GuessedMainBody)
- print GuessedMainBody
- end
- function guessSkomKSPWhatBodyIsTheMainBodyCurrentlyBeingOrbited()
- -- returns the SimpleOrbitalMechanics name of the body we think we're orbiting.
- -- suitable to pass to globalSomMainBodyReset, or returns UNKNOWN if it cannot tell
- --[[ Ex:
- print(guessSkomKSPWhatBodyIsTheMainBodyCurrentlyBeingOrbited())
- ]]
- -- NOTE: All warnings and notes for guessSomWhatBodyIsTheMainBodyFromaOvAltaslMoe apply.
- return guessSomWhatBodyIsTheMainBodyFromaOvAltaslMoe(vessel.orbitSemiMajorAxis, vessel.speedOrbital, vessel.altitudeASL, 0.01)
- end
- function calcSkomMolniyaApPeFromMainBodyCurrentlyBeingOrbitedFromMoe(MarginOfError)
- -- MarginOfError 0.001 means allow 0.1% for a "safety zone" above the minimum stable altitude, or below the Hill sphere of influence.
- -- Do this if you want to just barely scrape by!
- -- 2 means allow a 200% "safety zone" above the minimum stable altitude, or below the Hill sphere of influence.
- -- Do this if you want to have a much less eccentric orbit - perhaps you want to keep certain orbits clear for other uses!
- -- If in doubt, try setting MarginOfError to 0.1
- -- Returns Ap and Pe of the best possible Molniya orbit for a given body
- -- Returns -1, -1 if no stable Molniya orbit is possible
- -- REQUIRES globalSomMainBodyReset to have been called properly
- -- the simple way is to run globalSkomKSPMainBodyResetBasedOnGuessing() first!
- --[[ Ex:
- do
- local localAp, localPe = calcSkomMolniyaApPeFromMainBodyCurrentlyBeingOrbitedFromMoe(0.1)
- print("Your Apoapsis should be " .. localAp .. " meters and your Periapsis should be " .. localPe .. " meters")
- print(" For a classical Molniya orbit, use an inclination of 63.4, 116.6, -63.4, or -116.6")
- print(" These values are chosen because oblate planets (like the Earth's fat equatorial bulge")
- print(" would change your orbit with other inclinations, since the planet isn't a sphere")
- end
- ]]
- -- For oblate celestial bodies, of course, the inclination of the actual orbit must be 63.4 or 116.6 degrees, so the effect of the oblate equitorial belt is nullified.
- -- Now we call our standard Molniya calculator, and pass in numbers based on the celestial body we claim we're currently in orbit of!
- return calcSomMolniyaApPeFromHsoiMsaMbrGmSrpMoe(globalSomMainBodyHillSphereOfInfluence, globalSomMainBodyMinStableAltitude, globalSomMainBodyEquatorialRadius, globalSomMainBodyGM, globalSomMainBodySiderealRotationalPeriod, MarginOfError)
- end
- function printSkomKSPCurrentOrbitalElements()
- print("orbitApA " .. vessel.orbitApA)
- print("orbitPeA " .. vessel.orbitPeA)
- print("orbitPeriod " .. vessel.orbitPeriod)
- print("orbitTimeToAp " .. vessel.orbitTimeToAp)
- print("orbitTimeToPe " .. vessel.orbitTimeToPe)
- print("orbitLAN (RA/Long of Ascending Node) " .. vessel.orbitLAN)
- print("orbitArgumentOfPeriapsis " .. vessel.orbitArgumentOfPeriapsis)
- print("orbitInclination " .. vessel.orbitInclination)
- print("orbitEccentricity " .. vessel.orbitEccentricity)
- print("orbitSemiMajorAxis (a) " .. vessel.orbitSemiMajorAxis)
- print("speedOrbital " .. vessel.speedOrbital)
- print("speedSurface " .. vessel.speedSurface)
- print("speedVertical " .. vessel.speedVertical)
- print("speedHorizontal " .. vessel.speedHorizontal)
- print("altitudeASL " .. vessel.altitudeASL)
- print("altitudeTrue " .. vessel.altitudeTrue)
- print("altitudeBottom " .. vessel.altitudeBottom)
- print("latitude " .. vessel.latitude)
- print("longitude " .. vessel.longitude)
- print("time " .. vessel.time)
- end
- function helpSkomSimpleKSPOrbitalMechanics()
- print "Usage: globalSkomKSPMainBodyResetBasedOnGuessing()"
- print " prints body it guessed"
- print " also sets the globalMainBody... variables to that body (or all to -1"
- print " if the body is UNKNOWN)"
- print " Essentially, this function, if called while in orbit around a given body (like the Mun, or Kerbin)"
- print " sets some \"background\" variables so the rest of the functions know how to act based on what"
- print " you're orbiting; you use different values for the Min and for Kerbin, because Kerbin is bigger,"
- print " has more gravity, has an atmosphere, and has a much larger Hill sphere of influence."
- print " NOTE: Landed, on the launchpad, in ascent, in descent, and crashing all count as being \"in orbit\"!"
- print " no return value"
- print "Usage: guessSkomKSPWhatBodyIsTheMainBodyCurrentlyBeingOrbited()"
- print " returns the SimpleOrbitalMechanics name of the body we think we're orbiting."
- print " suitable to pass to globalSomMainBodyReset, or returns UNKNOWN if it cannot tell."
- print " Ex:"
- print " print(guessSkomKSPWhatBodyIsTheMainBodyCurrentlyBeingOrbited())"
- print "Usage: calcSkomMolniyaApPeFromMainBodyCurrentlyBeingOrbitedFromMoe(MarginOfError)"
- print " MarginOfError 0.001 means allow 0.1% for a \"safety zone\" above the minimum stable altitude, or below the Hill sphere of influence."
- print " Do this if you want to just barely scrape by!"
- print " 2 means allow a 200% \"safety zone\" above the minimum stable altitude, or below the Hill sphere of influence."
- print " Do this if you want to have a much less eccentric orbit - perhaps you want to keep certain orbits clear for other uses!"
- print " If in doubt, try setting MarginOfError to 0.1"
- print " Returns Ap and Pe of the best possible Molniya orbit for a given body"
- print " Returns -1, -1 if no stable Molniya orbit is possible"
- print " REQUIRES globalSomMainBodyReset to have been called properly"
- print " the simple way is to run globalSkomKSPMainBodyResetBasedOnGuessing() first!"
- print " Ex:"
- print " do"
- print " local localAp, localPe = calcSkomMolniyaApPeFromMainBodyCurrentlyBeingOrbitedFromMoe(0.1)"
- print " print(\"Your Apoapsis should be \" .. localAp .. \" meters and your Periapsis should be \" .. localPe .. \" meters\")"
- print " print(\" For a classical Molniya orbit, use an inclination of 63.4, 116.6, -63.4, or -116.6\")"
- print " print(\" These values are chosen because oblate planets (like the Earth's fat equatorial bulge\")"
- print " print(\" would change your orbit with other inclinations, since the planet isn't a sphere\")"
- print " end"
- print " For oblate celestial bodies, of course, the inclination of the actual orbit must be 63.4 or 116.6 degrees, so the effect of the oblate equitorial belt is nullified."
- print "Usage: printSkomKSPCurrentOrbitalElements()"
- print " Prints important KSP elements for human use or records"
- end
- globalSkomKSPMainBodyResetBasedOnGuessing() -- initalize main body dependent variables based on guessing what body we're orbiting.
- print "Usage: helpSkomSimpleKSPOrbitalMechanics() gives detailed help on all Skom functions."
- print " Skom functions relate to Kerbal Space Program (KSP) specific functionality."
- print("If it helps, I'm guessing you are currently orbiting " .. globalSomMainBodyName)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement