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 "../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 */
47 actTime = 367 * year - 7 *
48 (year + (t.gmt->tm_mon + 9) / 12) / 4 + 275 *
49 t.gmt->tm_mon / 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);
54 printf("current day = %f\t", actTime);
55 printf("GMT = %d, %d, %d, %d, %d, %d\n", year, t.gmt->tm_mon, t.gmt->tm_mday,
56 t.gmt->tm_hour, t.gmt->tm_min, t.gmt->tm_sec);
60 /* convert degrees to radians */
61 double fgDegToRad(double angle)
63 return (angle * PIOVER180);
66 double fgCalcEccAnom(double M, double e)
69 eccAnom, E0, E1, diff;
71 eccAnom = M + e * sin(M) * (1.0 + e * cos(M));
72 /* iterate to achieve a greater precision for larger eccentricities */
78 E1 = E0 - (E0 - e * sin(E0) - M) / (1 - e * cos(E0));
82 while (diff > fgDegToRad(0.001));
90 void fgReadOrbElements(struct OrbElements *dest, FILE *src)
98 for (i = 0; i < 256; i++)
103 /*printf("Reading line %d\n", j++); */
105 while (!(strlen(line)));
106 sscanf(line, "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf\n",
107 &dest->NFirst, &dest->NSec,
108 &dest->iFirst, &dest->iSec,
109 &dest->wFirst, &dest->wSec,
110 &dest->aFirst, &dest->aSec,
111 &dest->eFirst, &dest->eSec,
112 &dest->MFirst, &dest->MSec);
116 void fgSolarSystemInit(struct fgTIME t)
123 /* build the full path name to the orbital elements database file */
126 strcat(path, g->root_dir);
127 strcat(path, "/Scenery/");
128 strcat(path, "planets.dat");
130 if ( (data = fopen(path, "r")) == NULL )
132 printf("Cannot open data file: '%s'\n", path);
135 printf("reading datafile %s", path);
137 /* for all the objects... */
138 for (i = 0; i < 9; i ++)
140 /* ...read from the data file ... */
141 fgReadOrbElements(&pltOrbElements[i], data);
142 /* ...and calculate the actual values */
143 fgSolarSystemUpdate(&pltOrbElements[i], t);
149 void fgSolarSystemUpdate(struct OrbElements *planet, struct fgTIME t)
154 actTime = fgCalcActTime(t);
156 /* calculate the actual orbital elements */
157 planet->M = fgDegToRad(planet->MFirst + (planet->MSec * actTime)); // angle in radians
158 planet->w = fgDegToRad(planet->wFirst + (planet->wSec * actTime)); // angle in radians
159 planet->N = fgDegToRad(planet->NFirst + (planet->NSec * actTime)); // angle in radians
160 planet->i = fgDegToRad(planet->iFirst + (planet->iSec * actTime)); // angle in radians
161 planet->e = planet->eFirst + (planet->eSec * actTime);
162 planet->a = planet->aFirst + (planet->aSec * actTime);
167 /* Revision 1.1 1997/10/25 03:16:10 curt
168 /* Initial revision of code contributed by Durk Talsma.