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));
104 // This function assumes that if the FILE ptr is valid that the contents
105 // will be valid. Should we check the file for validity?
107 void fgReadOrbElements(struct OrbElements *dest, FILE *src)
114 fgets(line, 256,src);
115 for (i = 0; i < 256; i++)
120 /*printf("Reading line %d\n", j++); */
122 while (!(strlen(line)));
123 sscanf(line, "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf\n",
124 &dest->NFirst, &dest->NSec,
125 &dest->iFirst, &dest->iSec,
126 &dest->wFirst, &dest->wSec,
127 &dest->aFirst, &dest->aSec,
128 &dest->eFirst, &dest->eSec,
129 &dest->MFirst, &dest->MSec);
133 int fgSolarSystemInit(struct fgTIME t)
141 fgPrintf( FG_ASTRO, FG_INFO, "Initializing solar system\n");
143 /* build the full path name to the orbital elements database file */
146 strcat(path, g->root_dir);
147 strcat(path, "/Scenery/");
148 strcat(path, "Planets.dat");
150 if ( (data = fopen(path, "r")) == NULL )
152 fgPrintf( FG_ASTRO, FG_ALERT,
153 "Cannot open data file: '%s'\n", path);
155 /* printf(" reading datafile %s\n", path); */
156 fgPrintf( FG_ASTRO, FG_INFO, " reading datafile %s\n", path);
158 /* for all the objects... */
159 for (i = 0; i < 9; i ++)
161 /* ...read from the data file ... */
162 fgReadOrbElements(&pltOrbElements[i], data);
163 /* ...and calculate the actual values */
164 fgSolarSystemUpdate(&pltOrbElements[i], t);
172 void fgSolarSystemUpdate(struct OrbElements *planet, struct fgTIME t)
177 actTime = fgCalcActTime(t);
179 /* calculate the actual orbital elements */
180 planet->M = DEG_TO_RAD * (planet->MFirst + (planet->MSec * actTime)); // angle in radians
181 planet->w = DEG_TO_RAD * (planet->wFirst + (planet->wSec * actTime)); // angle in radians
182 planet->N = DEG_TO_RAD * (planet->NFirst + (planet->NSec * actTime)); // angle in radians
183 planet->i = DEG_TO_RAD * (planet->iFirst + (planet->iSec * actTime)); // angle in radians
184 planet->e = planet->eFirst + (planet->eSec * actTime);
185 planet->a = planet->aFirst + (planet->aSec * actTime);
190 /* Revision 1.7 1998/02/12 21:59:33 curt
191 /* Incorporated code changes contributed by Charlie Hotchkiss
192 /* <chotchkiss@namg.us.anritsu.com>
194 * Revision 1.6 1998/02/03 23:20:11 curt
195 * Lots of little tweaks to fix various consistency problems discovered by
196 * Solaris' CC. Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
197 * passed arguments along to the real printf(). Also incorporated HUD changes
198 * by Michele America.
200 * Revision 1.5 1998/02/02 20:53:22 curt
203 * Revision 1.4 1998/01/27 00:47:47 curt
204 * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
205 * system and commandline/config file processing code.
207 * Revision 1.3 1998/01/22 02:59:27 curt
208 * Changed #ifdef FILE_H to #ifdef _FILE_H
210 * Revision 1.2 1998/01/19 19:26:58 curt
211 * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
212 * This should simplify things tremendously.
214 * Revision 1.1 1998/01/07 03:16:17 curt
215 * Moved from .../Src/Scenery/ to .../Src/Astro/
217 * Revision 1.6 1997/12/30 20:47:52 curt
218 * Integrated new event manager with subsystem initializations.
220 * Revision 1.5 1997/12/15 23:55:02 curt
221 * Add xgl wrappers for debugging.
222 * Generate terrain normals on the fly.
224 * Revision 1.4 1997/12/10 22:37:51 curt
225 * Prepended "fg" on the name of all global structures that didn't have it yet.
226 * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
228 * Revision 1.3 1997/11/25 23:20:44 curt
229 * Changed planets.dat Planets.dat
231 * Revision 1.2 1997/11/25 19:25:36 curt
232 * Changes to integrate Durk's moon/sun code updates + clean up.
234 * Revision 1.1 1997/10/25 03:16:10 curt
235 * Initial revision of code contributed by Durk Talsma.