Skip to content

Commit

Permalink
Move Ellipticize and Circularize into MJLib
Browse files Browse the repository at this point in the history
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
  • Loading branch information
lamont-granquist committed Aug 30, 2023
1 parent 6aeee14 commit 127d704
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 196 deletions.
25 changes: 25 additions & 0 deletions MechJeb2/MechJebLib/Maneuvers/Simple.cs
Expand Up @@ -2,6 +2,8 @@
using MechJebLib.Core.TwoBody;
using MechJebLib.Primitives;
using MechJebLib.Utils;
using static System.Math;
using static MechJebLib.Utils.Statics;

namespace MechJebLib.Maneuvers
{
Expand Down Expand Up @@ -31,6 +33,29 @@ public static V3 DeltaVToCircularize(double mu, V3 r, V3 v)
return Maths.CircularVelocityFromHvec(mu, r, h) - v;
}

public static V3 DeltaVToEllipticize(double mu, V3 r, V3 v, double newPeR, double newApR)
{
double rm = r.magnitude;
// orbital energy
double e = -mu / (newPeR + newApR);
// orbital angular momentum
double l = Sqrt(Abs((Powi(e * (newApR - newPeR), 2) - mu * mu) / (2 * e)));
// orbital kinetic energy
double ke = e + mu / rm;

// radial/transverse as used in RSW, see Vallado
double vtransverse = l / rm;
double vradial = Sqrt(Abs(2 * ke - vtransverse * vtransverse));

V3 radialhat = r.normalized;
V3 transversehat = V3.Cross(V3.Cross(radialhat, v), radialhat).normalized;

V3 one = vtransverse * transversehat + vradial * radialhat - v;
V3 two = vtransverse * transversehat - vradial * radialhat - v;

return one.magnitude < two.magnitude ? one : two;
}

public static V3 DeltaVToCircularizeAfterTime(double mu, V3 r, V3 v, double dt)
{
Check.Finite(mu);
Expand Down
12 changes: 5 additions & 7 deletions MechJeb2/MechJebModuleFlightRecorder.cs
Expand Up @@ -180,15 +180,13 @@ public enum recordType
public int markBodyIndex = 1;

[ValueInfoItem("#MechJeb_MarkBody", InfoItem.Category.Recorder)] //Mark body
public string MarkBody() { return FlightGlobals.Bodies[markBodyIndex].bodyName; }
public string MarkBody() => FlightGlobals.Bodies[markBodyIndex].bodyName;

[ValueInfoItem("#MechJeb_DistanceFromMark", InfoItem.Category.Recorder, format = ValueInfoItem.SI, units = "m")] //Distance from mark
public double DistanceFromMark()
{
return Vector3d.Distance(VesselState.CoM,
public double DistanceFromMark() =>
Vector3d.Distance(VesselState.CoM,
FlightGlobals.Bodies[markBodyIndex].GetWorldSurfacePosition(markLatitude, markLongitude, markAltitude) -
FlightGlobals.Bodies[markBodyIndex].position);
}

[ValueInfoItem("#MechJeb_DownrangeDistance", InfoItem.Category.Recorder, format = ValueInfoItem.SI, units = "m")] //Downrange distance
public double GroundDistanceFromMark()
Expand Down Expand Up @@ -267,7 +265,7 @@ public override void OnFixedUpdate()

maxDragGees = Math.Max(maxDragGees, VesselState.drag / 9.81);

double circularPeriod = 2 * Math.PI * VesselState.radius / OrbitalManeuverCalculator.CircularOrbitSpeed(MainBody, VesselState.radius);
double circularPeriod = Orbit.CircularOrbitPeriod();
double angleTraversed = VesselState.longitude - markLongitude + 360 * (VesselState.time - markUT) / Part.vessel.mainBody.rotationPeriod;
phaseAngleFromMark = MuUtils.ClampDegrees360(360 * (VesselState.time - markUT) / circularPeriod - angleTraversed);

Expand Down Expand Up @@ -348,7 +346,7 @@ public void DumpCSV()
for (int idx = 0; idx <= historyIdx; idx++)
{
RecordStruct r = history[idx];
writer.Write(r[(recordType)0]);
writer.Write(r[0]);
for (int i = 1; i < typeCount; i++)
{
writer.Write(',');
Expand Down
2 changes: 1 addition & 1 deletion MechJeb2/MechJebModuleInfoItems.cs
Expand Up @@ -751,7 +751,7 @@ public string TimeToEquatorialDescendingNode()
}

[ValueInfoItem("#MechJeb_CircularOrbitSpeed", InfoItem.Category.Orbit, format = ValueInfoItem.SI, units = "m/s")] //Circular orbit speed
public double CircularOrbitSpeed() => OrbitalManeuverCalculator.CircularOrbitSpeed(MainBody, VesselState.radius);
public double CircularOrbitSpeed() => Orbit.CircularOrbitSpeed();

[Persistent(pass = (int)Pass.GLOBAL)]
public bool showStagedMass = false;
Expand Down

0 comments on commit 127d704

Please sign in to comment.