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