1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | // // Kerbal Engineer Redux // // Copyright (C) 2014 CYBUTEK // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. // #region Using Directives using System; using UnityEngine; #endregion namespace KerbalEngineer.Extensions { public static class OrbitExtensions { #region Constants public const double Tau = Math.PI * 2.0; #endregion #region Methods: public public static double GetAngleToAscendingNode(this Orbit orbit) { return GetAngleToTrueAnomaly(orbit, GetTrueAnomalyOfAscendingNode(orbit)); } public static double GetAngleToDescendingNode(this Orbit orbit) { return GetAngleToTrueAnomaly(orbit, GetTrueAnomalyOfDescendingNode(orbit)); } public static double GetAngleToPrograde(this Orbit orbit) { if (orbit.referenceBody == CelestialBodies.SystemBody.CelestialBody) { return 0.0; } var angle = AngleBetweenVectors(orbit.getRelativePositionAtUT(Planetarium.GetUniversalTime()), Vector3d.Exclude(orbit.GetOrbitNormal(), orbit.referenceBody.orbit.getRelativePositionAtUT(Planetarium.GetUniversalTime()))); angle = (angle + 90.0).ClampTo(0.0, 360.0); return orbit.inclination < 90.0 ? angle : 360.0 - angle; } public static double GetAngleToRetrograde(this Orbit orbit) { if (orbit.referenceBody == CelestialBodies.SystemBody.CelestialBody) { return 0.0; } var angle = AngleBetweenVectors(orbit.getRelativePositionAtUT(Planetarium.GetUniversalTime()), Vector3d.Exclude(orbit.GetOrbitNormal(), orbit.referenceBody.orbit.getRelativePositionAtUT(Planetarium.GetUniversalTime()))); angle = (angle - 90.0).ClampTo(0.0, 360.0); return orbit.inclination < 90.0 ? angle : 360.0 - angle; } public static double GetAngleToTrueAnomaly(this Orbit orbit, double tA) { return (tA - orbit.trueAnomaly).ClampTo(0.0, 360.0); } public static double GetAngleToVector(this Orbit orbit, Vector3d vector) { return GetAngleToTrueAnomaly(orbit, GetTrueAnomalyFromVector(orbit, Vector3d.Exclude(orbit.GetOrbitNormal(), vector))); } public static double GetPhaseAngle(this Orbit orbit, Orbit target) { return AngleBetweenVectors(orbit.pos, Vector3d.Exclude(orbit.GetOrbitNormal(), target.pos)); } public static double GetTimeToAscendingNode(this Orbit orbit) { return GetTimeToTrueAnomaly(orbit, GetTrueAnomalyOfAscendingNode(orbit)); } public static double GetTimeToDescendingNode(this Orbit orbit) { return GetTimeToTrueAnomaly(orbit, GetTrueAnomalyOfDescendingNode(orbit)); } public static double GetTimeToTrueAnomaly(this Orbit orbit, double tA) { var time = orbit.GetDTforTrueAnomaly(tA * Mathf.Deg2Rad, orbit.period); return time < 0.0 ? time + orbit.period : time; } public static double GetTimeToVector(this Orbit orbit, Vector3d vector) { return GetTimeToTrueAnomaly(orbit, GetTrueAnomalyFromVector(orbit, vector)); } public static double GetTrueAnomalyFromVector(this Orbit orbit, Vector3d vector) { return orbit.GetTrueAnomalyOfZupVector(vector) * Mathf.Rad2Deg; } public static double GetTrueAnomalyOfAscendingNode(this Orbit orbit) { return 360.0 - orbit.argumentOfPeriapsis; } public static double GetTrueAnomalyOfDescendingNode(this Orbit orbit) { return 180.0 - orbit.argumentOfPeriapsis; } #endregion #region Methods: private private static double AngleBetweenVectors(Vector3d vector1, Vector3d vector2) { var angle = Vector3d.Angle(vector1, vector2); var rotated = QuaternionD.AngleAxis(90.0, Vector3d.forward) * vector1; if (Vector3d.Angle(rotated, vector2) > 90.0) { angle = 360.0 - angle; } return angle; } #endregion } } |