Convert uses of Orbit.trueAnomaly to handle now being in radians
[VesselSimulator.git] / KerbalEngineer / Extensions / OrbitExtensions.cs
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
151
152
153
154
155
156
157
// 
//     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 KerbalEngineer.Helpers;
 
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)
        {
            return GetAngleToPrograde(orbit, Planetarium.GetUniversalTime());
        }
 
        public static double GetAngleToPrograde(this Orbit orbit, double universalTime)
        {
            if (orbit.referenceBody == CelestialBodies.SystemBody.CelestialBody)
            {
                return 0.0;
            }
 
            Vector3d orbitVector = orbit.getRelativePositionAtUT(universalTime);
            orbitVector.z = 0.0;
 
            Vector3d bodyVector = orbit.referenceBody.orbit.getOrbitalVelocityAtUT(universalTime);
            bodyVector.z = 0.0;
 
            double angle = AngleHelper.GetAngleBetweenVectors(bodyVector, orbitVector);
 
            return AngleHelper.Clamp360(orbit.inclination < 90.0 ? angle : 360.0 - angle);
        }
 
        public static double GetAngleToRetrograde(this Orbit orbit)
        {
            return GetAngleToRetrograde(orbit, Planetarium.GetUniversalTime());
        }
 
        public static double GetAngleToRetrograde(this Orbit orbit, double universalTime)
        {
            if (orbit.referenceBody == CelestialBodies.SystemBody.CelestialBody)
            {
                return 0.0;
            }
 
            Vector3d orbitVector = orbit.getRelativePositionAtUT(universalTime);
            orbitVector.z = 0.0;
 
            Vector3d bodyVector = orbit.referenceBody.orbit.getOrbitalVelocityAtUT(universalTime);
            bodyVector.z = 0.0;
 
            double angle = AngleHelper.GetAngleBetweenVectors(-bodyVector, orbitVector);
 
            return AngleHelper.Clamp360(orbit.inclination < 90.0 ? angle : 360.0 - angle);
        }
 
        public static double GetAngleToTrueAnomaly(this Orbit orbit, double trueAnomaly)
        {
            return AngleHelper.Clamp360(trueAnomaly - orbit.trueAnomaly);
        }
 
        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)
        {
            var angle = AngleHelper.GetAngleBetweenVectors(Vector3d.Exclude(orbit.GetOrbitNormal(), target.pos), orbit.pos);
            return (orbit.semiMajorAxis < target.semiMajorAxis) ? angle : angle - 360.0;
        }
 
        public static double GetRelativeInclination(this Orbit orbit, Orbit target)
        {
            return Vector3d.Angle(orbit.GetOrbitNormal(), target.GetOrbitNormal());
        }
 
        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 trueAnomaly)
        {
            var time = orbit.GetDTforTrueAnomaly(trueAnomaly * 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
    }
}