Advertisement
Guest User

node_escape.ks

a guest
Mar 12th, 2016
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //brief node_escape.ks:  Creates a node which escapes the current parent SOI either prograde or retrograde wise.
  2. //
  3. //Warning: Assumes the current orbit is somewhat circular. Assumes you have somewhat the same inclination as the current parent
  4. //Script Parameters:
  5. //   par_direction, type string. Either prograde or retrograde. The direction you want to escape to
  6. //   par_velocity,  type scalar (m/s). The requested escape-velocity. if 0, we will calculate a "just enough" speed
  7. //   par_phase,     type scalar (deg). The angle you want to burn earlier/later.
  8. parameter par_direction is "prograde".
  9. parameter par_velocity is 0.
  10. parameter par_phase    is -45.
  11.  
  12. //Can't get the velocity of the current body directly, as its zero. But we can get the body:body and inverse it
  13. //If we want to escape retrograde, we must take the current body:body prograde and visa versa
  14. if (par_direction = "retrograde") {
  15.     set vec_escape to ship:body:body:prograde:vector:normalized.
  16. } else {
  17.     if not (par_direction = "prograde") {
  18.         //Can't quit scripts? So just return to default + warning.
  19.         print "Warning: par_direction requires either 'prograde' or 'retrograde'. Assuming prograde. Invalid arg: " + par_direction.
  20.     }
  21.     set vec_escape to ship:body:body:retrograde:vector:normalized.
  22. }  
  23.  
  24.  
  25. //Get current velocity and the cross product between the escape-vector and vec_velocity.
  26. set vec_vel   to ship:prograde:vector:normalized.
  27. set vec_cross to VCRS(vec_escape, vec_vel):normalized.
  28.  
  29. //Get the angle between the two velocity vectors
  30. set phase_deg to vectorangle(vec_escape, vec_vel).
  31.  
  32. //When the cross product flips the other way, we just missed the manouver node
  33. //Note that when traveling clockwise (orbit-wise) the cross-product is already upside down
  34. if (orbit:inclination<90 and vec_cross:y < 0) or (orbit:inclination>90 and vec_cross:y > 0) {
  35.     set phase_deg to 360-phase_deg.
  36. }
  37.  
  38. //We are inside the current body SOI for a while, so put the phase a bit earlier (WIP)
  39. //Seems to be related to the degrees the current parent moves around the parent:parent (in the escape time) + the amount of degrees you complete before escaping. So with a higher par_velocity, you need a lower par_phase and visa versa
  40. set phase_deg to phase_deg + par_phase.
  41. if (phase_deg < 0) {
  42.     set phase_deg to phase_deg+360.
  43. }
  44.  
  45. //Get eta in sec and print some info
  46. set eta_sec to (ship:orbit:period/360) * phase_deg.
  47. print "Phase: " + phase_deg.
  48. print "ETA  : " + eta_sec.
  49.  
  50. //If zero, calculate our own (seems a tad inaccurate)
  51. if(par_velocity = 0) {
  52.     set new_periapsis to (ship:apoapsis+ship:periapsis)/2.
  53.     set margin to 50 * 1000.
  54.     set new_semimajor to (new_periapsis+ship:body:radius + ship:body:soiradius+ship:body:radius + margin) / 2.
  55.  
  56.     set eta_curVel to velocityat(ship, time+eta_sec):orbit:mag.
  57.     set eta_reqVel to SQRT(ship:body:mu * (2/(new_periapsis+ship:body:radius) - 1/new_semimajor)).
  58.     set par_velocity to eta_reqVel - eta_curVel.
  59.     print "Calculated escape vel: " + par_velocity.
  60. }
  61.  
  62. //Create node
  63. set man_node to node((time+eta_sec):seconds, 0, 0, par_velocity).
  64. add man_node.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement