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