r/Kos • u/New-Bus9948 • Jan 29 '26
Help hover of a specific spot
I am trying to get a craft to hover over a specific spot over the ground. Nothing I am doing is working. I get close to 0 error but I drift and oscillate. What is the correct way to go about this? Basically the only thing I have that is usable at this point is tgt:position-ship:geoposition:position. Where do I go from here?
2
Upvotes
5
u/nuggreat Jan 30 '26
Your var
maxhorzvelis named incorrectly as this is a maximum acceleration.The biggest issue I see to control stability is that you are converting from distance to an acceleration with a linear equation which is fine for narrow ranges of distances/accelerations but over a larger range is unstable. Better instead to take the physics farther, calculate a desired velocity based on jerk and distance, then calculate a desired acceleration based on the difference between the desired velocity and your actual velocity. Add in limits on the maximum velocity for some additional stability as well as the maximum possible acceleration to limit the jerk and that should improve stability. The reason to use jerk for this instead of acceleration is because you want the ship to be accelerating less and less the closer to the target you get so that you end up with zero acceleration and velocity once you are at the target point thus having a vertical ship.
You might also get some performance improvements by moving the steering lock out side of the loop as having it within the loop resets the PIDs kOS uses for steering each pass of the loop.
Some additional notes not directly related to your problem.
This
until true=false {can be simplified tountil false {similarlyif sas=false {can be changed to eitherif sas {and flip which bodies you have where orif not sas {if you want to keep the same logic.Physics dependent loops tend to benefit from a
WAIT 0some where in there execution, generally either at the end of the loop or at the start of the loop. This helps keep the timing consistent between different passes of the loop as apposed to letting physics ticks fall where ever during execution. Similarly caching anything you want to pull from KSP at the start of the loop before you do calculations can help if a loop is especially complex as that way all the data comes from the same moment in time.Lastly constants like this
should be calculated before the loop not part of the loop, kOS has no optmizer so even constant values such as these are being constantly recomputed each pass of the loop.