]> git.mxchange.org Git - flightgear.git/blob - Astro/orbits.c
5ce2ae9405737b043bd2a628ee3b5b30b9fa8243
[flightgear.git] / Astro / orbits.c
1 /**************************************************************************
2  * orbits.c - calculates the orbital elements of the sun, moon and planets.
3  *            For inclusion in flight gear
4  *
5  * Written 1997 by Durk Talsma, started October 19, 1997.
6  *
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.
11  *
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.
16  *
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.
20  *
21  * $Id$
22  * (Log is kept at end of this file)
23  **************************************************************************/
24
25
26 #include <string.h>
27
28 #include <Astro/orbits.h>
29
30 #include <Include/general.h>
31 #include <Time/fg_time.h>
32
33
34 struct OrbElements pltOrbElements[9];
35
36
37 double fgCalcActTime(struct fgTIME t)
38 {
39    double
40          actTime, UT;
41    int year;
42
43    /* a hack. This one introduces the 2000 problem into the program */
44    year = t.gmt->tm_year + 1900;
45
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;
50
51     UT = t.gmt->tm_hour + ((double) t.gmt->tm_min / 60);
52     /*printf("UT = %f\n", UT); */
53     actTime += (UT / 24.0);
54     #define DEBUG 1
55     #ifdef DEBUG
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); */
61     #endif
62     return actTime;
63 }
64
65 /* convert degrees to radians */
66 double fgDegToRad(double angle)
67 {
68         return (angle * PIOVER180);
69 }
70
71 double fgCalcEccAnom(double M, double e)
72 {
73     double
74         eccAnom, E0, E1, diff;
75
76     eccAnom = M + e * sin(M) * (1.0 + e * cos(M));
77     /* iterate to achieve a greater precision for larger eccentricities */
78     if (e > 0.05)
79     {
80         E0 = eccAnom;
81         do
82         {
83                 E1 = E0 - (E0 - e * sin(E0) - M) / (1 - e * cos(E0));
84             diff = abs(E0 - E1);
85             E0 = E1;
86                 }
87         while (diff > fgDegToRad(0.001));
88         return E0;
89         }
90     return eccAnom;
91 }
92
93
94
95 void fgReadOrbElements(struct OrbElements *dest, FILE *src)
96 {
97         char line[256];
98     int i,j;
99     j = 0;
100     do
101     {
102         fgets(line, 256,src);
103         for (i = 0; i < 256; i++)
104         {
105                 if (line[i] == '#')
106                 line[i] = 0;
107         }
108         /*printf("Reading line %d\n", j++); */
109     }
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);
118 }
119
120
121 void fgSolarSystemInit(struct fgTIME t)
122 {
123    struct fgGENERAL *g;
124    char path[80];
125    int i;
126    FILE *data;
127
128    printf("Initializing solar system\n");
129
130    /* build the full path name to the orbital elements database file */
131    g = &general;
132    path[0] = '\0';
133    strcat(path, g->root_dir);
134    strcat(path, "/Scenery/");
135    strcat(path, "Planets.dat");
136
137    if ( (data = fopen(path, "r")) == NULL )
138    {
139             printf("Cannot open data file: '%s'\n", path);
140             return;
141    }
142    #ifdef DEBUG
143    printf("  reading datafile %s\n", path);
144    #endif
145
146    /* for all the objects... */
147    for (i = 0; i < 9; i ++)
148    {
149       /* ...read from the data file ... */
150       fgReadOrbElements(&pltOrbElements[i], data);
151       /* ...and calculate the actual values */
152       fgSolarSystemUpdate(&pltOrbElements[i], t);
153    }
154
155 }
156
157
158 void fgSolarSystemUpdate(struct OrbElements *planet, struct fgTIME t)
159 {
160    double
161          actTime;
162
163    actTime = fgCalcActTime(t);
164
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);
172 }
173
174
175 /* $Log$
176 /* Revision 1.2  1998/01/19 19:26:58  curt
177 /* Merged in make system changes from Bob Kuehne <rpk@sgi.com>
178 /* This should simplify things tremendously.
179 /*
180  * Revision 1.1  1998/01/07 03:16:17  curt
181  * Moved from .../Src/Scenery/ to .../Src/Astro/
182  *
183  * Revision 1.6  1997/12/30 20:47:52  curt
184  * Integrated new event manager with subsystem initializations.
185  *
186  * Revision 1.5  1997/12/15 23:55:02  curt
187  * Add xgl wrappers for debugging.
188  * Generate terrain normals on the fly.
189  *
190  * Revision 1.4  1997/12/10 22:37:51  curt
191  * Prepended "fg" on the name of all global structures that didn't have it yet.
192  * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
193  *
194  * Revision 1.3  1997/11/25 23:20:44  curt
195  * Changed planets.dat Planets.dat
196  *
197  * Revision 1.2  1997/11/25 19:25:36  curt
198  * Changes to integrate Durk's moon/sun code updates + clean up.
199  *
200  * Revision 1.1  1997/10/25 03:16:10  curt
201  * Initial revision of code contributed by Durk Talsma.
202  *
203  */