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 **************************************************************************/
30 #include "../Include/general.h"
31 #include "../Time/fg_time.h"
34 struct OrbElements pltOrbElements[9];
37 double fgCalcActTime(struct fgTIME t)
43 /* a hack. This one introduces the 2000 problem into the program */
44 year = t.gmt->tm_year + 1900;
46 /* calculate the actual time, rember to add 1 to tm_mon! */
47 actTime = 367 * year - 7 *
48 (year + ((t.gmt->tm_mon+1) + 9) / 12) / 4 + 275 *
49 (t.gmt->tm_mon+1) / 9 + t.gmt->tm_mday - 730530;
51 UT = t.gmt->tm_hour + ((double) t.gmt->tm_min / 60);
52 /*printf("UT = %f\n", UT); */
53 actTime += (UT / 24.0);
56 /* printf(" Actual Time:\n"); */
57 /* printf(" current day = %f\t", actTime); */
58 /* printf(" GMT = %d, %d, %d, %d, %d, %d\n",
59 year, t.gmt->tm_mon, t.gmt->tm_mday,
60 t.gmt->tm_hour, t.gmt->tm_min, t.gmt->tm_sec); */
65 /* convert degrees to radians */
66 double fgDegToRad(double angle)
68 return (angle * PIOVER180);
71 double fgCalcEccAnom(double M, double e)
74 eccAnom, E0, E1, diff;
76 eccAnom = M + e * sin(M) * (1.0 + e * cos(M));
77 /* iterate to achieve a greater precision for larger eccentricities */
83 E1 = E0 - (E0 - e * sin(E0) - M) / (1 - e * cos(E0));
87 while (diff > fgDegToRad(0.001));
95 void fgReadOrbElements(struct OrbElements *dest, FILE *src)
102 fgets(line, 256,src);
103 for (i = 0; i < 256; i++)
108 /*printf("Reading line %d\n", j++); */
110 while (!(strlen(line)));
111 sscanf(line, "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf\n",
112 &dest->NFirst, &dest->NSec,
113 &dest->iFirst, &dest->iSec,
114 &dest->wFirst, &dest->wSec,
115 &dest->aFirst, &dest->aSec,
116 &dest->eFirst, &dest->eSec,
117 &dest->MFirst, &dest->MSec);
121 void fgSolarSystemInit(struct fgTIME t)
128 printf("Initializing solar system\n");
130 /* build the full path name to the orbital elements database file */
133 strcat(path, g->root_dir);
134 strcat(path, "/Scenery/");
135 strcat(path, "Planets.dat");
137 if ( (data = fopen(path, "r")) == NULL )
139 printf("Cannot open data file: '%s'\n", path);
143 printf(" reading datafile %s\n", path);
146 /* for all the objects... */
147 for (i = 0; i < 9; i ++)
149 /* ...read from the data file ... */
150 fgReadOrbElements(&pltOrbElements[i], data);
151 /* ...and calculate the actual values */
152 fgSolarSystemUpdate(&pltOrbElements[i], t);
158 void fgSolarSystemUpdate(struct OrbElements *planet, struct fgTIME t)
163 actTime = fgCalcActTime(t);
165 /* calculate the actual orbital elements */
166 planet->M = fgDegToRad(planet->MFirst + (planet->MSec * actTime)); // angle in radians
167 planet->w = fgDegToRad(planet->wFirst + (planet->wSec * actTime)); // angle in radians
168 planet->N = fgDegToRad(planet->NFirst + (planet->NSec * actTime)); // angle in radians
169 planet->i = fgDegToRad(planet->iFirst + (planet->iSec * actTime)); // angle in radians
170 planet->e = planet->eFirst + (planet->eSec * actTime);
171 planet->a = planet->aFirst + (planet->aSec * actTime);
176 /* Revision 1.1 1998/01/07 03:16:17 curt
177 /* Moved from .../Src/Scenery/ to .../Src/Astro/
179 * Revision 1.6 1997/12/30 20:47:52 curt
180 * Integrated new event manager with subsystem initializations.
182 * Revision 1.5 1997/12/15 23:55:02 curt
183 * Add xgl wrappers for debugging.
184 * Generate terrain normals on the fly.
186 * Revision 1.4 1997/12/10 22:37:51 curt
187 * Prepended "fg" on the name of all global structures that didn't have it yet.
188 * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
190 * Revision 1.3 1997/11/25 23:20:44 curt
191 * Changed planets.dat Planets.dat
193 * Revision 1.2 1997/11/25 19:25:36 curt
194 * Changes to integrate Durk's moon/sun code updates + clean up.
196 * Revision 1.1 1997/10/25 03:16:10 curt
197 * Initial revision of code contributed by Durk Talsma.