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 library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 **************************************************************************/
26 #include <simgear/debug/logstream.hxx>
28 #ifdef FG_MATH_EXCEPTION_CLASH
29 # define exception c_exception
33 #include "celestialBody.hxx"
37 /**************************************************************************
38 * void CelestialBody::updatePosition(double mjd, Star *ourSun)
40 * Basically, this member function provides a general interface for
41 * calculating the right ascension and declinaion. This function is
42 * used for calculating the planetary positions. For the planets, an
43 * overloaded member function is provided to additionally calculate the
45 * The sun and moon have their own overloaded updatePosition member, as their
46 * position is calculated an a slightly different manner.
49 * double mjd: provides the modified julian date.
50 * Star *ourSun: the sun's position is needed to convert heliocentric
51 * coordinates into geocentric coordinates.
55 *************************************************************************/
56 void CelestialBody::updatePosition(double mjd, Star *ourSun)
58 double eccAnom, v, ecl, actTime,
59 xv, yv, xh, yh, zh, xg, yg, zg, xe, ye, ze;
61 updateOrbElements(mjd);
62 actTime = sgCalcActTime(mjd);
64 // calcualate the angle bewteen ecliptic and equatorial coordinate system
65 ecl = DEG_TO_RAD * (23.4393 - 3.563E-7 *actTime);
67 eccAnom = sgCalcEccAnom(M, e); //calculate the eccentric anomaly
68 xv = a * (cos(eccAnom) - e);
69 yv = a * (sqrt (1.0 - e*e) * sin(eccAnom));
70 v = atan2(yv, xv); // the planet's true anomaly
71 r = sqrt (xv*xv + yv*yv); // the planet's distance
73 // calculate the planet's position in 3D space
74 xh = r * (cos(N) * cos(v+w) - sin(N) * sin(v+w) * cos(i));
75 yh = r * (sin(N) * cos(v+w) + cos(N) * sin(v+w) * cos(i));
76 zh = r * (sin(v+w) * sin(i));
78 // calculate the ecliptic longitude and latitude
79 xg = xh + ourSun->getxs();
80 yg = yh + ourSun->getys();
83 lonEcl = atan2(yh, xh);
84 latEcl = atan2(zh, sqrt(xh*xh+yh*yh));
87 ye = yg * cos(ecl) - zg * sin(ecl);
88 ze = yg * sin(ecl) + zg * cos(ecl);
89 rightAscension = atan2(ye, xe);
90 declination = atan2(ze, sqrt(xe*xe + ye*ye));
91 /* FG_LOG(FG_GENERAL, FG_INFO, "Planet found at : "
92 << rightAscension << " (ra), " << declination << " (dec)" ); */
94 //calculate some variables specific to calculating the magnitude
96 R = sqrt (xg*xg + yg*yg + zg*zg);
97 s = ourSun->getDistance();
99 // It is possible from these calculations for the argument to acos
100 // to exceed the valid range for acos(). So we do a little extra
103 double tmp = (r*r + R*R - s*s) / (2*r*R);
106 } else if ( tmp < -1.0) {
110 FV = RAD_TO_DEG * acos( tmp );
113 /****************************************************************************
114 * double CelestialBody::sgCalcEccAnom(double M, double e)
115 * this private member calculates the eccentric anomaly of a celestial body,
116 * given its mean anomaly and eccentricity.
118 * -Mean anomaly: the approximate angle between the perihelion and the current
119 * position. this angle increases uniformly with time.
121 * True anomaly: the actual angle between perihelion and current position.
123 * Eccentric anomaly: this is an auxilary angle, used in calculating the true
124 * anomaly from the mean anomaly.
126 * -eccentricity. Indicates the amount in which the orbit deviates from a
127 * circle (0 = circle, 0-1, is ellipse, 1 = parabola, > 1 = hyperbola).
129 * This function is also known as solveKeplersEquation()
132 * M: the mean anomaly
133 * e: the eccentricity
136 * the eccentric anomaly
138 ****************************************************************************/
139 double CelestialBody::sgCalcEccAnom(double M, double e)
142 eccAnom, E0, E1, diff;
144 eccAnom = M + e * sin(M) * (1.0 + e * cos (M));
145 // iterate to achieve a greater precision for larger eccentricities
151 E1 = E0 - (E0 - e * sin(E0) - M) / (1 - e *cos(E0));
152 diff = fabs(E0 - E1);
155 while (diff > (DEG_TO_RAD * 0.001));