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 **************************************************************************/
33 #include <Debug/fg_debug.h>
34 #include <Include/fg_constants.h>
35 #include <Include/general.h>
36 #include <Time/fg_time.hxx>
41 struct OrbElements pltOrbElements[9];
44 double fgCalcActTime(struct fgTIME t)
46 return (t.mjd - 36523.5);
50 double fgCalcEccAnom(double M, double e)
53 eccAnom, E0, E1, diff;
55 eccAnom = M + e * sin(M) * (1.0 + e * cos(M));
56 /* iterate to achieve a greater precision for larger eccentricities */
62 E1 = E0 - (E0 - e * sin(E0) - M) / (1 - e * cos(E0));
66 while (diff > (DEG_TO_RAD * 0.001));
73 /* This function assumes that if the FILE ptr is valid that the
74 contents will be valid. Should we check the file for validity? */
76 /* Sounds like a good idea to me. What type of checks are you thinking
77 of, other than feof(FILE*)? That's currently the only check I can
80 int fgReadOrbElements(struct OrbElements *dest, FILE *src)
88 fgPrintf (FG_ASTRO, FG_ALERT,
89 "End of file found while reading planetary positions:\n");
94 for (i = 0; i < 256; i++)
99 /*printf("Reading line %d\n", j++); */
101 while (!(strlen(line)));
102 sscanf(line, "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf\n",
103 &dest->NFirst, &dest->NSec,
104 &dest->iFirst, &dest->iSec,
105 &dest->wFirst, &dest->wSec,
106 &dest->aFirst, &dest->aSec,
107 &dest->eFirst, &dest->eSec,
108 &dest->MFirst, &dest->MSec);
114 int fgSolarSystemInit(struct fgTIME t)
122 fgPrintf( FG_ASTRO, FG_INFO, "Initializing solar system\n");
124 /* build the full path name to the orbital elements database file */
127 strcat(path, g->root_dir);
128 strcat(path, "/Scenery/");
129 strcat(path, "Planets.dat");
131 if ( (data = fopen(path, "r")) == NULL )
133 fgPrintf( FG_ASTRO, FG_ALERT,
134 "Cannot open data file: '%s'\n", path);
136 /* printf(" reading datafile %s\n", path); */
137 fgPrintf( FG_ASTRO, FG_INFO, " reading datafile %s\n", path);
139 /* for all the objects... */
140 for (i = 0; i < 9; i ++)
142 /* ...read from the data file ... */
143 if (!(fgReadOrbElements (&pltOrbElements[i], data))) {
146 /* ...and calculate the actual values */
147 fgSolarSystemUpdate(&pltOrbElements[i], t);
155 void fgSolarSystemUpdate(struct OrbElements *planet, struct fgTIME t)
160 actTime = fgCalcActTime(t);
162 /* calculate the actual orbital elements */
163 planet->M = DEG_TO_RAD * (planet->MFirst + (planet->MSec * actTime));
164 planet->w = DEG_TO_RAD * (planet->wFirst + (planet->wSec * actTime));
165 planet->N = DEG_TO_RAD * (planet->NFirst + (planet->NSec * actTime));
166 planet->i = DEG_TO_RAD * (planet->iFirst + (planet->iSec * actTime));
167 planet->e = planet->eFirst + (planet->eSec * actTime);
168 planet->a = planet->aFirst + (planet->aSec * actTime);
173 /* Revision 1.2 1998/04/24 00:45:01 curt
174 /* Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
175 /* Fixed a bug when generating sky colors.
177 * Revision 1.1 1998/04/22 13:21:29 curt
178 * C++ - ifing the code a bit.
180 * Revision 1.10 1998/04/18 04:13:57 curt
181 * Moved fg_debug.c to it's own library.
183 * Revision 1.9 1998/03/14 00:27:12 curt
184 * Updated fgGENERAL to a "type" of struct.
186 * Revision 1.8 1998/02/23 19:07:55 curt
187 * Incorporated Durk's Astro/ tweaks. Includes unifying the sun position
188 * calculation code between sun display, and other FG sections that use this
189 * for things like lighting.
191 * Revision 1.7 1998/02/12 21:59:33 curt
192 * Incorporated code changes contributed by Charlie Hotchkiss
193 * <chotchkiss@namg.us.anritsu.com>
195 * Revision 1.6 1998/02/03 23:20:11 curt
196 * Lots of little tweaks to fix various consistency problems discovered by
197 * Solaris' CC. Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
198 * passed arguments along to the real printf(). Also incorporated HUD changes
199 * by Michele America.
201 * Revision 1.5 1998/02/02 20:53:22 curt
204 * Revision 1.4 1998/01/27 00:47:47 curt
205 * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
206 * system and commandline/config file processing code.
208 * Revision 1.3 1998/01/22 02:59:27 curt
209 * Changed #ifdef FILE_H to #ifdef _FILE_H
211 * Revision 1.2 1998/01/19 19:26:58 curt
212 * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
213 * This should simplify things tremendously.
215 * Revision 1.1 1998/01/07 03:16:17 curt
216 * Moved from .../Src/Scenery/ to .../Src/Astro/
218 * Revision 1.6 1997/12/30 20:47:52 curt
219 * Integrated new event manager with subsystem initializations.
221 * Revision 1.5 1997/12/15 23:55:02 curt
222 * Add xgl wrappers for debugging.
223 * Generate terrain normals on the fly.
225 * Revision 1.4 1997/12/10 22:37:51 curt
226 * Prepended "fg" on the name of all global structures that didn't have it yet.
227 * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
229 * Revision 1.3 1997/11/25 23:20:44 curt
230 * Changed planets.dat Planets.dat
232 * Revision 1.2 1997/11/25 19:25:36 curt
233 * Changes to integrate Durk's moon/sun code updates + clean up.
235 * Revision 1.1 1997/10/25 03:16:10 curt
236 * Initial revision of code contributed by Durk Talsma.