Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nazfib/check maths #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified GameData/LunarTransferPlanner/Plugins/LunarTransferPlanner.dll
Binary file not shown.
19 changes: 11 additions & 8 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 @@ -424,10 +427,10 @@ void MakeMainWindow(int id)

GUILayout.Space(4);
GUILayout.Label("Launch Now Incl", GUILayout.ExpandWidth(true));
GUILayout.Box(new GUIContent($"{(launchOrbit.azimuth > 90d ? -launchOrbit.inclination : launchOrbit.inclination):F2}",
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
11 changes: 6 additions & 5 deletions LunarTransferPlanner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,15 @@
<None Include="README.md" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>xcopy /y "$(TargetPath)" "$(ProjectDir)/GameData/LunarTransferPlanner\Plugins\"</PostBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
-->
<Target Name="AfterBuild">
<Copy
SourceFiles="$(TargetPath)"
DestinationFolder="$(ProjectDir)/GameData/LunarTransferPlanner/Plugins"
SkipUnchangedFiles="false" />
</Target>
-->
</Project>
</Project>