]> git.mxchange.org Git - flightgear.git/blob - Astro/celestialBody.cxx
Changes contributed by Durk Talsma:
[flightgear.git] / 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  * (Log is kept at end of this file)
24  **************************************************************************/
25
26 #include "celestialBody.hxx"
27 #include "star.hxx"
28 #include <Debug/logstream.hxx>
29
30 #ifdef FG_MATH_EXCEPTION_CLASH
31 #  define exception c_exception
32 #endif
33 #include <math.h>
34
35 /**************************************************************************
36  * void CelestialBody::updatePosition(fgTIME *t, Star *ourSun)
37  *
38  * Basically, this member function provides a general interface for 
39  * calculating the right ascension and declinaion. This function is 
40  * used for calculating the planetary positions. For the planets, an 
41  * overloaded member function is provided to additionally calculate the
42  * planet's magnitude. 
43  * The sun and moon have their own overloaded updatePosition member, as their
44  * position is calculated an a slightly different manner.  
45  *
46  * arguments:
47  * fgTIME t: provides the current time.
48  * Star *ourSun: the sun's position is needed to convert heliocentric 
49  *               coordinates into geocentric coordinates.
50  *
51  * return value: none
52  *
53  *************************************************************************/
54 void CelestialBody::updatePosition(fgTIME *t, Star *ourSun)
55 {
56   double eccAnom, v, ecl, actTime, 
57     xv, yv, xh, yh, zh, xg, yg, zg, xe, ye, ze;
58
59   updateOrbElements(t);
60   actTime = fgCalcActTime(t);
61
62   // calcualate the angle bewteen ecliptic and equatorial coordinate system
63   ecl = DEG_TO_RAD * (23.4393 - 3.563E-7 *actTime);
64   
65   eccAnom = fgCalcEccAnom(M, e);  //calculate the eccentric anomaly
66   xv = a * (cos(eccAnom) - e);
67   yv = a * (sqrt (1.0 - e*e) * sin(eccAnom));
68   v = atan2(yv, xv);           // the planet's true anomaly
69   r = sqrt (xv*xv + yv*yv);    // the planet's distance
70   
71   // calculate the planet's position in 3D space
72   xh = r * (cos(N) * cos(v+w) - sin(N) * sin(v+w) * cos(i));
73   yh = r * (sin(N) * cos(v+w) + cos(N) * sin(v+w) * cos(i));
74   zh = r * (sin(v+w) * sin(i));
75
76   // calculate the ecliptic longitude and latitude
77   xg = xh + ourSun->getxs();
78   yg = yh + ourSun->getys();
79   zg = zh;
80
81   lonEcl = atan2(yh, xh);
82   latEcl = atan2(zh, sqrt(xh*xh+yh*yh));
83
84   xe = xg;
85   ye = yg * cos(ecl) - zg * sin(ecl);
86   ze = yg * sin(ecl) + zg * cos(ecl);
87   rightAscension = atan2(ye, xe);
88   declination = atan2(ze, sqrt(xe*xe + ye*ye));
89   FG_LOG(FG_GENERAL, FG_INFO, "Planet found at : " 
90          << rightAscension << " (ra), " << declination << " (dec)" );
91
92   //calculate some variables specific to calculating the magnitude 
93   //of the planet
94   R = sqrt (xg*xg + yg*yg + zg*zg);
95   s = ourSun->getDistance();
96
97   // It is possible from these calculations for the argument to acos
98   // to exceed the valid range for acos(). So we do a little extra
99   // checking.
100
101   double tmp = (r*r + R*R - s*s) / (2*r*R);
102   if ( tmp > 1.0) { 
103       tmp = 1.0;
104   } else if ( tmp < -1.0) {
105       tmp = -1.0;
106   }
107
108   FV = RAD_TO_DEG * acos( tmp );
109 };
110
111 /****************************************************************************
112  * double CelestialBody::fgCalcEccAnom(double M, double e)
113  * this private member calculates the eccentric anomaly of a celestial body, 
114  * given its mean anomaly and eccentricity.
115  * 
116  * -Mean anomaly: the approximate angle between the perihelion and the current
117  *  position. this angle increases uniformly with time.
118  *
119  * True anomaly: the actual angle between perihelion and current position.
120  *
121  * Eccentric anomaly: this is an auxilary angle, used in calculating the true
122  * anomaly from the mean anomaly.
123  * 
124  * -eccentricity. Indicates the amount in which the orbit deviates from a 
125  *  circle (0 = circle, 0-1, is ellipse, 1 = parabola, > 1 = hyperbola).
126  *
127  * This function is also known as solveKeplersEquation()
128  *
129  * arguments: 
130  * M: the mean anomaly
131  * e: the eccentricity
132  *
133  * return value:
134  * the eccentric anomaly
135  *
136  ****************************************************************************/
137 double CelestialBody::fgCalcEccAnom(double M, double e)
138 {
139   double 
140     eccAnom, E0, E1, diff;
141   
142   eccAnom = M + e * sin(M) * (1.0 + e * cos (M));
143   // iterate to achieve a greater precision for larger eccentricities 
144   if (e > 0.05)
145     {
146       E0 = eccAnom;
147       do
148         {
149           E1 = E0 - (E0 - e * sin(E0) - M) / (1 - e *cos(E0));
150           diff = fabs(E0 - E1);
151           E0 = E1;
152         }
153       while (diff > (DEG_TO_RAD * 0.001));
154       return E0;
155     }
156   return eccAnom;
157 }
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177