]> git.mxchange.org Git - flightgear.git/blob - Scenery/orbits.c
Prepended "fg" on the name of all global structures that didn't have it yet.
[flightgear.git] / Scenery / 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 "orbits.h"
29
30 #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",           year, t.gmt->tm_mon, t.gmt->tm_mday,
59                                              t.gmt->tm_hour, t.gmt->tm_min, t.gmt->tm_sec);
60     #endif
61     return actTime;
62 }
63
64 /* convert degrees to radians */
65 double fgDegToRad(double angle)
66 {
67         return (angle * PIOVER180);
68 }
69
70 double fgCalcEccAnom(double M, double e)
71 {
72     double
73         eccAnom, E0, E1, diff;
74
75     eccAnom = M + e * sin(M) * (1.0 + e * cos(M));
76     /* iterate to achieve a greater precision for larger eccentricities */
77     if (e > 0.05)
78     {
79         E0 = eccAnom;
80         do
81         {
82                 E1 = E0 - (E0 - e * sin(E0) - M) / (1 - e * cos(E0));
83             diff = abs(E0 - E1);
84             E0 = E1;
85                 }
86         while (diff > fgDegToRad(0.001));
87         return E0;
88         }
89     return eccAnom;
90 }
91
92
93
94 void fgReadOrbElements(struct OrbElements *dest, FILE *src)
95 {
96         char line[256];
97     int i,j;
98     j = 0;
99     do
100     {
101         fgets(line, 256,src);
102         for (i = 0; i < 256; i++)
103         {
104                 if (line[i] == '#')
105                 line[i] = 0;
106         }
107         /*printf("Reading line %d\n", j++); */
108     }
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);
117 }
118
119
120 void fgSolarSystemInit(struct fgTIME t)
121 {
122    struct fgGENERAL *g;
123    char path[80];
124    int i;
125    FILE *data;
126
127    /* build the full path name to the orbital elements database file */
128    g = &general;
129    path[0] = '\0';
130    strcat(path, g->root_dir);
131    strcat(path, "/Scenery/");
132    strcat(path, "Planets.dat");
133
134    if ( (data = fopen(path, "r")) == NULL )
135    {
136             printf("Cannot open data file: '%s'\n", path);
137             return;
138    }
139    #ifdef DEBUG
140    printf("reading datafile %s", path);
141    #endif
142
143    /* for all the objects... */
144    for (i = 0; i < 9; i ++)
145    {
146       /* ...read from the data file ... */
147       fgReadOrbElements(&pltOrbElements[i], data);
148       /* ...and calculate the actual values */
149       fgSolarSystemUpdate(&pltOrbElements[i], t);
150    }
151
152 }
153
154
155 void fgSolarSystemUpdate(struct OrbElements *planet, struct fgTIME t)
156 {
157    double
158          actTime;
159
160    actTime = fgCalcActTime(t);
161
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);
169 }
170
171
172 /* $Log$
173 /* Revision 1.4  1997/12/10 22:37:51  curt
174 /* Prepended "fg" on the name of all global structures that didn't have it yet.
175 /* i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
176 /*
177  * Revision 1.3  1997/11/25 23:20:44  curt
178  * Changed planets.dat Planets.dat
179  *
180  * Revision 1.2  1997/11/25 19:25:36  curt
181  * Changes to integrate Durk's moon/sun code updates + clean up.
182  *
183  * Revision 1.1  1997/10/25 03:16:10  curt
184  * Initial revision of code contributed by Durk Talsma.
185  *
186  */