1 /**************************************************************************
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).
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.
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.
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.
23 **************************************************************************/
25 #include <simgear/debug/logstream.hxx>
27 #ifdef FG_MATH_EXCEPTION_CLASH
28 # define exception c_exception
32 #include "celestialBody.hxx"
36 /**************************************************************************
37 * void CelestialBody::updatePosition(fgTIME *t, Star *ourSun)
39 * Basically, this member function provides a general interface for
40 * calculating the right ascension and declinaion. This function is
41 * used for calculating the planetary positions. For the planets, an
42 * overloaded member function is provided to additionally calculate the
44 * The sun and moon have their own overloaded updatePosition member, as their
45 * position is calculated an a slightly different manner.
48 * fgTIME t: provides the current time.
49 * Star *ourSun: the sun's position is needed to convert heliocentric
50 * coordinates into geocentric coordinates.
54 *************************************************************************/
55 void CelestialBody::updatePosition(FGTime *t, Star *ourSun)
57 double eccAnom, v, ecl, actTime,
58 xv, yv, xh, yh, zh, xg, yg, zg, xe, ye, ze;
61 actTime = fgCalcActTime(t);
63 // calcualate the angle bewteen ecliptic and equatorial coordinate system
64 ecl = DEG_TO_RAD * (23.4393 - 3.563E-7 *actTime);
66 eccAnom = fgCalcEccAnom(M, e); //calculate the eccentric anomaly
67 xv = a * (cos(eccAnom) - e);
68 yv = a * (sqrt (1.0 - e*e) * sin(eccAnom));
69 v = atan2(yv, xv); // the planet's true anomaly
70 r = sqrt (xv*xv + yv*yv); // the planet's distance
72 // calculate the planet's position in 3D space
73 xh = r * (cos(N) * cos(v+w) - sin(N) * sin(v+w) * cos(i));
74 yh = r * (sin(N) * cos(v+w) + cos(N) * sin(v+w) * cos(i));
75 zh = r * (sin(v+w) * sin(i));
77 // calculate the ecliptic longitude and latitude
78 xg = xh + ourSun->getxs();
79 yg = yh + ourSun->getys();
82 lonEcl = atan2(yh, xh);
83 latEcl = atan2(zh, sqrt(xh*xh+yh*yh));
86 ye = yg * cos(ecl) - zg * sin(ecl);
87 ze = yg * sin(ecl) + zg * cos(ecl);
88 rightAscension = atan2(ye, xe);
89 declination = atan2(ze, sqrt(xe*xe + ye*ye));
90 FG_LOG(FG_GENERAL, FG_INFO, "Planet found at : "
91 << rightAscension << " (ra), " << declination << " (dec)" );
93 //calculate some variables specific to calculating the magnitude
95 R = sqrt (xg*xg + yg*yg + zg*zg);
96 s = ourSun->getDistance();
98 // It is possible from these calculations for the argument to acos
99 // to exceed the valid range for acos(). So we do a little extra
102 double tmp = (r*r + R*R - s*s) / (2*r*R);
105 } else if ( tmp < -1.0) {
109 FV = RAD_TO_DEG * acos( tmp );
112 /****************************************************************************
113 * double CelestialBody::fgCalcEccAnom(double M, double e)
114 * this private member calculates the eccentric anomaly of a celestial body,
115 * given its mean anomaly and eccentricity.
117 * -Mean anomaly: the approximate angle between the perihelion and the current
118 * position. this angle increases uniformly with time.
120 * True anomaly: the actual angle between perihelion and current position.
122 * Eccentric anomaly: this is an auxilary angle, used in calculating the true
123 * anomaly from the mean anomaly.
125 * -eccentricity. Indicates the amount in which the orbit deviates from a
126 * circle (0 = circle, 0-1, is ellipse, 1 = parabola, > 1 = hyperbola).
128 * This function is also known as solveKeplersEquation()
131 * M: the mean anomaly
132 * e: the eccentricity
135 * the eccentric anomaly
137 ****************************************************************************/
138 double CelestialBody::fgCalcEccAnom(double M, double e)
141 eccAnom, E0, E1, diff;
143 eccAnom = M + e * sin(M) * (1.0 + e * cos (M));
144 // iterate to achieve a greater precision for larger eccentricities
150 E1 = E0 - (E0 - e * sin(E0) - M) / (1 - e *cos(E0));
151 diff = fabs(E0 - E1);
154 while (diff > (DEG_TO_RAD * 0.001));