Skip to content

Commit

Permalink
[CL] Added new entries
Browse files Browse the repository at this point in the history
- Target
  - Target_ClosestApproachDistance (viewable from Flight view also)
  - Target_ClosestApproachTime (viewable from Flight view also)
  - Target_ClosestApproachRelativeSpeed (viewable from Flight view also)
  requested by Biggen & alex-sherwin
  • Loading branch information
Falki-git committed Feb 20, 2024
1 parent 7d3fafc commit 73343b6
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 4 deletions.
121 changes: 117 additions & 4 deletions src/MicroEngineer/Entries/TargetEntries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ public DistanceAtCloseApproach1()
Name = "C.A.Dist.1";
Description = "Close approach distance to target (1).";
Category = MicroEntryCategory.Target;
IsDefault = true;
IsDefault = false;
HideWhenNoData = true;
MiliUnit = "mm";
BaseUnit = "m";
Expand Down Expand Up @@ -680,7 +680,7 @@ public TimeToCloseApproach1()
Description = "Close approach time to target (1).";
EntryType = EntryType.Time;
Category = MicroEntryCategory.Target;
IsDefault = true;
IsDefault = false;
HideWhenNoData = true;
Formatting = null;
}
Expand Down Expand Up @@ -843,7 +843,7 @@ public override void RefreshData()
public override string ValueDisplay => base.ValueDisplay;
}

