1 /**************************************************************************
2 * orbits.c - calculates the orbital elements of the sun, moon and planets.
3 * For inclusion in flight gear
5 * Written 1997 by Durk Talsma, started October 19, 1997.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * (Log is kept at end of this file)
23 **************************************************************************/
29 #include <Astro/orbits.h>
31 #include <Include/general.h>
32 #include <Time/fg_time.h>
33 #include <Main/fg_debug.h>
35 struct OrbElements pltOrbElements[9];
38 double fgCalcActTime(struct fgTIME t)
44 /* a hack. This one introduces the 2000 problem into the program */
45 year = t.gmt->tm_year + 1900;
47 /* calculate the actual time, rember to add 1 to tm_mon! */
48 actTime = 367 * year - 7 *
49 (year + ((t.gmt->tm_mon+1) + 9) / 12) / 4 + 275 *
50 (t.gmt->tm_mon+1) / 9 + t.gmt->tm_mday - 730530;
52 UT = t.gmt->tm_hour + ((double) t.gmt->tm_min / 60);
53 /*printf("UT = %f\n", UT); */
54 actTime += (UT / 24.0);
57 /* printf(" Actual Time:\n"); */
58 /* printf(" current day = %f\t", actTime); */
59 /* printf(" GMT = %d, %d, %d, %d, %d, %d\n",
60 year, t.gmt->tm_mon, t.gmt->tm_mday,
61 t.gmt->tm_hour, t.gmt->tm_min, t.gmt->tm_sec); */
66 /* convert degrees to radians */
67 double fgDegToRad(double angle)
69 return (angle * PIOVER180);
72 double fgCalcEccAnom(double M, double e)
75 eccAnom, E0, E1, diff;
77 eccAnom = M + e * sin(M) * (1.0 + e * cos(M));
78 /* iterate to achieve a greater precision for larger eccentricities */
84 E1 = E0 - (E0 - e * sin(E0) - M) / (1 - e * cos(E0));
88 while (diff > fgDegToRad(0.001));
96 void fgReadOrbElements(struct OrbElements *dest, FILE *src)
103 fgets(line, 256,src);
104 for (i = 0; i < 256; i++)
109 /*printf("Reading line %d\n", j++); */
111 while (!(strlen(line)));
112 sscanf(line, "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf\n",
113 &dest->NFirst, &dest->NSec,
114 &dest->iFirst, &dest->iSec,
115 &dest->wFirst, &dest->wSec,
116 &dest->aFirst, &dest->aSec,
117 &dest->eFirst, &dest->eSec,
118 &dest->MFirst, &dest->MSec);
122 void fgSolarSystemInit(struct fgTIME t)
129 fgPrintf( FG_ASTRO, FG_INFO, "Initializing solar system\n");
131 /* build the full path name to the orbital elements database file */
134 strcat(path, g->root_dir);
135 strcat(path, "/Scenery/");
136 strcat(path, "Planets.dat");
138 if ( (data = fopen(path, "r")) == NULL )
140 fgPrintf( FG_ASTRO, FG_ALERT, "Cannot open data file: '%s'\n", path);
144 fgPrintf( FG_ASTRO, FG_INFO, " reading datafile %s\n", path);
147 /* for all the objects... */
148 for (i = 0; i < 9; i ++)
150 /* ...read from the data file ... */
151 fgReadOrbElements(&pltOrbElements[i], data);
152 /* ...and calculate the actual values */
153 fgSolarSystemUpdate(&pltOrbElements[i], t);
159 void fgSolarSystemUpdate(struct OrbElements *planet, struct fgTIME t)
164 actTime = fgCalcActTime(t);
166 /* calculate the actual orbital elements */
167 planet->M = fgDegToRad(planet->MFirst + (planet->MSec * actTime)); // angle in radians
168 planet->w = fgDegToRad(planet->wFirst + (planet->wSec * actTime)); // angle in radians
169 planet->N = fgDegToRad(planet->NFirst + (planet->NSec * actTime)); // angle in radians
170 planet->i = fgDegToRad(planet->iFirst + (planet->iSec * actTime)); // angle in radians
171 planet->e = planet->eFirst + (planet->eSec * actTime);
172 planet->a = planet->aFirst + (planet->aSec * actTime);
177 /* Revision 1.4 1998/01/27 00:47:47 curt
178 /* Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
179 /* system and commandline/config file processing code.
181 * Revision 1.3 1998/01/22 02:59:27 curt
182 * Changed #ifdef FILE_H to #ifdef _FILE_H
184 * Revision 1.2 1998/01/19 19:26:58 curt
185 * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
186 * This should simplify things tremendously.
188 * Revision 1.1 1998/01/07 03:16:17 curt
189 * Moved from .../Src/Scenery/ to .../Src/Astro/
191 * Revision 1.6 1997/12/30 20:47:52 curt
192 * Integrated new event manager with subsystem initializations.
194 * Revision 1.5 1997/12/15 23:55:02 curt
195 * Add xgl wrappers for debugging.
196 * Generate terrain normals on the fly.
198 * Revision 1.4 1997/12/10 22:37:51 curt
199 * Prepended "fg" on the name of all global structures that didn't have it yet.
200 * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
202 * Revision 1.3 1997/11/25 23:20:44 curt
203 * Changed planets.dat Planets.dat
205 * Revision 1.2 1997/11/25 19:25:36 curt
206 * Changes to integrate Durk's moon/sun code updates + clean up.
208 * Revision 1.1 1997/10/25 03:16:10 curt
209 * Initial revision of code contributed by Durk Talsma.