Advertisement
japsert

Trajectory prediction

Aug 20th, 2023 (edited)
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | Science | 0 0
  1. @lazyGlobal off.
  2. clearScreen.
  3. clearVecDraws().
  4.  
  5. // Time step in seconds
  6. global DT is 1.
  7. print "dT: " + DT + "s" at (0, 0).
  8.  
  9. // Draws a vector from pos to pos + vec
  10. function drawDebugVec {
  11. local parameter pos.
  12. local parameter alt_.
  13. local parameter vecToNewPos.
  14. local parameter i.
  15.  
  16. vecDraw(
  17. { return pos:altitudePosition(alt_). }, vecToNewPos,
  18. red, i, 1, true
  19. ).
  20. }
  21.  
  22. // Initial burn
  23. stage.
  24. lock throttle to 1.
  25. lock steering to heading(90, 80, 270).
  26. wait until ship:altitude > 1000.
  27. lock throttle to 0.
  28.  
  29. // Wait 5 ticks to make sure throttle delay doesn't influence acceleration
  30. wait 0.1.
  31. // Draw debug vectors for the first loop
  32. local drawDebugVectors is true.
  33.  
  34. until false {
  35. local pos is ship:geoPosition.
  36. local alt_ is ship:altitude.
  37. local velVec is ship:velocity:surface.
  38.  
  39. local i is 1.
  40. until alt_ <= pos:terrainheight {
  41. // Gravitational acceleration at current pos/alt
  42. local g is body:mu / (body:radius + alt_)^2.
  43. local gravForce is g * ship:mass. // kN
  44. local gravForceVec is gravForce * -ship:up:vector.
  45. local accVec is gravForceVec / ship:mass.
  46.  
  47. // Vector from this iteration's pos/alt to next iteration's pos/alt
  48. local positionChangeVec is velVec * DT + 1/2 * accVec * DT^2.
  49. // Vector from ship to next iteration's pos/alt, to calculate
  50. // newPos and newAlt below
  51. local vecToNewPos is pos:altitudePosition(alt_) + positionChangeVec.
  52.  
  53. // Update position, altitude and velocity, accounting for the
  54. // curvature of Kerbin
  55. local newPos is body:geoPositionOf(vecToNewPos).
  56. local newAlt is body:altitudeOf(vecToNewPos).
  57. local newVelVec is velVec + accVec * DT.
  58.  
  59. if drawDebugVectors drawDebugVec(pos, alt_, positionChangeVec, i).
  60.  
  61. set i to i + 1.
  62. // Set variables for next iteration
  63. set pos to newPos.
  64. set alt_ to newAlt.
  65. set velVec to newVelVec.
  66. }
  67. // Finished first loop, stop drawing debug vectors
  68. set drawDebugVectors to false.
  69.  
  70. print "reached ground in " + i + " iterations" at (0, 1).
  71.  
  72. wait 0.
  73. }
  74.  
Tags: KSP KOS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement