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", year, t.gmt->tm_mon, t.gmt->tm_mday,
59 t.gmt->tm_hour, t.gmt->tm_min, t.gmt->tm_sec);
64 /* convert degrees to radians */
65 double fgDegToRad(double angle)
67 return (angle * PIOVER180);
70 double fgCalcEccAnom(double M, double e)
73 eccAnom, E0, E1, diff;
75 eccAnom = M + e * sin(M) * (1.0 + e * cos(M));
76 /* iterate to achieve a greater precision for larger eccentricities */
82 E1 = E0 - (E0 - e * sin(E0) - M) / (1 - e * cos(E0));
86 while (diff > fgDegToRad(0.001));
94 void fgReadOrbElements(struct OrbElements *dest, FILE *src)
101 fgets(line, 256,src);
102 for (i = 0; i < 256; i++)
107 /*printf("Reading line %d\n", j++); */
109 while (!(strlen(line)));
110 sscanf(line, "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf\n",
111 &dest->NFirst, &dest->NSec,
112 &dest->iFirst, &dest->iSec,
113 &dest->wFirst, &dest->wSec,
114 &dest->aFirst, &dest->aSec,
115 &dest->eFirst, &dest->eSec,
116 &dest->MFirst, &dest->MSec);
120 void fgSolarSystemInit(struct fgTIME t)
127 /* build the full path name to the orbital elements database file */
130 strcat(path, g->root_dir);
131 strcat(path, "/Scenery/");
132 strcat(path, "Planets.dat");
134 if ( (data = fopen(path, "r")) == NULL )
136 printf("Cannot open data file: '%s'\n", path);
140 printf("reading datafile %s", path);
143 /* for all the objects... */
144 for (i = 0; i < 9; i ++)
146 /* ...read from the data file ... */
147 fgReadOrbElements(&pltOrbElements[i], data);
148 /* ...and calculate the actual values */
149 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 = fgDegToRad(planet->MFirst + (planet->MSec * actTime)); // angle in radians
164 planet->w = fgDegToRad(planet->wFirst + (planet->wSec * actTime)); // angle in radians
165 planet->N = fgDegToRad(planet->NFirst + (planet->NSec * actTime)); // angle in radians
166 planet->i = fgDegToRad(planet->iFirst + (planet->iSec * actTime)); // angle in radians
167 planet->e = planet->eFirst + (planet->eSec * actTime);
168 planet->a = planet->aFirst + (planet->aSec * actTime);
173 /* Revision 1.5 1997/12/15 23:55:02 curt
174 /* Add xgl wrappers for debugging.
175 /* Generate terrain normals on the fly.
177 * Revision 1.4 1997/12/10 22:37:51 curt
178 * Prepended "fg" on the name of all global structures that didn't have it yet.
179 * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
181 * Revision 1.3 1997/11/25 23:20:44 curt
182 * Changed planets.dat Planets.dat
184 * Revision 1.2 1997/11/25 19:25:36 curt
185 * Changes to integrate Durk's moon/sun code updates + clean up.
187 * Revision 1.1 1997/10/25 03:16:10 curt
188 * Initial revision of code contributed by Durk Talsma.