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