Skip to content

Commit

Permalink
Small corrections to the calculations
Browse files Browse the repository at this point in the history
- Use double precision quaternion
- Make sure the orbit normal always points roughly northwards
- The 'East' vector used to point to the west;
- Replace latitude with abs(latitude) for checking against inclinations

Fixes #7
  • Loading branch information
Nazfib committed Aug 15, 2023
1 parent 05d8e6c commit 302f319
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
Binary file modified GameData/LunarTransferPlanner/Plugins/LunarTransferPlanner.dll
Binary file not shown.
17 changes: 10 additions & 7 deletions LunarTransferPlanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,20 +269,23 @@ private OrbitData CalcOrbitForTime(CelestialBody target, Vector3d launchPos, dou
double targetTime = Planetarium.GetUniversalTime() + flightTime * 24d * 3600d + delayTime;
Vector3d targetPos = target.getPositionAtUT(targetTime);

Vector3d upVector = Quaternion.AngleAxis((float)(delayTime * 360d / mainBody.rotationPeriod), EarthAxis) * (launchPos - EarthPos).normalized;
Vector3d upVector = QuaternionD.AngleAxis(delayTime * 360d / mainBody.rotationPeriod, EarthAxis) * (launchPos - EarthPos).normalized;

Vector3d orbitNorm = Vector3d.Cross(targetPos - EarthPos, upVector).normalized;
double inclination = Math.Acos(Vector3d.Dot(orbitNorm, mainBody.angularVelocity.normalized));
double inclination = Math.Acos(Vector3d.Dot(orbitNorm, EarthAxis));
if (inclination > Math.PI / 2)
{
inclination = Math.PI - inclination;
orbitNorm *= -1; // make sure orbitNorm always points roughly northwards
}

Vector3d eastVec = Vector3d.Cross(EarthAxis, upVector).normalized;
// When checking this: remember that Unity (and KSP) use a left-handed coordinate system; therefore, the
// cross product follows the left-hand rule.
Vector3d eastVec = Vector3d.Cross(upVector, EarthAxis).normalized;
Vector3d northVec = Vector3d.Cross(eastVec, upVector).normalized;
Vector3d launchVec = Vector3d.Cross(upVector, orbitNorm).normalized;

double azimuth = Math.Acos(Vector3d.Dot(launchVec, northVec));
if (Vector3d.Dot(launchVec, eastVec) < 0d)
azimuth = Math.PI - azimuth;

return new OrbitData(orbitNorm, inclination * 180d / Math.PI, azimuth * 180d / Math.PI);
}
Expand All @@ -293,7 +296,7 @@ private double EstimateLaunchTime(CelestialBody target, Vector3d launchPos, doub
double t = startTime;
OrbitData launchOrbit = CalcOrbitForTime(target, launchPos, t);

if (latitude >= target.orbit.inclination)
if (Math.Abs(latitude) >= target.orbit.inclination)
{
// High latitude path - find the next easterly launch to the target
while (Math.Abs(launchOrbit.azimuth - targetAz) > 0.01d)
Expand Down Expand Up @@ -427,7 +430,7 @@ void MakeMainWindow(int id)
GUILayout.Box(new GUIContent($"{(launchOrbit.azimuth > 90d ? -launchOrbit.inclination : launchOrbit.inclination):F2}°",
"Launch to this inclination now to reach a Lunar parking orbit"), GUILayout.MinWidth(100));

string tooltip = latitude >= target.orbit.inclination ?
string tooltip = Math.Abs(latitude) >= target.orbit.inclination ?
"Launch at this time for an Easterly launch to Lunar parking orbit" :
"Launch at this time for a low inclination launch to Lunar parking orbit";

Expand Down

0 comments on commit 302f319

Please sign in to comment.