]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment.cxx
59a26ec11aa6247b13f2c71384816777f7b1122f
[flightgear.git] / src / Environment / environment.cxx
1 // environment.cxx -- routines to model the natural environment
2 //
3 // Written by David Megginson, started February 2002.
4 //
5 // Copyright (C) 2002  David Megginson - david@megginson.com
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
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #ifdef HAVE_WINDOWS_H
29 #  include <windows.h>                     
30 #endif
31
32 #include <math.h>
33
34 #include <simgear/debug/logstream.hxx>
35
36 #include <Main/fg_props.hxx>
37 #include "environment.hxx"
38
39
40 FGEnvironment::FGEnvironment()
41   : visibility_m(32000),
42     wind_from_heading_deg(0),
43     wind_speed_kt(0),
44     wind_from_north_fps(0),
45     wind_from_east_fps(0),
46     wind_from_down_fps(0)
47 {
48 }
49
50
51 FGEnvironment::~FGEnvironment()
52 {
53 }
54
55
56 void
57 FGEnvironment::set_visibility_m (double v)
58 {
59   visibility_m = v;
60 }
61
62 void
63 FGEnvironment::set_wind_from_heading_deg (double h)
64 {
65   wind_from_heading_deg = h;
66   _recalc_ne();
67 }
68
69 void
70 FGEnvironment::set_wind_speed_kt (double s)
71 {
72   wind_speed_kt = s;
73   _recalc_ne();
74 }
75
76 void
77 FGEnvironment::set_wind_from_north_fps (double n)
78 {
79   wind_from_north_fps = n;
80   _recalc_hdgspd();
81 }
82
83 void
84 FGEnvironment::set_wind_from_east_fps (double e)
85 {
86   wind_from_east_fps = e;
87   _recalc_hdgspd();
88 }
89
90 void
91 FGEnvironment::set_wind_from_down_fps (double d)
92 {
93   wind_from_down_fps = d;
94   _recalc_hdgspd();
95 }
96
97 void
98 FGEnvironment::_recalc_hdgspd ()
99 {
100   double angle_rad;
101
102   if (wind_from_east_fps == 0) {
103     angle_rad = (wind_from_north_fps >= 0 ? SGD_PI/2 : -SGD_PI/2);
104   } else {
105     angle_rad = atan(wind_from_north_fps/wind_from_east_fps);
106   }
107   wind_from_heading_deg = angle_rad * SGD_RADIANS_TO_DEGREES;
108   if (wind_from_east_fps >= 0)
109     wind_from_heading_deg = 90 - wind_from_heading_deg;
110   else
111     wind_from_heading_deg = 270 - wind_from_heading_deg;
112   if (angle_rad == 0)
113     wind_speed_kt = fabs(wind_from_east_fps
114                          * SG_METER_TO_NM * SG_FEET_TO_METER * 3600);
115   else
116     wind_speed_kt = (wind_from_north_fps / sin(angle_rad))
117       * SG_METER_TO_NM * SG_FEET_TO_METER * 3600;
118 }
119
120 void
121 FGEnvironment::_recalc_ne ()
122 {
123   double speed_fps =
124     wind_speed_kt * SG_NM_TO_METER * SG_METER_TO_FEET * (1.0/3600);
125
126   wind_from_north_fps = speed_fps *
127     cos(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
128   wind_from_east_fps = speed_fps *
129     sin(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
130 }
131
132 // end of environment.cxx
133