]> git.mxchange.org Git - flightgear.git/blob - src/Astro/celestialBody.cxx
source tree reorganization prior to flightgear 0.7
[flightgear.git] / src / Astro / celestialBody.cxx
1 /**************************************************************************
2  * celestialBody.cxx
3  * Written by Durk Talsma. Originally started October 1997, for distribution  
4  * with the FlightGear project. Version 2 was written in August and 
5  * September 1998. This code is based upon algorithms and data kindly 
6  * provided by Mr. Paul Schlyter. (pausch@saaf.se). 
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * $Id$
23  **************************************************************************/
24
25 #include "celestialBody.hxx"
26 #include "star.hxx"
27 #include <Debug/logstream.hxx>
28
29 #ifdef FG_MATH_EXCEPTION_CLASH
30 #  define exception c_exception
31 #endif
32 #include <math.h>
33
34 /**************************************************************************
35  * void CelestialBody::updatePosition(fgTIME *t, Star *ourSun)
36  *
37  * Basically, this member function provides a general interface for 
38  * calculating the right ascension and declinaion. This function is 
39  * used for calculating the planetary positions. For the planets, an 
40  * overloaded member function is provided to additionally calculate the
41  * planet's magnitude. 
42  * The sun and moon have their own overloaded updatePosition member, as their
43  * position is calculated an a slightly different manner.  
44  *
45  * arguments:
46  * fgTIME t: provides the current time.
47  * Star *ourSun: the sun's position is needed to convert heliocentric 
48  *               coordinates into geocentric coordinates.
49  *
50  * return value: none
51  *
52  *************************************************************************/
53 void CelestialBody::updatePosition(FGTime *t, Star *ourSun)
54 {
55   double eccAnom, v, ecl, actTime, 
56     xv, yv, xh, yh, zh, xg, yg, zg, xe, ye, ze;
57
58   updateOrbElements(t);
59   actTime = fgCalcActTime(t);
60
61   // calcualate the angle bewteen ecliptic and equatorial coordinate system
62   ecl = DEG_TO_RAD * (23.4393 - 3.563E-7 *actTime);
63   
64   eccAnom = fgCalcEccAnom(M, e);  //calculate the eccentric anomaly
65   xv = a * (cos(eccAnom) - e);
66   yv = a * (sqrt (1.0 - e*e) * sin(eccAnom));
67   v = atan2(yv, xv);           // the planet's true anomaly
68   r = sqrt (xv*xv + yv*yv);    // the planet's distance
69   
70   // calculate the planet's position in 3D space
71   xh = r * (cos(N) * cos(v+w) - sin(N) * sin(v+w) * cos(i));
72   yh = r * (sin(N) * cos(v+w) + cos(N) * sin(v+w) * cos(i));
73   zh = r * (sin(v+w) * sin(i));
74
75   // calculate the ecliptic longitude and latitude
76   xg = xh + ourSun->getxs();
77   yg = yh + ourSun->getys();
78   zg = zh;
79
80   lonEcl = atan2(yh, xh);
81   latEcl = atan2(zh, sqrt(xh*xh+yh*yh));
82
83   xe = xg;
84   ye = yg * cos(ecl) - zg * sin(ecl);
85   ze = yg * sin(ecl) + zg * cos(ecl);
86   rightAscension = atan2(ye, xe);
87   declination = atan2(ze, sqrt(xe*xe + ye*ye));
88   FG_LOG(FG_GENERAL, FG_INFO, "Planet found at : " 
89          << rightAscension << " (ra), " << declination << " (dec)" );
90
91   //calculate some variables specific to calculating the magnitude 
92   //of the planet
93   R = sqrt (xg*xg + yg*yg + zg*zg);
94   s = ourSun->getDistance();
95
96   // It is possible from these calculations for the argument to acos
97   // to exceed the valid range for acos(). So we do a little extra
98   // checking.
99
100   double tmp = (r*r + R*R - s*s) / (2*r*R);
101   if ( tmp > 1.0) { 
102       tmp = 1.0;
103   } else if ( tmp < -1.0) {
104       tmp = -1.0;
105   }
106
107   FV = RAD_TO_DEG * acos( tmp );
108 };
109
110 /****************************************************************************
111  * double CelestialBody::fgCalcEccAnom(double M, double e)
112  * this private member calculates the eccentric anomaly of a celestial body, 
113  * given its mean anomaly and eccentricity.
114  * 
115  * -Mean anomaly: the approximate angle between the perihelion and the current
116  *  position. this angle increases uniformly with time.
117  *
118  * True anomaly: the actual angle between perihelion and current position.
119  *
120  * Eccentric anomaly: this is an auxilary angle, used in calculating the true
121  * anomaly from the mean anomaly.
122  * 
123  * -eccentricity. Indicates the amount in which the orbit deviates from a 
124  *  circle (0 = circle, 0-1, is ellipse, 1 = parabola, > 1 = hyperbola).
125  *
126  * This function is also known as solveKeplersEquation()
127  *
128  * arguments: 
129  * M: the mean anomaly
130  * e: the eccentricity
131  *
132  * return value:
133  * the eccentric anomaly
134  *
135  ****************************************************************************/
136 double CelestialBody::fgCalcEccAnom(double M, double e)
137 {
138   double 
139     eccAnom, E0, E1, diff;
140   
141   eccAnom = M + e * sin(M) * (1.0 + e * cos (M));
142   // iterate to achieve a greater precision for larger eccentricities 
143   if (e > 0.05)
144     {
145       E0 = eccAnom;
146       do
147         {
148           E1 = E0 - (E0 - e * sin(E0) - M) / (1 - e *cos(E0));
149           diff = fabs(E0 - E1);
150           E0 = E1;
151         }
152       while (diff > (DEG_TO_RAD * 0.001));
153       return E0;
154     }
155   return eccAnom;
156 }
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176