Repository navigation
Expand file tree
/
Copy pathorbital_information.ks
More file actions
328 lines (236 loc) · 11.6 KB
/
Copy pathorbital_information.ks
File metadata and controls
328 lines (236 loc) · 11.6 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//TODO: This function does not take into consideration the angular velocity of the orbiting body.
//A much more sophisticated approach is needed to get really accurate time to nearest point.
function timeToOrbitalPointNearestGeoLocation {
parameter geoCoordinates.
LOCAL orbitalPoint IS getOrbitalPointNearestGeoLocation(geoCoordinates).
return timeToNextStateVector(orbitalPoint, SHIP).
}
//Note, this function calculates the closest point in orbit at the current time, which may not be closet point in orbit
//at a future true given that the body being orbited is likely spinning.
function getOrbitalPointNearestGeoLocation {
parameter geoCoordinates.
LOCAL vectorToCoords IS geoCoordinates:POSITION - SHIP:BODY:POSITION.
return VXCL(getOrbitAngularMomentumVector(SHIP), vectorToCoords)*2.
}
function meanAnomalyAt {
parameter orbitTime.
parameter sourceVessel IS SHIP.
Local trueAnomaly IS trueAnomalyAt(orbitTime, sourceVessel).
Local meanAnomaly IS meanAnomalyFromTrueAnomaly(trueAnomaly, sourceVessel:Orbit:Eccentricity).
return meanAnomaly.
}
function eccentricAnomalyAt {
parameter orbitTime.
parameter sourceVessel IS SHIP.
Local trueAnomaly IS trueAnomalyAt(orbitTime, sourceVessel).
Local eccentricAnomaly IS eccentricAnomalyFromTrueAnomaly(trueAnomaly, sourceVessel:Orbit:Eccentricity).
return eccentricAnomaly.
}
function trueAnomalyAt {
parameter orbitTime.
parameter sourceVessel IS SHIP.
LOCAL orbitalPosition IS positionVectorAt(sourceVessel, orbitTime).
return trueAnomalyFromStateVectors(orbitalPosition, sourceVessel).
}
function timeToNextStateVector {
parameter stateVector.
parameter sourceVessel IS SHIP.
return timeAtNextStateVector(stateVector, sourceVessel) - TIME:SECONDS.
}
function timeAtNextStateVector {
parameter stateVector.
parameter sourceVessel IS SHIP.
//Calculate eccentricity vector.
//See this equation: https://en.wikipedia.org/wiki/Eccentricity_vector#Calculation
Local eccentricityVector Is getEccentricityVector(sourceVessel).
//Get True Anomaly of State Vector.
//See this equation: https://en.wikipedia.org/wiki/True_anomaly#From_state_vectors
Local trueAnomaly Is trueAnomalyFromStateVectors(stateVector).
Local meanAnomaly Is meanAnomalyFromTrueAnomaly(trueAnomaly, eccentricityVector:Mag).
// Print "True anomaly: " + trueAnomaly.
// Print "Mean Anomaly: " + meanAnomaly.
//Get Time to State Vector from Periapsis
Local t IS timeAtNextPeriapsis(sourceVessel) - sourceVessel:ORBIT:PERIOD.
Local n Is 360/sourceVessel:ORBIT:PERIOD.
Local timeAtNextMeanAnomaly Is meanAnomaly / n + t.
IF (timeAtNextMeanAnomaly < TIME:SECONDS) {
SET timeAtNextMeanAnomaly TO timeAtNextMeanAnomaly + sourceVessel:ORBIT:PERIOD.
}
return timeAtNextMeanAnomaly.
}
//See this equation: https://en.wikipedia.org/wiki/True_anomaly#From_state_vectors
function trueAnomalyFromStateVectors {
parameter orbitalPosition.
parameter sourceVessel IS SHIP.
LOCAL eccentricityVector IS getEccentricityVector(sourceVessel).
Local shipOrbitalPosition Is positionVectorAt(sourceVessel, TIME:SECONDS).
Local shipOrbitalVelocity Is sourceVessel:ORBIT:VELOCITY:ORBIT.
Local shipOrbitalMomentum Is VCRS(shipOrbitalPosition, shipOrbitalVelocity):NORMALIZED*(sourceVessel:ORBIT:APOAPSIS+sourceVessel:ORBIT:BODY:RADIUS).
// Print "VDOT: " + VDOT(eccentricityVector, orbitalPosition).
// Print "Bottom: " + eccentricityVector:MAG * orbitalPosition:MAG.
// Print "Before Arc Cos: " + VDOT(eccentricityVector, orbitalPosition) / (eccentricityVector:MAG * orbitalPosition:MAG).
// Print "Arc Cos: " + arccos(VDOT(eccentricityVector, orbitalPosition) / (eccentricityVector:MAG * orbitalPosition:MAG)).
//TODO: Testing indicates that VANG between eccentricity vector and orbital position is exactly what we need here. No need to re-invent the wheel.
// LOCAL trueAnomaly IS arccos(VDOT(eccentricityVector, orbitalPosition) / (eccentricityVector:MAG * orbitalPosition:MAG)).
LOCAl trueAnomaly IS VANG(eccentricityVector, orbitalPosition).
// PRINT "True Anamoly1: " + trueAnomaly.
// PRINT "True Anomaly2: " + trueAnomaly2.
// PRINT "Diff: " + abs(trueAnomaly - trueAnomaly2).
// Print "Orbital Position: " + orbitalPosition.
// Print "Orbital Velocity: " + orbitalVelocity.
LOCAL eccentricityCross IS VCRS(orbitalPosition,eccentricityVector):NORMALIZED*(sourceVessel:ORBIT:APOAPSIS+sourceVessel:ORBIT:BODY:RADIUS).
// drawVector(eccentricityCross, "EccentricityCross",sourceVessel:ORBIT:BODY:POSITION).
// drawVector(shipOrbitalMomentum, "Orbital Momentum", sourceVessel:ORBIT:BODY:POSITION).
//TODO: If orbitalPosition and orbitalVelocity are perpendicular this becomes inconsistent.
if (VANG(eccentricityCross, shipOrbitalMomentum) < 90) {
SET trueAnomaly TO 360 - trueAnomaly.
}
return trueAnomaly.
}
function eccentricAnomalyFromTrueAnomaly {
parameter trueAnomaly.
parameter eccentricity.
LOCAL cosE IS (eccentricity + cos(trueAnomaly)) / (1 + (eccentricity * cos(trueAnomaly))).
// PRINT "cosE: " + cosE.
LOCAL eccentricAnomaly IS arccos(cosE).
if(trueAnomaly > 180 AND eccentricAnomaly < 180) {
SET eccentricAnomaly TO 360 - eccentricAnomaly.
}
return eccentricAnomaly.
}
function meanAnomalyFromEccentricAnomaly {
parameter eccentricAnomaly.
parameter eccentricity.
LOCAL meanAnomaly IS eccentricAnomaly - eccentricity * abs(sin(eccentricAnomaly)) * CONSTANT:RADTODEG.
return meanAnomaly.
}
function meanAnomalyFromTrueAnomaly {
parameter trueAnomaly.
parameter eccentricity.
LOCAL eccentricAnomaly IS eccentricAnomalyFromTrueAnomaly(trueAnomaly, eccentricity).
LOCAL meanAnomaly IS meanAnomalyFromEccentricAnomaly(eccentricAnomaly, eccentricity).
return meanAnomaly.
}
//TODO: Change method signature, put sourceVessel as 2nd param, default to SHIP.
function positionVectorAt {
parameter sourceVessel.
parameter orbitTime.
return POSITIONAT(sourceVessel, orbitTime) - sourceVessel:ORBIT:BODY:POSITION.
}
function getEccentricityVector {
parameter sourceVessel IS SHIP.
//Method 1, which appears to have some innacuracy:
// LOCAL shipOrbitalVelocity IS sourceVessel:ORBIT:VELOCITY:ORBIT.
// LOCAL shipOrbitalPosition IS -sourceVessel:ORBIT:BODY:ORBIT:POSITION.
// LOCAL shipOrbitalMomentum IS VCRS(shipOrbitalPosition, shipOrbitalVelocity).
// LOCAL eccentricityVector IS (VCRS(shipOrbitalVelocity, shipOrbitalMomentum) / sourceVessel:ORBIT:BODY:MU)
// - (shipOrbitalPosition / shipOrbitalPosition:MAG).
//Method 2, seems to be more accurate
LOCAL timeAtPeriapsis IS timeAtNextPeriapsis(sourceVessel).
LOCAL positionAtPeriapsis IS positionVectorAt(sourceVessel, timeAtPeriapsis).
LOCAL newEccentricityVector IS positionAtPeriapsis:NORMALIZED * sourceVessel:ORBIT:ECCENTRICITY.
return newEccentricityVector.
}
//Returns the next instant in time that the source vessel will be at its orbital apoapsis.
function timeAtNextApoapsis {
parameter sourceVessel IS SHIP.
return timeToApoapsis(sourceVessel) + TIME:SECONDS.
}
//Returns the next instant in time that the source vessel will be at its orbital periapsis.
function timeAtNextPeriapsis {
parameter sourceVessel IS SHIP.
return timeToPeriapsis(sourceVessel) + TIME:SECONDS.
}
//Returns the seconds until the source vessel will be at its orbital periapsis. To get the actual point in time, add Time:SECONDS.
//Uses time equation
function timeToPeriapsis {
parameter sourceVessel IS SHIP.
//1 revolution from last periapsis will be the next time we're at periapsis.
return timeToOrbitRevolutionsFromLastPeriapsis(1, sourceVessel).
}
//Returns the seconds until the source vessel will be at its orbital apoapsis. To get the actual point in time, add Time:SECONDS.
//Uses time equation
function timeToApoapsis {
parameter sourceVessel IS SHIP.
//Half a revolution will be at apoapsis.
LOCAL timeToNextApoapsis IS timeToOrbitRevolutionsFromLastPeriapsis(0.5, sourceVessel).
//It's possible that we already passed apoapsis since last periapsis (we're currently on our way towards periapsis)
IF timeToNextApoapsis < 0 {
SET timeToNextApoapsis TO timeToNextApoapsis + sourceVessel:ORBIT:PERIOD.
}
return timeToNextApoapsis.
}
//Returns the time in seconds until the source vessel goes through the given revolutions
//through it's orbit. This parameter can be a decimal, and can be used to get the next time
//to periapsis (revolutions = 1), next Apoapsis (revolutions = 0.5), etc.
//Derivation is taken by calculating mean anomaly from eccentric anomaly and then dividing by mean motion.
function timeToOrbitRevolutionsFromLastPeriapsis {
parameter revolutions.
parameter sourceVessel IS SHIP.
LOCAL a iS sourceVessel:ORBIT:SEMIMAJORAXIS.
LOCAL mu IS sourceVessel:ORBIT:BODY:MU.
LOCAL e IS sourceVessel:ORBIT:ECCENTRICITY.
LOCAL period IS sourceVessel:ORBIT:PERIOD.
LOCAL myV is sourceVessel:ORBIT:TRUEANOMALY.
LOCAL tau IS eccentricAnomalyFromTrueAnomaly(myV, e).
//mean anomaly divided by mean motion gives you a reference (t - T) of where we are at in our orbit.
//see https://en.wikipedia.org/wiki/Mean_anomaly#Formula.
//Note that for the purposes this calculation we need meanAnomaly in Radians instead of degrees.
LOCAL timeSinceLastPeriapsis IS sqrt(a*a*a/mu)*(CONSTANT:DegToRad*tau - e*sin(tau)).
// PRINT "t is: " + t.
// "Revolutions True Anomaly: " + v.
// PRINT "Eccentric Anomaly: " + tau.
// PRINT "Period: " + period.
// PRINT "Revolutions: " + revolutions.
// PRINT "timeSinceLastPeriapsis Before negative: " + timeSinceLastPeriapsis.
return revolutions*period - timeSinceLastPeriapsis.
}
function getOrbitAngularMomentumVector {
parameter sourceVessel IS SHIP.
return VCRS(-sourceVessel:BODY:POSITION, sourceVessel:VELOCITY:ORBIT).
}
//Since the MUN has 0 inclination with respect to the "reference plane", we can reverse
//engineer the normal vector to the true reference plane using the cross product of it and the solar prime vector.
function getReferencePlaneNormalVector {
LOCAL refVector IS POSITIONAT(MUN,0).
return VCRS(refVector, SOLARPRIMEVECTOR):NORMALIZED.
}
function getLANVector {
parameter targetOrbital.
LOCAL c IS 1.
LOCAL p IS 1.
//IF no inclination, the LANVector is the same as the SOLARPRIMEVECTOR.
IF targetOrbital:ORBIT:LAN = 0 {
return SOLARPRIMEVECTOR:NORMALIZED.
} ELSE {
LOCAL rotation IS -ANGLEAXIS(targetOrbital:ORBIT:LAN,getReferencePlaneNormalVector()).
return (rotation*SOLARPRIMEVECTOR):NORMALIZED.
}
}
function getPeriapsisVector {
parameter targetOrbital.
IF targetOrbital:ORBIT:ECCENTRICITY > 0 {
LOCAL timeAtPeriapsis IS timeToOrbitRevolutionsFromLastPeriapsis(1, targetOrbital)+TIME:SECONDS.
return positionVectorAt(targetOrbital, timeAtPeriapsis).
}
LOCAL lanVector IS getLANVector(targetOrbital).
LOCAL refAngle IS -ANGLEAXIS(targetOrbital:Orbit:INCLINATION,lanVector)*SOLARPRIMEVECTOR.
IF (VANG(refAngle,getReferencePlaneNormalVector()) > 90) {
SET refAngle TO -refAngle.
}
// drawVector(refAngle*(targetOrbital:ORBIT:PERIAPSIS+b:ORBIT:BODY:RADIUS),"Reference Vector", KERBIN:POSITION).
LOCAL orbitNormalVector IS VCRS(lanVector, refAngle).
// drawVector(orbitNormalVector*(targetOrbital:ORBIT:PERIAPSIS+b:ORBIT:BODY:RADIUS),"Normal Vector", KERBIN:POSITION).
LOCAL periapsisVector IS ANGLEAXIS(targetOrbital:ORBIT:ARGUMENTOFPERIAPSIS, orbitNormalVector)*lanVector.
return periapsisVector:NORMALIZED.
}
//See https://en.wikipedia.org/wiki/True_anomaly#Radius_from_true_anomaly
function getRadiusFromTrueAnomaly {
parameter targetOrbital.
parameter trueAnomaly.
LOCAL e IS targetOrbital:ORBIT:ECCENTRICITY.
LOCAL a IS targetOrbital:ORBIT:SEMIMAJORAXIS.
LOCAL numerator IS 1 - (e*e).
LOCAL denominator IS 1 + e*cos(trueAnomaly).
return a*numerator/denominator.
}