This repository was archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 328
Expand file tree
/
Copy pathOrbitUpdater.cs
More file actions
279 lines (232 loc) · 8.75 KB
/
OrbitUpdater.cs
File metadata and controls
279 lines (232 loc) · 8.75 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using UnityEngine;
namespace GalaxyExplorer
{
public class OrbitUpdater : MonoBehaviour
{
/// <summary>
/// Semi-major axis, km / DistanceScaleFactor
/// </summary>
public float SemiMajorAxis;
public float SemiMajorAxisReal;
public float CurrentSemiMajorAxis
{
get
{
return Mathf.Lerp(SemiMajorAxis, SemiMajorAxisReal * RealitySemiMajorAxisScale, Reality);
}
}
/// <summary>
/// Eccentricity
/// </summary>
public float Eccentricity;
public float EccentricityReal;
private float CurrentEccentricity
{
get
{
return Mathf.Lerp(Eccentricity, EccentricityReal, Reality);
}
}
/// <summary>
/// Argument of perigee, radians
/// </summary>
public float ArgumentOfPerigee;
/// <summary>
/// Inclination, radians
/// </summary>
public float Inclination;
public float InclinationReal;
private float CurrentInclination
{
get
{
return Mathf.Lerp(Inclination, InclinationReal, Reality);
}
}
/// <summary>
/// Right ascension of ascending node, radians
/// </summary>
public float RightAscensionOfAscendingNode;
/// <summary>
/// Mean anomaly, radians
/// </summary>
public float MeanAnomaly;
/// <summary>
/// Sidereal period, days
/// </summary>
public float Period;
public float PeriodReal;
public float CurrentPeriod
{
get
{
return Mathf.Lerp(Period, PeriodReal, Reality);
}
}
/// <summary>
/// Orbital plane
/// </summary>
public Coordinates Plane;
/// <summary>
/// True anomaly, radians
/// </summary>
public float TrueAnomaly;
/// <summary>
/// Epoch, JD
/// </summary>
public float Epoch;
/// <summary>
/// Realilty from 0 (schematic) to 1 (real)
/// </summary>
[Range(0, 1)]
public float Reality;
public float RealitySemiMajorAxisScale = 1;
public float Speed = 100.0f;
public float SpeedMultiplier = 1;
public float TransitionSpeedMultiplier = 1.0f;
public int MaxIterations = 50;
private DateTime myDateTime;
public DateTime StartDate { get; private set; }
private bool computed = false;
// Use this for initialization
private void Start()
{
StartDate = myDateTime = DateTime.Now;
}
// Update is called once per frame
private void Update()
{
if (computed && TransitionSpeedMultiplier == 0.0f)
{
// Don't animate the planet rotation during transitions
return;
}
myDateTime += TimeSpan.FromDays(Time.deltaTime * Speed * SpeedMultiplier * TransitionSpeedMultiplier);
transform.localPosition = CalculatePosition(myDateTime);
computed = true;
}
public Vector3 CalculatePosition(DateTime time)
{
float trueAnomoly = CalculateTrueAnomaly(time);
return CalculatePosition(trueAnomoly);
}
/// <summary>
/// True anomaly using Newton-Raphson iteration
/// </summary>
/// <param name="orbit"></param>
/// <param name="dateTime"></param>
/// <returns></returns>
public float CalculateTrueAnomaly(DateTime dateTime)
{
const float epsilon = 0.000001f;
float trueAnomoly;
// Mean Anomoly
float meanAnomoly = CalculateMeanAnomaly(dateTime);
// Eccentric Anomaly
float eccentricAnomoly, oldEccentricAnomoly, newEccentricAnomoly = meanAnomoly + (CurrentEccentricity / 2);
// Solve [ 0 = E - e sin(E) - M ] for E using Newton-Raphson: Xn+1 = Xn - [ f(Xn) / f'(Xn) ]
// E = Eccentric Anomaly, M = Mean Anomaly
int iterations = 0;
do
{
iterations++;
oldEccentricAnomoly = newEccentricAnomoly;
newEccentricAnomoly = oldEccentricAnomoly - (oldEccentricAnomoly - (CurrentEccentricity * Mathf.Sin(oldEccentricAnomoly)) - meanAnomoly) / (1.0f - (CurrentEccentricity * Mathf.Cos(oldEccentricAnomoly)));
}
while (Mathf.Abs(oldEccentricAnomoly - newEccentricAnomoly) > epsilon && iterations < MaxIterations);
// Iteractions
if (iterations == MaxIterations)
{
trueAnomoly = TrueAnomaly;
}
else
{
eccentricAnomoly = newEccentricAnomoly;
float cosEccentricAnomoly = Mathf.Cos(eccentricAnomoly);
// Solve cos(bodyAngle) = ( cos(E) - e ) / (1 - e cos(E) ) to get body angle with sun
// E = Eccentric Anomaly, e = Eccentricity
trueAnomoly = Mathf.Acos((cosEccentricAnomoly - CurrentEccentricity) / (1.0f - (CurrentEccentricity * cosEccentricAnomoly)));
// Arccos returns value between 0 and Pi, but when Mean Anomoly > Pi (ie past halfway point) take (TwoPi - angle) to get solution between Pi and TwoPi
if (meanAnomoly > Mathf.PI)
{
trueAnomoly = (Mathf.PI * 2.0f) - trueAnomoly;
}
// Fail
if (float.IsNaN(trueAnomoly))
{
trueAnomoly = TrueAnomaly;
}
}
return trueAnomoly;
}
/// <summary>
/// Mean anomaly
/// </summary>
/// <returns>Mean anomaly, radians (0 to TwoPi)</returns>
public float CalculateMeanAnomaly(DateTime dateTime)
{
float angle = (ElapsedDays(dateTime, Epoch) % CurrentPeriod) / CurrentPeriod * (Mathf.PI * 2) * (-1);
// Add mean anomaly at defined epoch
angle += MeanAnomaly;
// Wrap angle 0-TwoPi
if (angle > Mathf.PI * 2)
{
angle -= Mathf.PI * 2;
}
else if (angle < 0)
{
angle += Mathf.PI * 2;
}
return angle;
}
/// <summary>
/// Calculate position in orbit relative to orbit origin
/// </summary>
/// <param name="trueAnomaly">True anomaly, radians</param>
public Vector3 CalculatePosition(float trueAnomaly)
{
// Compute radius from orbit origin
float radius = CurrentSemiMajorAxis * (1 - CurrentEccentricity * CurrentEccentricity) / (1 + CurrentEccentricity * Mathf.Cos(trueAnomaly));
// Calculate position relative to orbit origin
// XZ-plane is ecliptic, Y towards celestial north pole
return new Vector3(
radius * (Mathf.Cos(RightAscensionOfAscendingNode) * Mathf.Cos(trueAnomaly + ArgumentOfPerigee) - Mathf.Sin(RightAscensionOfAscendingNode) * Mathf.Sin(trueAnomaly + ArgumentOfPerigee) * Mathf.Cos(CurrentInclination)),
radius * (Mathf.Sin(trueAnomaly + ArgumentOfPerigee) * Mathf.Sin(CurrentInclination)),
-radius * (Mathf.Sin(RightAscensionOfAscendingNode) * Mathf.Cos(trueAnomaly + ArgumentOfPerigee) + Mathf.Cos(RightAscensionOfAscendingNode) * Mathf.Sin(trueAnomaly + ArgumentOfPerigee) * Mathf.Cos(CurrentInclination)));
}
/// <summary>
/// Elapsed days since J2000.0
/// </summary>
/// <returns></returns>
public float ElapsedDays(DateTime dateTime, float epoch)
{
return ToJulianDate(dateTime) - epoch;
}
public float ToJulianDate(DateTime dateTime)
{
return ToJulianDay(dateTime) + (dateTime.Hour - 12) / 24f + dateTime.Minute / 1440f + dateTime.Second / 86400f;
}
public long ToJulianDay(DateTime dateTime)
{
int month = dateTime.Month;
int day = dateTime.Day;
int year = dateTime.Year;
if (month < 3)
{
month += 12;
year -= 1;
}
return day + (153 * month - 457) / 5 + 365 * year + (year / 4) - (year / 100) + (year / 400) + 1721119;
}
public enum Coordinates
{
Ecliptic,
Equatorial,
Galactic,
Laplace
}
}
}