1 /**************************************************************************
3 * Written by Durk Talsma. Started October 1997, for the flight gear project.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * (Log is kept at end of this file)
21 **************************************************************************/
28 #include <Astro/orbits.h>
29 #include <Astro/moon.h>
31 #include <Aircraft/aircraft.h>
32 #include <Include/fg_constants.h>
33 #include <Include/general.h>
34 #include <Main/views.h>
35 #include <Time/fg_time.h>
36 #include <Main/fg_debug.h>
38 struct CelestialCoord moonPos;
40 static float xMoon, yMoon, zMoon;
45 /* --------------------------------------------------------------
46 This section contains the code that calculates the actual
47 position of the moon in the night sky.
48 ----------------------------------------------------------------*/
49 struct CelestialCoord fgCalculateMoon(struct OrbElements params,
50 struct OrbElements sunParams,
54 geocCoord, topocCoord;
57 eccAnom, ecl, lonecl, latecl, actTime,
58 xv, yv, v, r, xh, yh, zh, xg, yg, zg, xe, ye, ze,
59 Ls, Lm, D, F, mpar, gclat, rho, HA, g;
64 a = ¤t_aircraft;
67 /* calculate the angle between ecliptic and equatorial coordinate
68 * system, in Radians */
69 actTime = fgCalcActTime(t);
70 /* ecl = DEG_TO_RAD * (23.4393 - 3.563E-7 * actTime);*/
71 ecl = 0.409093 - 6.2186E-9 * actTime;
73 /* calculate the eccentric anomaly */
74 eccAnom = fgCalcEccAnom(params.M, params.e);
76 /* calculate the moon's distance (r) and true anomaly (v) */
77 xv = params.a * ( cos(eccAnom) - params.e);
78 yv = params.a * ( sqrt(1.0 - params.e*params.e) * sin(eccAnom));
80 r = sqrt(xv*xv + yv*yv);
82 /* estimate the geocentric rectangular coordinates here */
83 xh = r * (cos(params.N) * cos(v + params.w) - sin(params.N) * sin(v + params.w) * cos(params.i));
84 yh = r * (sin(params.N) * cos(v + params.w) + cos(params.N) * sin(v + params.w) * cos(params.i));
85 zh = r * (sin(v + params.w) * sin(params.i));
87 /* calculate the ecliptic latitude and longitude here */
88 lonecl = atan2( yh, xh);
89 latecl = atan2( zh, sqrt( xh*xh + yh*yh));
91 /* calculate a number of perturbations, i.e. disturbances caused by
92 * the gravitational influence of the sun and the other mayor
93 * planets, the largest even have their own names */
94 Ls = sunParams.M + sunParams.w;
95 Lm = params.M + params.w + params.N;
99 lonecl += DEG_TO_RAD * (
100 - 1.274 * sin (params.M - 2*D) /* the Evection */
101 + 0.658 * sin (2 * D) /* the Variation */
102 - 0.186 * sin (sunParams.M) /* the yearly variation */
103 - 0.059 * sin (2*params.M - 2*D)
104 - 0.057 * sin (params.M - 2*D + sunParams.M)
105 + 0.053 * sin (params.M + 2*D)
106 + 0.046 * sin (2*D - sunParams.M)
107 + 0.041 * sin (params.M - sunParams.M)
108 - 0.035 * sin (D) /* the Parallactic Equation */
109 - 0.031 * sin (params.M + sunParams.M)
110 - 0.015 * sin (2*F - 2*D)
111 + 0.011 * sin (params.M - 4*D)
113 latecl += DEG_TO_RAD * (
114 - 0.173 * sin (F - 2*D)
115 - 0.055 * sin (params.M - F - 2*D)
116 - 0.046 * sin (params.M + F - 2*D)
117 + 0.033 * sin (F + 2*D)
118 + 0.017 * sin (2 * params.M + F)
122 - 0.58 * cos(params.M - 2*D)
126 xg = r * cos(lonecl) * cos(latecl);
127 yg = r * sin(lonecl) * cos(latecl);
128 zg = r * sin(latecl);
131 ye = yg * cos(ecl) - zg * sin(ecl);
132 ze = yg * sin(ecl) + zg * cos(ecl);
137 geocCoord.RightAscension = atan2(ye, xe);
138 geocCoord.Declination = atan2(ze, sqrt(xe*xe + ye*ye));
140 /* New since 25 december 1997 */
141 /* Calculate the moon's topocentric position instead of it's geocentric! */
143 /* calculate the moon's parrallax, i.e. the apparent size of the
144 * (equatorial) radius of the Earth, as seen from the moon */
146 gclat = FG_Latitude - 0.083358 * sin (2 * DEG_TO_RAD * FG_Latitude);
147 rho = 0.99883 + 0.00167 * cos(2 * DEG_TO_RAD * FG_Latitude);
149 if (geocCoord.RightAscension < 0)
150 geocCoord.RightAscension += (2*FG_PI);
152 HA = t.lst - (3.8197186 * geocCoord.RightAscension);
154 g = atan (tan(gclat) / cos( (HA / 3.8197186)));
158 topocCoord.RightAscension = geocCoord.RightAscension - mpar * rho * cos(gclat) * sin(HA) / cos(geocCoord.Declination);
159 topocCoord.Declination = geocCoord.Declination - mpar * rho * sin(gclat) * sin(g - geocCoord.Declination) / sin(g);
164 void fgMoonInit( void ) {
165 /* struct fgLIGHT *l; */
166 static int dl_exists = 0;
167 /* GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 }; */
168 GLfloat moonColor[4] = {0.85, 0.65, 0.05, 1.0};
169 GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
171 fgPrintf( FG_ASTRO, FG_INFO, "Initializing the Moon\n");
173 /* l = &cur_light_params; */
175 /* position the moon */
176 fgSolarSystemUpdate(&(pltOrbElements[1]), cur_time_params);
177 moonPos = fgCalculateMoon(pltOrbElements[1], pltOrbElements[0],
179 fgPrintf( FG_ASTRO, FG_DEBUG,
180 "Moon found at %f (ra), %f (dec)\n", moonPos.RightAscension,
181 moonPos.Declination);
183 xMoon = 60000.0 * cos(moonPos.RightAscension) * cos(moonPos.Declination);
184 yMoon = 60000.0 * sin(moonPos.RightAscension) * cos(moonPos.Declination);
185 zMoon = 60000.0 * sin(moonPos.Declination);
190 /* printf("First time through, creating moon display list\n"); */
192 moon = xglGenLists(1);
193 xglNewList(moon, GL_COMPILE );
195 /* xglMaterialfv(GL_FRONT, GL_AMBIENT, l->scene_clear);
196 xglMaterialfv(GL_FRONT, GL_DIFFUSE, moon_color); */
197 xglMaterialfv(GL_FRONT, GL_AMBIENT, black);
198 xglMaterialfv(GL_FRONT, GL_DIFFUSE, moonColor);
200 glutSolidSphere(1.0, 10, 10);
208 void fgMoonRender( void ) {
209 /* struct fgLIGHT *l; */
211 /* printf("Rendering moon\n"); */
213 /* l = &cur_light_params; */
215 /* xglMaterialfv(GL_FRONT, GL_AMBIENT, l->sky_color ); */
216 /* xglMaterialfv(GL_FRONT, GL_DIFFUSE, white); */
219 xglTranslatef(xMoon, yMoon, zMoon);
220 xglScalef(1400, 1400, 1400);
229 /* Revision 1.6 1998/02/07 15:29:32 curt
230 /* Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
231 /* <chotchkiss@namg.us.anritsu.com>
233 * Revision 1.5 1998/02/02 20:53:21 curt
236 * Revision 1.4 1998/01/27 00:47:46 curt
237 * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
238 * system and commandline/config file processing code.
240 * Revision 1.3 1998/01/19 19:26:57 curt
241 * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
242 * This should simplify things tremendously.
244 * Revision 1.2 1998/01/19 18:40:16 curt
245 * Tons of little changes to clean up the code and to remove fatal errors
246 * when building with the c++ compiler.
248 * Revision 1.1 1998/01/07 03:16:16 curt
249 * Moved from .../Src/Scenery/ to .../Src/Astro/
251 * Revision 1.16 1998/01/06 01:20:24 curt
252 * Tweaks to help building with MSVC++
254 * Revision 1.15 1998/01/05 18:44:35 curt
255 * Add an option to advance/decrease time from keyboard.
257 * Revision 1.14 1997/12/30 20:47:50 curt
258 * Integrated new event manager with subsystem initializations.
260 * Revision 1.13 1997/12/30 16:41:00 curt
261 * Added log at end of file.