-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpositions.c
139 lines (118 loc) · 4.71 KB
/
positions.c
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
/*
ssystem 1.6
Copyright (C) 1997-1999 Raul Alonso <[email protected]>
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 2 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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
Calculate planets positions in a given date.
The main algorithm uses elements dated Julian 2450680.5 (8/20/1997) so
it's quite accurate for dates near this.
To be improved in the future ...
Keith Burnett's planet positioning algorithm used with persmission.
*/
#include "ssystem.h"
static double SolveKepler(double, double);
static double SolveKepler(double M, double ec)
{
static double E,d,delta;
E=M;
d=E-ec*sin(E)-M;
while (fabs(d)>1.0E-8) {
delta=d/(1.0L-ec*cos(E));
E=E-delta;
d=E-ec*sin(E)-M;
}
return E;
}
/* Based on Keith Burnett's QBASIC code found here:
http://www.xylem.demon.co.uk/kepler/
*/
void UpdatePositions(double days, int num)
{
static int j,k;
static double e,M,E,r,v,o,p,i,x,y,z,l,b;
if (!num) {
j=1;
k=NUMBODIES;
} else {
j=num;
k=j+1;
}
for (j=1;j<k;j++) {
if (planets[j].Type>=RING) continue;
e=planets[j].Eccentricity;
M=planets[j].DailyMotion*days+planets[j].MeanLongitude-planets[j].Perihelion;
E=SolveKepler(M,e);
r=planets[j].MeanDistance*(1.0-e*cos(E));
v=2.0L*atan(sqrt((1.0L+e)/(1.0L-e))*tan(E/2.0L));
o=planets[j].AscendingNode;
p=planets[j].Perihelion;
i=planets[j].Inclination;
planets[j].posx=r*(cos(o)*cos(v+p-o)-sin(o)*sin(v+p-o)*cos(i));
planets[j].posz=-r*(sin(o)*cos(v+p-o)+cos(o)*sin(v+p-o)*cos(i));
planets[j].posy=r*(sin(v+p-o)*sin(i));
if (planets[j].CustomFunction) {
switch(planets[j].CustomFunction) {
case 1:MercuryPos(days+2450680.5, &l, &b, &r); break;
case 2:VenusPos(days+2450680.5, &l, &b, &r); break;
case 3:EarthPos(days+2450680.5, &l, &b, &r); break;
case 4:MarsPos(days+2450680.5, &l, &b, &r); break;
case 5:JupiterPos(days+2450680.5, &l, &b, &r); break;
case 6:SaturnPos(days+2450680.5, &l, &b, &r); break;
case 7:UranusPos(days+2450680.5, &l, &b, &r); break;
case 8:NeptunePos(days+2450680.5, &l, &b, &r); break;
case 9:PlutoPos(days+2450680.5, &l, &b, &r); break;
case 10:MoonPos(days+2450680.5, &l, &b, &r); break;
}
r=DISTCORRECTION(r);
planets[j].posz=r*cos(b)*cos(l);
planets[j].posx=r*cos(b)*sin(l);
planets[j].posy=r*cos(b)*sin(b);
}
if (planets[j].Sat) {
/* satellites coords are expressed in host planet radii, convert
* to global coord ... similar to RADIUSSCALE macro */
planets[j].posx=planets[j].posx*planets[planets[j].Sat].Radius*0.0005;
planets[j].posy=planets[j].posy*planets[planets[j].Sat].Radius*0.0005;
planets[j].posz=planets[j].posz*planets[planets[j].Sat].Radius*0.0005;
if (!planets[j].CustomFunction) {
/* Rotate satellite to match host planet tilt + satellite orbit
* tilt. We could do this with glRotate when drawing objects
* but then we don't know real satellite coords so it's
* impossible to point the camera accurately */
i=-atan2(planets[planets[j].Sat].posx,planets[planets[j].Sat].posz);
o=cos(i);
p=sin(i);
x = planets[j].posx*o+planets[j].posz*p;
y = planets[j].posy;
z = -planets[j].posx*p+planets[j].posz*o;
o=cos(DEG2RAD(planets[planets[j].Sat].Degrees)+planets[j].Inclination);
p=sin(DEG2RAD(planets[planets[j].Sat].Degrees)+planets[j].Inclination);
planets[j].posx = planets[planets[j].Sat].posx+x;
planets[j].posy = planets[planets[j].Sat].posy+y*o+z*p;
planets[j].posz = planets[planets[j].Sat].posz-y*p+z*o;
} else {
/* Customs functions return always corrected values */
planets[j].posx += planets[planets[j].Sat].posx;
planets[j].posy += planets[planets[j].Sat].posy;
planets[j].posz += planets[planets[j].Sat].posz;
}
}
/* Rotate body. Due to the rendering process (where every body is moved
* from origin to its x,y,z position) it's neccesary for us to correct
* rotation angle so we undo the implicit rotation in that translation */
i=atan2(planets[j].posz,planets[j].posx)*180.0L/PIl;
e=planets[j].Rotation*(days/360.252502L); /* convert earth days to local */
planets[j].DeltaRotation=(e-floor(e))*360.0L-i+85.0L;
}
}