public class Target_BodyLowOrbitMaxAltitude : BodyEntry
public class Target_BodyLowOrbitMaxAltitude : TargetEntry
{
public Target_BodyLowOrbitMaxAltitude()
{
Expand Down Expand Up @@ -876,7 +876,7 @@ public override void RefreshData()
public override string ValueDisplay => base.ValueDisplay;
}

public class Target_BodyHighOrbitMaxAltitude : BodyEntry
public class Target_BodyHighOrbitMaxAltitude : TargetEntry
{
public Target_BodyHighOrbitMaxAltitude()
{
Expand Down Expand Up @@ -906,5 +906,118 @@ public override void RefreshData()
?.HighOrbitMaxAltitude / 1000;
}

public override string ValueDisplay => base.ValueDisplay;
}

public class Target_ClosestApproachDistance : TargetEntry
{
public Target_ClosestApproachDistance()
{
Name = "C. Approach Dist.";
Description = "Distance between active vessel and target vessel at closest approach.";
Category = MicroEntryCategory.Target;
IsDefault = true;
MiliUnit = "mm";
BaseUnit = "m";
KiloUnit = "km";
MegaUnit = "Mm";
GigaUnit = "Gm";
NumberOfDecimalDigits = 0;
Formatting = "N";
}

public override void RefreshData()
{
//EntryValue = Utility.ActiveVessel.TargetObject?.Orbiter?.PatchedConicsOrbit.ClosestApproachDistance;

var activeVesselOrbit = Utility.ActiveVessel.Orbit;
var targetOrbit = Utility.ActiveVessel.TargetObject?.Orbit as PatchedConicsOrbit;

if (activeVesselOrbit == null || targetOrbit == null)
{
EntryValue = null;
return;
}

EntryValue = activeVesselOrbit.NextClosestApproachDistance(
targetOrbit, Utility.UniversalTime);
}

public override string ValueDisplay => base.ValueDisplay;
}

public class Target_ClosestApproachTime : TargetEntry
{
public Target_ClosestApproachTime()
{
Name = "C. Approach Time";
Description = "Time until close approach with the target.";
EntryType = EntryType.Time;
Category = MicroEntryCategory.Target;
IsDefault = true;
Formatting = null;
}

public override void RefreshData()
{
var activeVesselOrbit = Utility.ActiveVessel.Orbit;
var targetOrbit = Utility.ActiveVessel.TargetObject?.Orbit as PatchedConicsOrbit;

if (activeVesselOrbit == null || targetOrbit == null)
{
EntryValue = null;
return;
}

EntryValue = activeVesselOrbit.NextClosestApproachTime(
targetOrbit, Utility.UniversalTime) - Utility.UniversalTime;
}

public override string ValueDisplay => base.ValueDisplay;
}

public class Target_ClosestApproachRelativeSpeed : TargetEntry
{
public Target_ClosestApproachRelativeSpeed()
{
Name = "C. Approach Speed";
Description = "Relative speed at close approach with the target.";
Category = MicroEntryCategory.Target;
IsDefault = true;
MiliUnit = "mm/s";
BaseUnit = "m/s";
KiloUnit = "km/s";
MegaUnit = "Mm/s";
GigaUnit = "Gm/s";
NumberOfDecimalDigits = 1;
Formatting = "N";
AltUnit = new AltUnit()
{
IsActive = false,
Unit = "km/h",
Factor = (60f * 60f) / 1000f
};
}

public override void RefreshData()
{
var activeVesselOrbit = Utility.ActiveVessel.Orbit;
var targetOrbit = Utility.ActiveVessel.TargetObject?.Orbit as PatchedConicsOrbit;

if (activeVesselOrbit == null || targetOrbit == null)
{
EntryValue = null;
return;
}

var closeApproachTime = activeVesselOrbit.NextClosestApproachTime(
targetOrbit, Utility.UniversalTime);

var vesselVelocity = activeVesselOrbit.WorldOrbitalVelocityAtUT(closeApproachTime);
var targetVelocity = targetOrbit.WorldOrbitalVelocityAtUT(closeApproachTime);

EntryValue = (vesselVelocity - targetVelocity).magnitude;
}

public override string ValueDisplay => base.ValueDisplay;
}
77 changes: 77 additions & 0 deletions src/MicroEngineer/Utilities/OrbitExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using KSP.Api;
using KSP.Sim.impl;

namespace MicroEngineer.Utilities;

public static class OrbitExtensions
{
public static double NextClosestApproachTime(
this PatchedConicsOrbit a,
PatchedConicsOrbit b,
double UT)
{
double num1 = UT;
double num2 = double.MaxValue;
double num3 = UT;
double num4 = a.period;
if (a.eccentricity > 1.0)
num4 = 100.0 / a.MeanMotion();
double num5 = UT + num4;
for (int index1 = 0; index1 < 8; ++index1)
{
double num6 = (num5 - num3) / 20.0;
for (int index2 = 0; index2 < 20; ++index2)
{
double UT1 = num3 + (double) index2 * num6;
double num7 = a.Separation(b, UT1);
if (num7 < num2)
{
num2 = num7;
num1 = UT1;
}
}
num3 = Math.Clamp(num1 - num6, UT, UT + num4);
num5 = Math.Clamp(num1 + num6, UT, UT + num4);
}
return num1;
}

public static double MeanMotion(this PatchedConicsOrbit o)
{
return o.eccentricity > 1.0 ? Math.Sqrt(o.referenceBody.gravParameter / Math.Abs(Math.Pow(o.semiMajorAxis, 3.0))) : 2.0 * Math.PI / o.period;
}

public static double Separation(this PatchedConicsOrbit a, PatchedConicsOrbit b, double UT)
{
return (a.WorldPositionAtUT(UT) - b.WorldPositionAtUT(UT)).magnitude;
}

public static Vector3d WorldPositionAtUT(this PatchedConicsOrbit o, double UT)
{
return o.referenceBody.transform.celestialFrame.ToLocalPosition((ICoordinateSystem) o.ReferenceFrame, o.referenceBody.Position.localPosition + o.GetRelativePositionAtUTZup(UT).SwapYAndZ);
}

public static double NextClosestApproachDistance(
this PatchedConicsOrbit a,
PatchedConicsOrbit b,
double UT)
{
return a.Separation(b, a.NextClosestApproachTime(b, UT));
}

// Not working correctly
// public static double RelativeSpeed(this PatchedConicsOrbit a, PatchedConicsOrbit b, double UT)
// {
// return Vector3d.Dot(a.WorldOrbitalVelocityAtUT(UT) - b.WorldOrbitalVelocityAtUT(UT), (a.WorldBCIPositionAtUT(UT) - b.WorldBCIPositionAtUT(UT)).normalized);
// }

public static Vector3d WorldOrbitalVelocityAtUT(this PatchedConicsOrbit o, double UT)
{
return o.referenceBody.transform.celestialFrame.ToLocalPosition((ICoordinateSystem) o.ReferenceFrame, o.GetOrbitalVelocityAtUTZup(UT).SwapYAndZ);
}

public static Vector3d WorldBCIPositionAtUT(this PatchedConicsOrbit o, double UT)
{
return o.referenceBody.transform.celestialFrame.ToLocalPosition((ICoordinateSystem) o.ReferenceFrame, o.GetRelativePositionAtUTZup(UT).SwapYAndZ);
}
}

0 comments on commit 73343b6

Please sign in to comment.