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 <Debug/fg_debug.h>
32 #include <Include/fg_constants.h>
33 #include <Include/general.h>
34 #include <Time/fg_time.h>
36 struct OrbElements pltOrbElements[9];
39 double fgCalcActTime(struct fgTIME t)
41 return (t.mjd - 36523.5);
45 double fgCalcEccAnom(double M, double e)
48 eccAnom, E0, E1, diff;
50 eccAnom = M + e * sin(M) * (1.0 + e * cos(M));
51 /* iterate to achieve a greater precision for larger eccentricities */
57 E1 = E0 - (E0 - e * sin(E0) - M) / (1 - e * cos(E0));
61 while (diff > (DEG_TO_RAD * 0.001));
68 /* This function assumes that if the FILE ptr is valid that the
69 contents will be valid. Should we check the file for validity? */
71 /* Sounds like a good idea to me. What type of checks are you thinking
72 of, other than feof(FILE*)? That's currently the only check I can
75 int fgReadOrbElements(struct OrbElements *dest, FILE *src)
83 fgPrintf (FG_ASTRO, FG_ALERT,
84 "End of file found while reading planetary positions:\n");
89 for (i = 0; i < 256; i++)
94 /*printf("Reading line %d\n", j++); */
96 while (!(strlen(line)));
97 sscanf(line, "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf\n",
98 &dest->NFirst, &dest->NSec,
99 &dest->iFirst, &dest->iSec,
100 &dest->wFirst, &dest->wSec,
101 &dest->aFirst, &dest->aSec,
102 &dest->eFirst, &dest->eSec,
103 &dest->MFirst, &dest->MSec);
109 int fgSolarSystemInit(struct fgTIME t)
117 fgPrintf( FG_ASTRO, FG_INFO, "Initializing solar system\n");
119 /* build the full path name to the orbital elements database file */
122 strcat(path, g->root_dir);
123 strcat(path, "/Scenery/");
124 strcat(path, "Planets.dat");
126 if ( (data = fopen(path, "r")) == NULL )
128 fgPrintf( FG_ASTRO, FG_ALERT,
129 "Cannot open data file: '%s'\n", path);
131 /* printf(" reading datafile %s\n", path); */
132 fgPrintf( FG_ASTRO, FG_INFO, " reading datafile %s\n", path);
134 /* for all the objects... */
135 for (i = 0; i < 9; i ++)
137 /* ...read from the data file ... */
138 if (!(fgReadOrbElements (&pltOrbElements[i], data))) {
141 /* ...and calculate the actual values */
142 fgSolarSystemUpdate(&pltOrbElements[i], t);
150 void fgSolarSystemUpdate(struct OrbElements *planet, struct fgTIME t)
155 actTime = fgCalcActTime(t);
157 /* calculate the actual orbital elements */
158 planet->M = DEG_TO_RAD * (planet->MFirst + (planet->MSec * actTime));
159 planet->w = DEG_TO_RAD * (planet->wFirst + (planet->wSec * actTime));
160 planet->N = DEG_TO_RAD * (planet->NFirst + (planet->NSec * actTime));
161 planet->i = DEG_TO_RAD * (planet->iFirst + (planet->iSec * actTime));
162 planet->e = planet->eFirst + (planet->eSec * actTime);
163 planet->a = planet->aFirst + (planet->aSec * actTime);
168 /* Revision 1.10 1998/04/18 04:13:57 curt
169 /* Moved fg_debug.c to it's own library.
171 * Revision 1.9 1998/03/14 00:27:12 curt
172 * Updated fgGENERAL to a "type" of struct.
174 * Revision 1.8 1998/02/23 19:07:55 curt
175 * Incorporated Durk's Astro/ tweaks. Includes unifying the sun position
176 * calculation code between sun display, and other FG sections that use this
177 * for things like lighting.
179 * Revision 1.7 1998/02/12 21:59:33 curt
180 * Incorporated code changes contributed by Charlie Hotchkiss
181 * <chotchkiss@namg.us.anritsu.com>
183 * Revision 1.6 1998/02/03 23:20:11 curt
184 * Lots of little tweaks to fix various consistency problems discovered by
185 * Solaris' CC. Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
186 * passed arguments along to the real printf(). Also incorporated HUD changes
187 * by Michele America.
189 * Revision 1.5 1998/02/02 20:53:22 curt
192 * Revision 1.4 1998/01/27 00:47:47 curt
193 * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
194 * system and commandline/config file processing code.
196 * Revision 1.3 1998/01/22 02:59:27 curt
197 * Changed #ifdef FILE_H to #ifdef _FILE_H
199 * Revision 1.2 1998/01/19 19:26:58 curt
200 * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
201 * This should simplify things tremendously.
203 * Revision 1.1 1998/01/07 03:16:17 curt
204 * Moved from .../Src/Scenery/ to .../Src/Astro/
206 * Revision 1.6 1997/12/30 20:47:52 curt
207 * Integrated new event manager with subsystem initializations.
209 * Revision 1.5 1997/12/15 23:55:02 curt
210 * Add xgl wrappers for debugging.
211 * Generate terrain normals on the fly.
213 * Revision 1.4 1997/12/10 22:37:51 curt
214 * Prepended "fg" on the name of all global structures that didn't have it yet.
215 * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
217 * Revision 1.3 1997/11/25 23:20:44 curt
218 * Changed planets.dat Planets.dat
220 * Revision 1.2 1997/11/25 19:25:36 curt
221 * Changes to integrate Durk's moon/sun code updates + clean up.
223 * Revision 1.1 1997/10/25 03:16:10 curt
224 * Initial revision of code contributed by Durk Talsma.