KSPModdingLibs / KSPCommunityFixes

Community patches for bugs in the KSP codebase.
https://github.com/KSPModdingLibs/KSPCommunityFixes/releases/latest
59 stars 20 forks source link

Activating timewarp can often cause a vessel to forget about SOI changes #258

Open JonnyOThan opened 1 month ago

JonnyOThan commented 1 month ago

In the attached saved game my current ship is about to encounter the Mun. If you activate timewarp, the intercept disappears. quicksave.zip

JonnyOThan commented 1 month ago

I've made a bit of progress here: GetDTForTrueAnomaly can sometimes return a number greater than one period (implying that it is not the earliest occurrence of that tA), and GetDTforTrueAnomalyAtUT can return a negative number.

The connection to timewarp is that when activating warp in the second half of your orbit, your mean anomaly is negative while ObTAtEpoch is positive. When not in timewarp, your epoch is constantly updating to the current UTC and ObT and ObTAtEpoch are the same.

GetDTForTrueAnomaly should subtract a whole number of periods such that the result is always in [0,period), and GetDTforTrueAnomalyAtUT tries to do this but actually ends up ROUNDING the number of periods instead of taking the floor, which can produce a negative number when it rounded up. Fixing these two issues makes the intercept work again in timewarp. HOWEVER the intercept when not in warp starts flickering. It looks like the very end of the intercept calculation is a gradient descent solver that runs over the entire synodic period of the two orbits. This seems excessive, because we should already know roughly where the close approaches are. I'd guess that the instability of the intercept is related to that, but I haven't dug in yet.