]> git.mxchange.org Git - flightgear.git/blob - Astro/moon.c
Prepairing for C++ integration.
[flightgear.git] / Astro / moon.c
1 /**************************************************************************
2  * moon.c
3  * Written by Durk Talsma. Started October 1997, for the flight gear project.
4  *
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.
9  *
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.
14  *
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.
18  *
19  * $Id$
20  * (Log is kept at end of this file)
21  **************************************************************************/
22
23
24 #include <config.h>
25
26 #ifdef HAVE_WINDOWS_H
27 #  include <windows.h>
28 #endif
29
30 #include <math.h>
31 #include <GL/glut.h>
32 #include <XGL/xgl.h>
33
34 #include <Astro/orbits.h>
35 #include <Astro/moon.h>
36
37 #include <Aircraft/aircraft.h>
38 #include <Debug/fg_debug.h>
39 #include <Include/fg_constants.h>
40 #include <Include/general.h>
41 #include <Main/views.h>
42 #include <Time/fg_time.h>
43
44 struct CelestialCoord moonPos;
45
46 static float xMoon, yMoon, zMoon;
47 static GLint moon = 0;
48
49
50
51 /* --------------------------------------------------------------
52       This section contains the code that calculates the actual
53       position of the moon in the night sky.
54 ----------------------------------------------------------------*/
55 struct CelestialCoord fgCalculateMoon(struct OrbElements params,
56                                       struct OrbElements sunParams,
57                                       struct fgTIME t)
58 {
59   struct CelestialCoord
60     geocCoord, topocCoord; 
61   
62   double
63     eccAnom, ecl, lonecl, latecl, actTime,
64     xv, yv, v, r, xh, yh, zh, xg, yg, zg, xe, ye, ze,
65     Ls, Lm, D, F, mpar, gclat, rho, HA, g;
66   
67   fgAIRCRAFT *a;
68   fgFLIGHT *f;
69
70   a = &current_aircraft;
71   f = a->flight;
72   
73   /* calculate the angle between ecliptic and equatorial coordinate
74    * system, in Radians */
75   actTime = fgCalcActTime(t);
76   ecl = ((DEG_TO_RAD * 23.4393) - (DEG_TO_RAD * 3.563E-7) * actTime);
77   /*ecl = 0.409093 - 6.2186E-9 * actTime; */
78                                                         
79   /* calculate the eccentric anomaly */
80   eccAnom = fgCalcEccAnom(params.M, params.e);
81
82   /* calculate the moon's distance (r) and  true anomaly (v) */
83   xv = params.a * ( cos(eccAnom) - params.e);
84   yv = params.a * ( sqrt(1.0 - params.e*params.e) * sin(eccAnom));
85   v =atan2(yv, xv);
86   r = sqrt(xv*xv + yv*yv);
87   
88   /* estimate the geocentric rectangular coordinates here */
89   xh = r * (cos (params.N) * cos (v + params.w) -
90             sin (params.N) * sin (v + params.w) * cos (params.i));
91   yh = r * (sin (params.N) * cos (v + params.w) +
92             cos (params.N) * sin (v + params.w) * cos (params.i));
93   zh = r * (sin(v + params.w) * sin(params.i));
94   
95   /* calculate the ecliptic latitude and longitude here */
96   lonecl = atan2( yh, xh);
97   latecl = atan2( zh, sqrt( xh*xh + yh*yh));
98
99   /* calculate a number of perturbations, i.e. disturbances caused by
100    * the gravitational influence of the sun and the other mayor
101    * planets. The largest of these even have their own names */
102   Ls = sunParams.M + sunParams.w;
103   Lm =    params.M +    params.w + params.N;
104   D = Lm - Ls;
105   F = Lm - params.N;
106   
107   lonecl += DEG_TO_RAD * (
108                           - 1.274 * sin (params.M - 2*D)                        /* the Evection         */
109                           + 0.658 * sin (2 * D)                                 /* the Variation        */
110                           - 0.186 * sin (sunParams.M)                           /* the yearly variation */
111                           - 0.059 * sin (2*params.M - 2*D)
112                           - 0.057 * sin (params.M - 2*D + sunParams.M)
113                           + 0.053 * sin (params.M + 2*D)
114                           + 0.046 * sin (2*D - sunParams.M)
115                           + 0.041 * sin (params.M - sunParams.M)
116                           - 0.035 * sin (D)                                      /* the Parallactic Equation */
117                           - 0.031 * sin (params.M + sunParams.M)
118                           - 0.015 * sin (2*F - 2*D)
119                           + 0.011 * sin (params.M - 4*D)
120                           );
121   latecl += DEG_TO_RAD * (
122                           - 0.173 * sin (F - 2*D)
123                           - 0.055 * sin (params.M - F - 2*D)
124                           - 0.046 * sin (params.M + F - 2*D)
125                           + 0.033 * sin (F + 2*D)
126                           + 0.017 * sin (2 * params.M + F)
127                           );
128   
129   r += (
130         - 0.58 * cos(params.M - 2*D)
131         - 0.46 * cos(2*D)
132         );
133   
134   xg = r * cos(lonecl) * cos(latecl);
135   yg = r * sin(lonecl) * cos(latecl);
136   zg = r *               sin(latecl);
137
138   xe  = xg;
139   ye = yg * cos(ecl) - zg * sin(ecl);
140   ze = yg * sin(ecl) + zg * cos(ecl);
141   
142
143   
144
145   geocCoord.RightAscension = atan2(ye, xe);
146   geocCoord.Declination = atan2(ze, sqrt(xe*xe + ye*ye));
147   
148   /* New since 25 december 1997 */
149   /* Calculate the moon's topocentric position instead of it's geocentric! */
150
151   /* calculate the moon's parrallax, i.e. the apparent size of the
152    * (equatorial) radius of the Earth, as seen from the moon */
153   mpar = asin( 1 / r); 
154   gclat = FG_Latitude - 0.083358 * sin (2 * DEG_TO_RAD *  FG_Latitude);
155   rho = 0.99883 + 0.00167 * cos(2 * DEG_TO_RAD * FG_Latitude);
156
157   if (geocCoord.RightAscension < 0)
158     geocCoord.RightAscension += (2*FG_PI);
159
160   HA = t.lst - (3.8197186 * geocCoord.RightAscension);
161
162   g = atan (tan(gclat) / cos( (HA / 3.8197186))); 
163
164      
165
166   topocCoord.RightAscension = geocCoord.RightAscension -
167       mpar * rho * cos (gclat) * sin (HA) / cos (geocCoord.Declination);
168   topocCoord.Declination = geocCoord.Declination -
169       mpar * rho * sin (gclat) * sin (g - geocCoord.Declination) / sin (g);
170   return topocCoord;
171 }
172
173
174 void fgMoonInit( void ) {
175     GLfloat moonColor[4] = {0.85, 0.75, 0.35, 1.0};
176     GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
177
178     fgPrintf( FG_ASTRO, FG_INFO, "Initializing the Moon\n");
179     fgSolarSystemUpdate(&(pltOrbElements[1]), cur_time_params);
180     moonPos = fgCalculateMoon(pltOrbElements[1], pltOrbElements[0], 
181                               cur_time_params);
182     fgPrintf( FG_ASTRO, FG_DEBUG, 
183               "Moon found at %f (ra), %f (dec)\n", moonPos.RightAscension, 
184               moonPos.Declination);
185
186     xMoon = 60000.0 * cos(moonPos.RightAscension) * cos(moonPos.Declination);
187     yMoon = 60000.0 * sin(moonPos.RightAscension) * cos(moonPos.Declination);
188     zMoon = 60000.0 * sin(moonPos.Declination);
189
190     if (moon) {
191         xglDeleteLists (moon, 1);
192     }
193
194     moon = xglGenLists (1);
195     xglNewList (moon, GL_COMPILE);
196   
197     xglMaterialfv (GL_FRONT, GL_AMBIENT, black);
198     xglMaterialfv (GL_FRONT, GL_DIFFUSE, moonColor);
199     xglPushMatrix ();
200     xglTranslatef (xMoon, yMoon, zMoon);
201     xglScalef (1400, 1400, 1400);
202   
203     glutSolidSphere (1.0, 10, 10);
204     xglPopMatrix ();
205     xglEndList ();
206 }
207
208
209 /* Draw the moon */
210 void fgMoonRender( void ) {
211     xglCallList(moon);
212 }
213
214
215 /* $Log$
216 /* Revision 1.9  1998/04/18 04:13:56  curt
217 /* Moved fg_debug.c to it's own library.
218 /*
219  * Revision 1.8  1998/04/03 21:52:49  curt
220  * Converting to Gnu autoconf system.
221  *
222  * Revision 1.7  1998/02/23 19:07:54  curt
223  * Incorporated Durk's Astro/ tweaks.  Includes unifying the sun position
224  * calculation code between sun display, and other FG sections that use this
225  * for things like lighting.
226  *
227  * Revision 1.6  1998/02/07 15:29:32  curt
228  * Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
229  * <chotchkiss@namg.us.anritsu.com>
230  *
231  * Revision 1.5  1998/02/02 20:53:21  curt
232  * To version 0.29
233  *
234  * Revision 1.4  1998/01/27 00:47:46  curt
235  * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
236  * system and commandline/config file processing code.
237  *
238  * Revision 1.3  1998/01/19 19:26:57  curt
239  * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
240  * This should simplify things tremendously.
241  *
242  * Revision 1.2  1998/01/19 18:40:16  curt
243  * Tons of little changes to clean up the code and to remove fatal errors
244  * when building with the c++ compiler.
245  *
246  * Revision 1.1  1998/01/07 03:16:16  curt
247  * Moved from .../Src/Scenery/ to .../Src/Astro/
248  *
249  * Revision 1.16  1998/01/06 01:20:24  curt
250  * Tweaks to help building with MSVC++
251  *
252  * Revision 1.15  1998/01/05 18:44:35  curt
253  * Add an option to advance/decrease time from keyboard.
254  *
255  * Revision 1.14  1997/12/30 20:47:50  curt
256  * Integrated new event manager with subsystem initializations.
257  *
258  * Revision 1.13  1997/12/30 16:41:00  curt
259  * Added log at end of file.
260  *
261  */