This repository has been archived by the owner on Mar 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stateToKepler.js
71 lines (43 loc) · 1.49 KB
/
stateToKepler.js
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
import vector from '../vector';
import constants from '../constants';
export default function stateToKepler(r, rDot, t, m1, m2) {
var GM;
if ( m1 && m2 ) {
GM = constants.common.G * (m1 + m2);
} else if ( m1 ) {
GM = constants.common.G * m1;
} else {
GM = constants.earth.GM;
}
let h = vector.cross(r, rDot);
let Ω = Math.atan2(h[0], -h[1]);
let i = Math.atan2(Math.hypot(...h.slice(0, 2)), h[2]);
let p = vector.dot(h, h) / GM;
let rLen = Math.hypot(...r);
let e = Math.sqrt(
p / GM * Math.pow(vector.dot(r, rDot) / rLen, 2) +
Math.pow(p / rLen - 1, 2)
);
let ν = Math.atan2( Math.sqrt( p / GM ) * vector.dot(r, rDot), p - rLen );
let rb = vector.mm(
vector.r(i, 1), vector.mm(
vector.r(Ω, 3), r
)
);
let ω = Math.atan2(rb[1], rb[0]) - ν;
if ( e < 1 ) {
let a = p / (1 - Math.pow(e, 2));
let E = 2 * Math.atan( Math.sqrt( (1 - e) / (1 + e) ) * Math.tan(ν / 2) );
let T0 = t - Math.sqrt( Math.pow(a, 3) / GM ) * (E - e * Math.sin(E));
return [a, e, i, Ω, ω, T0];
} else if ( e > 1 ) {
let a = p / (Math.pow(e, 2) - 1);
let H = 2 * Math.atanh( Math.sqrt( (1 - e) / (1 + e) ) * Math.tan(ν / 2) );
let T0 = t + Math.sqrt( Math.pow(a, 3) / GM ) * (H - e * Math.sinh(H));
return [a, e, i, Ω, ω, T0];
} else if ( e === 1 ) {
let T0 = t - 0.5 * Math.sqrt( Math.pow(p, 3) / GM ) *
(Math.tan(ν / 2) + 1 / 3 * Math.pow( Math.tan(ν / 2), 3) );
return [p, e, i, Ω, ω, T0];
}
}