Advertisement
Guest User

Untitled

a guest
Jul 28th, 2014
2,470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. //Missile by Peter Sharpe
  2. //For KSP with the kOS mod
  3.  
  4. //YOU MUST MANUALLY TARGET YOUR TARGET BEFORE EXECUTING THIS PROGRAM.
  5.  
  6. //Editable Parameters
  7. set explosionradius to 8.
  8. set DrawVectors to True.
  9.  
  10. //Don't edit these unless you really understand what you're doing!
  11. set iterations to 5.
  12. set initialguess to 15.
  13. set gravcorrection to 0.5.
  14. set dragcorrection to 1.15.
  15.  
  16. //Line 26
  17.  
  18. Clearscreen.
  19.  
  20. //Do Targeting
  21. print "Enemy heat signature acquired. Launching...".
  22.  
  23. //Launch Sequence
  24. stage.
  25. lock throttle to 1.
  26. lock steering to up.
  27. wait until alt:radar>100 or velocity:surface:mag>100.
  28. print "Launch sequence completed. Now tracking target.".
  29.  
  30. //Set up Staging
  31. when ship:maxthrust=0 then {stage. wait 0.01. If ship:maxthrust>0 {preserve.}.}.
  32.  
  33. //Set up outputs
  34. print "Time to interception:" at (0,10).
  35. print "Distance to Target:" at (0,12).
  36. print "Steering Error:" at (0,14).
  37.  
  38. //Take steering control, lock it to "steeringangle"
  39. set steeringangle to target:direction.
  40. sas off.
  41. rcs on.
  42. lock steering to steeringangle.
  43.  
  44. //Arm the warhead to trigger with radius of "explosionradius" + 1 frame to decouple.
  45. when t<explosionradius/50+0.05 or target:distance<50 then {stage.}.
  46. print "Warhead armed. Initiating homing algorithm.".
  47.  
  48. //Homing Algorithm, infinite loop
  49. set t to 10.
  50. until 0
  51. {
  52. set rpos to (0-1)*(target:Position).
  53. set rvel to (ship:velocity:surface-target:velocity:orbit).
  54. set gravfactor to gravcorrection.
  55. if target:loaded {set rvel to ship:velocity:surface-target:velocity:surface.}.
  56. if target:altitude>25000 {set rvel to ship:velocity:orbit-target:velocity:orbit.}.
  57. set amag to ship:maxthrust/(ship:mass*9.81).
  58.  
  59. //Solve for t, where 0=at^4+bt^2+ct+d. First get coefficients
  60. set a to 0-((amag)^2)/4.
  61. set b to (rvel:sqrmagnitude).
  62. set c to 2*(rvel*rpos).
  63. set d to (rpos:sqrmagnitude).
  64.  
  65. //Do a few Newton-Raphson iterations:
  66. set timeguesses to list().
  67. set timeguesses:add to initialguess.
  68. set position to 0.
  69. until position>=iterations
  70. {
  71. set timeguesses:add to timeguesses[position]-(a*timeguesses[position]^4+b*timeguesses[position]^2+c*timeguesses[position]+d)/(4*a*timeguesses[position]^3+2*b*timeguesses[position]+c).
  72. set position to position+1.
  73. }.
  74. set initialguess to abs(timeguesses[iterations]).
  75.  
  76. //Then calculate your desired direction. There is a correction for drag.
  77. set t to abs(timeguesses[iterations])/dragcorrection.
  78. if altitude>25000 {set t to abs(timeguesses[iterations]). set gravfactor to 0.}.
  79. print t at (22,10).
  80. print target:distance at (20,12).
  81. set steeringvector to v((0-1)*(rpos:x+(rvel:x)*t),(0-1)*(rpos:y+(rvel:y)*t),(0-1)*(rpos:z+(rvel:z)*t)).
  82. set correctedsteeringvector to (9.81*amag*steeringvector:normalized+9.81*gravfactor*(up:vector)).
  83. set steeringangle to correctedsteeringvector:direction.
  84. print vectorangle(steeringvector,ship:facing:vector) at (16,14).
  85.  
  86. //Draw Vectors
  87. if DrawVectors=True {
  88. set Steerdraw TO VECDRAWARGS(v(0,0,0), 9.81*v((0-2)*(rpos:x+(rvel:x)*t)/(t^2),(0-2)*(rpos:y+(rvel:y)*t)/(t^2),(0-2)*(rpos:z+(rvel:z)*t)/(t^2)), rgb(0.5,1,0.5),"Algo. Output", 1, true ).
  89. set UPdraw TO VECDRAWARGS(9.81*v((0-2)*(rpos:x+(rvel:x)*t)/(t^2),(0-2)*(rpos:y+(rvel:y)*t)/(t^2),(0-2)*(rpos:z+(rvel:z)*t)/(t^2)), 9.81*gravfactor*(up:vector), rgb(0.5,0.5,1),"Grav. Corr.", 1, true ).
  90. set Vectordraw TO VECDRAWARGS(v(0,0,0), correctedsteeringvector:normalized, rgb(1,0.5,0.5),"Sum", 12.0, true ).
  91. wait 0.01.
  92. }.
  93. }.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement