Skip to content

Commit

Permalink
Fix stock burn termination and bring back MANEUVER_COT
Browse files Browse the repository at this point in the history
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 <lamont@scriptkiddie.org>
  • Loading branch information
lamont-granquist committed Oct 26, 2023
1 parent d7a69e0 commit 64da2d1
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions MechJeb2/MechJebModuleNodeExecutor.cs
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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;

Expand All @@ -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);

Expand All @@ -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));
}
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 64da2d1

Please sign in to comment.