From 64da2d1c8abd564010a91db5295ecf27563426fb Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Thu, 26 Oct 2023 14:02:54 -0700 Subject: [PATCH] Fix stock burn termination and bring back MANEUVER_COT Still don't know if MANEUVER_COT works, but it is back. The expression: Quaternion.FromToRotation(VesselState.forward, VesselState.thrustForward) * VesselState.forward Looks a little useless but FromToRotation returns identity when thrustForward is [0,0,0] which fixes things for the non-thrust case. More importantly this fixes stock-style burn termination to work correctly again and it measure the angle to the actual maneuver node rather than the angle to the frozen inertial direction (which really shouldn't have worked at all, dunno why it eventually did stop). Signed-off-by: Lamont Granquist --- MechJeb2/MechJebModuleNodeExecutor.cs | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/MechJeb2/MechJebModuleNodeExecutor.cs b/MechJeb2/MechJebModuleNodeExecutor.cs index 8c7a5971..5adce55e 100644 --- a/MechJeb2/MechJebModuleNodeExecutor.cs +++ b/MechJeb2/MechJebModuleNodeExecutor.cs @@ -109,7 +109,7 @@ private enum Mode { ONE_NODE, ONE_PNODE, ALL_NODES } private Mode _mode = Mode.ONE_NODE; public bool BurnTriggered; - private double _dvLeft; // for Principia + private double _dvLeft; // for Principia private Vector3d _direction; // de-rotated world vector public override void Drive(FlightCtrlState s) => HandleUllage(s); @@ -152,7 +152,7 @@ public override void OnFixedUpdate() return; } - Core.Attitude.attitudeTo(Planetarium.fetch.rotation * _direction, AttitudeReference.INERTIAL, this); + Core.Attitude.attitudeTo(Planetarium.fetch.rotation * _direction, AttitudeReference.INERTIAL_COT, this); double ignitionUT = CalculateIgnitionUT(node); @@ -172,10 +172,10 @@ public override void OnFixedUpdate() { //check if we've finished a node: ManeuverNode node = Vessel.patchedConicSolver.maneuverNodes[0]; - _dvLeft = node.GetBurnVector(Orbit).magnitude; + _dvLeft = node.GetBurnVector(Orbit).magnitude; _direction = NextDirection(); - if (BurnTriggered && AngleFromTarget() >= 0.5 * PI) + if (BurnTriggered && AngleFromNode() >= 0.5 * PI) { BurnTriggered = false; @@ -199,7 +199,7 @@ public override void OnFixedUpdate() } } - Core.Attitude.attitudeTo(Planetarium.fetch.rotation * _direction, AttitudeReference.INERTIAL, this); + Core.Attitude.attitudeTo(Planetarium.fetch.rotation * _direction, AttitudeReference.INERTIAL_COT, this); double ignitionUT = CalculateIgnitionUT(node); @@ -217,9 +217,16 @@ public override void OnFixedUpdate() } } - private double AngleFromTarget() + private double AngleFromNode() { - Vector3d fwd = VesselState.forward.normalized; + Vector3d fwd = Quaternion.FromToRotation(VesselState.forward, VesselState.thrustForward) * VesselState.forward; + Vector3d dir = Vessel.patchedConicSolver.maneuverNodes[0].GetBurnVector(Orbit).normalized; + return SafeAcos(Vector3d.Dot(fwd, dir)); + } + + private double AngleFromDirection() + { + Vector3d fwd = Quaternion.FromToRotation(VesselState.forward, VesselState.thrustForward) * VesselState.forward; Vector3d dir = (Planetarium.fetch.rotation * _direction).normalized; return SafeAcos(Vector3d.Dot(fwd, dir)); } @@ -273,8 +280,8 @@ private void HandleAutowarp(double ignitionUT) } if (MuUtils.PhysicsRunning() - ? AngleFromTarget() < Deg2Rad(1) && Core.vessel.angularVelocity.magnitude < 0.001 - : AngleFromTarget() < Deg2Rad(2)) + ? AngleFromDirection() < Deg2Rad(1) && Core.vessel.angularVelocity.magnitude < 0.001 + : AngleFromDirection() < Deg2Rad(2)) Core.Warp.WarpToUT(ignitionUT - 3); else Core.Warp.MinimumWarp();