function hohmannDeltaV { parameter r1 is 1, r2 is 1, mu is 1. local vCirc to sqrt(mu / r1). local vHohmann to sqrt(mu * (2 / r1 - 2 / (r1 + r2))). return vHohmann - vCirc. } function angleBetween { parameter posA to V(1, 1, 0), posB to V(1, 1, 0). local angle to arcTan2(posB:y, posB:x) - arcTan2(posA:y, posA:x). if angle < 0 set angle to angle + 2 * constant:pi. return angle. } function hohmannTransfer { // parameter targetPeriapsis is 60000. parameter tgtBody is Mun. // get the approximate required deltav for a transfer local r1 to ship:orbit:semimajoraxis. local r2 to tgtBody:orbit:semimajoraxis. local vTransfer to hohmannDeltaV(r1, r2, kerbin:mu). // compute the transfer time local transferSMA to (r1 + r2) / 2. local transferPeriod to 2 * constant:pi * sqrt(transferSMA^3 / kerbin:mu). local transitTime to transferPeriod / 2. // get lead angle (how far ahead the tgtBody must be) local tgtAV to 2 * constant:pi / tgtBody:orbit:period. local leadAngle to tgtAV * transitTime. local transferAngle to constant:pi - leadAngle. // find the next time the phase angle condition is satisfied local shipAV to 2 * constant:pi / ship:orbit:period. local relativeAV to tgtAV - shipAV. // get the phase angle to the target local bodyToShip to (ship:position - body:position):normalized. local obtNorm to vCrs(bodyToShip, ship:velocity:orbit:normalized):normalized. local bodyToTgt to vXcl(obtNorm, (tgtBody:position - body:position):normalized):normalized. local signVec to vCrs(obtNorm, bodyToShip):normalized. local phaseSign to vDot(bodyToTgt, signVec). local currentPhaseAngle to vAng(ship:position - body:position, tgtBody:position - body:position) * (constant:pi / 180). if phaseSign < 0 set currentPhaseAngle to -1 * currentPhaseAngle + 2 * constant:pi. // get the time to start the maneuver local phaseError is transferAngle - currentPhaseAngle. if phaseError < 0 set phaseError to phaseError + 2 * constant:pi. local waitTime to phaseError / relativeAV. if waitTime < 0 set waitTime to waitTime + ship:orbit:period. set nd to node(time:seconds + waitTime, 0, 0, vTransfer). add nd. // print "Transfer DeltaV (m/s) : " + round(vTransfer, 2). // print "Transit Time (s) : " + round(transitTime*57.3, 0). // print "Lead Angle (deg) : " + round(transferAngle*57.3, 2). // print "Relative AV (deg/s) : " + relativeAV*57.3. // print "Phase Angle (deg) : " + round(currentPhaseAngle*57.3, 2). // print "Phase Error (deg) : " + round(phaseError*57.3, 2). // print "Wait Time (s) : " + round(waitTime, 0). } local g is GUI(300). set g:x to 100. set g:y to 100. local titleLabel is g:addlabel("Transfer to which body?"). set titleLabel:style:align to "center". set titleLabel:style:hstretch to true. local popup is g:addpopupmenu(). set popup:optionsuffix to "NAME". for localMoon in body:orbitingchildren popup:addoption(localMoon). popup:changed. // print popup:options. // print popup:changed. g:show(). wait until popup:changed. // print popup:value. g:dispose(). hohmannTransfer(popup:value).