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/fg_constants.h>
32 #include <Include/general.h>
33 #include <Time/fg_time.h>
34 #include <Main/fg_debug.h>
36 struct OrbElements pltOrbElements[9];
39 //double fgCalcActTime(struct fgTIME t)
45 // /* a hack. This one introduces the 2000 problem into the program */
46 // year = t.gmt->tm_year + 1900;
48 // /* calculate the actual time, remember to add 1 to tm_mon! */
49 // actTime = 367 * year - 7 *
50 // (year + ((t.gmt->tm_mon+1) + 9) / 12) / 4 + 275 *
51 // (t.gmt->tm_mon+1) / 9 + t.gmt->tm_mday - 730530;
53 // UT = t.gmt->tm_hour + ((double) t.gmt->tm_min / 60);
54 // /*printf("UT = %f\n", UT); */
55 // actTime += (UT / 24.0);
58 // /* printf(" Actual Time:\n"); */
59 // /* printf(" current day = %f\t", actTime); */
60 // /* printf(" GMT = %d, %d, %d, %d, %d, %d\n",
61 // year, t.gmt->tm_mon, t.gmt->tm_mday,
62 // t.gmt->tm_hour, t.gmt->tm_min, t.gmt->tm_sec); */
68 double fgCalcActTime(struct fgTIME t)
70 return (t.mjd - 36523.5);
74 /* convert degrees to radians */
76 double fgDegToRad(double angle)
78 return (angle * PIOVER180);
81 double fgCalcEccAnom(double M, double e)
84 eccAnom, E0, E1, diff;
86 eccAnom = M + e * sin(M) * (1.0 + e * cos(M));
87 /* iterate to achieve a greater precision for larger eccentricities */
93 E1 = E0 - (E0 - e * sin(E0) - M) / (1 - e * cos(E0));
97 while (diff > (DEG_TO_RAD * 0.001));
105 void fgReadOrbElements(struct OrbElements *dest, FILE *src)
112 fgets(line, 256,src);
113 for (i = 0; i < 256; i++)
118 /*printf("Reading line %d\n", j++); */
120 while (!(strlen(line)));
121 sscanf(line, "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf\n",
122 &dest->NFirst, &dest->NSec,
123 &dest->iFirst, &dest->iSec,
124 &dest->wFirst, &dest->wSec,
125 &dest->aFirst, &dest->aSec,
126 &dest->eFirst, &dest->eSec,
127 &dest->MFirst, &dest->MSec);
131 void fgSolarSystemInit(struct fgTIME t)
138 fgPrintf( FG_ASTRO, FG_INFO, "Initializing solar system\n");
140 /* build the full path name to the orbital elements database file */
143 strcat(path, g->root_dir);
144 strcat(path, "/Scenery/");
145 strcat(path, "Planets.dat");
147 if ( (data = fopen(path, "r")) == NULL )
149 fgPrintf( FG_ASTRO, FG_ALERT,
150 "Cannot open data file: '%s'\n", path);
153 fgPrintf( FG_ASTRO, FG_INFO, " reading datafile %s\n", path);
155 /* for all the objects... */
156 for (i = 0; i < 9; i ++)
158 /* ...read from the data file ... */
159 fgReadOrbElements(&pltOrbElements[i], data);
160 /* ...and calculate the actual values */
161 fgSolarSystemUpdate(&pltOrbElements[i], t);
167 void fgSolarSystemUpdate(struct OrbElements *planet, struct fgTIME t)
172 actTime = fgCalcActTime(t);
174 /* calculate the actual orbital elements */
175 planet->M = DEG_TO_RAD * (planet->MFirst + (planet->MSec * actTime)); // angle in radians
176 planet->w = DEG_TO_RAD * (planet->wFirst + (planet->wSec * actTime)); // angle in radians
177 planet->N = DEG_TO_RAD * (planet->NFirst + (planet->NSec * actTime)); // angle in radians
178 planet->i = DEG_TO_RAD * (planet->iFirst + (planet->iSec * actTime)); // angle in radians
179 planet->e = planet->eFirst + (planet->eSec * actTime);
180 planet->a = planet->aFirst + (planet->aSec * actTime);
185 /* Revision 1.5 1998/02/02 20:53:22 curt
188 * Revision 1.4 1998/01/27 00:47:47 curt
189 * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
190 * system and commandline/config file processing code.
192 * Revision 1.3 1998/01/22 02:59:27 curt
193 * Changed #ifdef FILE_H to #ifdef _FILE_H
195 * Revision 1.2 1998/01/19 19:26:58 curt
196 * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
197 * This should simplify things tremendously.
199 * Revision 1.1 1998/01/07 03:16:17 curt
200 * Moved from .../Src/Scenery/ to .../Src/Astro/
202 * Revision 1.6 1997/12/30 20:47:52 curt
203 * Integrated new event manager with subsystem initializations.
205 * Revision 1.5 1997/12/15 23:55:02 curt
206 * Add xgl wrappers for debugging.
207 * Generate terrain normals on the fly.
209 * Revision 1.4 1997/12/10 22:37:51 curt
210 * Prepended "fg" on the name of all global structures that didn't have it yet.
211 * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
213 * Revision 1.3 1997/11/25 23:20:44 curt
214 * Changed planets.dat Planets.dat
216 * Revision 1.2 1997/11/25 19:25:36 curt
217 * Changes to integrate Durk's moon/sun code updates + clean up.
219 * Revision 1.1 1997/10/25 03:16:10 curt
220 * Initial revision of code contributed by Durk Talsma.