Skip to content

Instantly share code, notes, and snippets.

@jberkman
Created January 7, 2018 06:57
Show Gist options
  • Save jberkman/451d8367f07650ea20b313d74b1d030e to your computer and use it in GitHub Desktop.
Save jberkman/451d8367f07650ea20b313d74b1d030e to your computer and use it in GitHub Desktop.
@lazyglobal off.
function gravMag {
parameter alt,bdy.
return bdy:mu/(bdy:radius+altitude)^2.
}
function gravVec {
parameter self is ship.
return gravMag(self:altitude,self:obt:body)*-self:up:vector:normalized.
}
function thrustVec {
parameter self is ship.
return self:availableThrust*-self:facing:vector:normalized.
}
function maxMag {
parameter vec, mag.
if vec:mag<mag return vec.
return mag*vec:normalized.
}
clearVecDraws().
local posVec is vecDraw(V(0,0,0),V(0,0,0),red,"POS",1,true,0.2).
local velVec is vecDraw(V(0,0,0),V(0,0,0),green,"VEL",1,true,0.2).
local accVec is vecDraw(V(0,0,0),V(0,0,0),blue,"ACC",1,true,0.2).
local launchPad is LatLng(-0.0971570496595165,-74.557757572037).
local landingPad is LatLng(-0.0967218940930756,-74.6173570891441).
lock throttle to 0.
if ship:availableThrust=0 stage.
sas off.
rcs on.
local waypt is 0.
until waypt>0 and status="LANDED"{
local tgtPos is 0.
if waypt=0 set tgtPos to launchPad:altitudePosition(landingPad:terrainHeight+100).
else if waypt=1 set tgtPos to landingPad:altitudePosition(landingPad:terrainHeight+100).
else if waypt=2 set tgtPos to landingPad:altitudePosition(landingPad:terrainHeight).
local gAccV is gravVec().
local tgtVel is maxMag(tgtPos,7.5).
local tgtAcc is maxMag(tgtVel-ship:velocity:surface,7.5).
local tAccV is thrustVec()/ship:mass.
local netAcc is -tgtAcc+gAccV.
print tgtPos:mag.
//print netAcc:mag.
lock steering to lookDirUp(-netAcc,-ship:north:vector).
local x is vDot(tAccV,netAcc).
if x<>0 lock throttle to netAcc:vec:mag^2/x. //vDot(tAccV,netAcc).
//print throttle*thrust:mag/gAccV:mag.
//print round(cos(vang(up:vector,facing:vector)),4).
//print throttle*tAccV+gAccV.
set posVec:vec to tgtPos.
set velVec:vec to tgtVel.
set accVec:vec to tgtAcc.
wait 0.1.
if tgtPos:mag<1 set waypt to waypt+1.
}
lock throttle to 0.
unlock steering.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment