]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIStorm.cxx
Roy Vegard Ovesen:
[flightgear.git] / src / AIModel / AIStorm.cxx
1 // FGAIStorm - FGAIBase-derived class creates an AI thunderstorm or cloud
2 //
3 // Written by David Culp, started Feb 2004.
4 //
5 // Copyright (C) 2004  David P. Culp - davidculp2@comcast.net
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 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <simgear/math/point3d.hxx>
26 #include <Main/fg_props.hxx>
27 #include <Main/globals.hxx>
28 #include <Scenery/scenery.hxx>
29 #include <string>
30 #include <math.h>
31
32 SG_USING_STD(string);
33
34 #include "AIStorm.hxx"
35
36
37 FGAIStorm *FGAIStorm::_self = NULL;
38
39 FGAIStorm::FGAIStorm(FGAIManager* mgr) {
40    manager = mgr;   
41    _self = this;
42    _type_str = "thunderstorm";
43    _otype = otStorm;
44 }
45
46
47 FGAIStorm::~FGAIStorm() {
48     _self = NULL;
49 }
50
51
52 bool FGAIStorm::init() {
53    return FGAIBase::init();
54 }
55
56 void FGAIStorm::bind() {
57     FGAIBase::bind();
58 }
59
60 void FGAIStorm::unbind() {
61     FGAIBase::unbind();
62 }
63
64
65 void FGAIStorm::update(double dt) {
66    Run(dt);
67    Transform();
68    FGAIBase::update(dt);
69 }
70
71
72 void FGAIStorm::Run(double dt) {
73
74    FGAIStorm::dt = dt;
75         
76    double speed_north_deg_sec;
77    double speed_east_deg_sec;
78    double ft_per_deg_lon;
79    double ft_per_deg_lat;
80
81    // get size of a degree at this latitude
82    ft_per_deg_lat = 366468.96 - 3717.12 * cos(pos.lat()/SG_RADIANS_TO_DEGREES);
83    ft_per_deg_lon = 365228.16 * cos(pos.lat() / SG_RADIANS_TO_DEGREES);
84
85    // convert speed to degrees per second
86    speed_north_deg_sec = cos( hdg / SG_RADIANS_TO_DEGREES )
87                           * speed * 1.686 / ft_per_deg_lat;
88    speed_east_deg_sec  = sin( hdg / SG_RADIANS_TO_DEGREES )
89                           * speed * 1.686 / ft_per_deg_lon;
90
91    // set new position
92    pos.setlat( pos.lat() + speed_north_deg_sec * dt);
93    pos.setlon( pos.lon() + speed_east_deg_sec * dt); 
94
95    double altitude_ft = altitude * SG_METER_TO_FEET;
96
97    //###########################//
98    // do calculations for radar //
99    //###########################//
100
101    // copy values from the AIManager
102    double user_latitude  = manager->get_user_latitude();
103    double user_longitude = manager->get_user_longitude();
104    double user_altitude  = manager->get_user_altitude();
105    double user_heading   = manager->get_user_heading();
106    double user_pitch     = manager->get_user_pitch();
107    double user_yaw       = manager->get_user_yaw();
108    double user_speed     = manager->get_user_speed();
109
110    // calculate range to target in feet and nautical miles
111    double lat_range = fabs(pos.lat() - user_latitude) * ft_per_deg_lat;
112    double lon_range = fabs(pos.lon() - user_longitude) * ft_per_deg_lon;
113    double range_ft = sqrt( lat_range*lat_range + lon_range*lon_range );
114    range = range_ft / 6076.11549;
115
116    // calculate bearing to target
117    if (pos.lat() >= user_latitude) {   
118       bearing = atan2(lat_range, lon_range) * SG_RADIANS_TO_DEGREES;
119         if (pos.lon() >= user_longitude) {
120            bearing = 90.0 - bearing;
121         } else {
122            bearing = 270.0 + bearing;
123         }
124    } else {
125       bearing = atan2(lon_range, lat_range) * SG_RADIANS_TO_DEGREES;
126         if (pos.lon() >= user_longitude) {
127            bearing = 180.0 - bearing;
128         } else {
129            bearing = 180.0 + bearing;
130         }
131    }
132
133    // calculate look left/right to target, without yaw correction
134    horiz_offset = bearing - user_heading;
135    if (horiz_offset > 180.0) horiz_offset -= 360.0;
136    if (horiz_offset < -180.0) horiz_offset += 360.0;
137
138    // calculate elevation to target
139    elevation = atan2( altitude_ft - user_altitude, range_ft )
140                       * SG_RADIANS_TO_DEGREES;
141    
142    // calculate look up/down to target
143    vert_offset = elevation + user_pitch;
144
145    // now correct look left/right for yaw
146    horiz_offset += user_yaw;
147
148    // calculate values for radar display
149    y_shift = range * cos( horiz_offset * SG_DEGREES_TO_RADIANS);
150    x_shift = range * sin( horiz_offset * SG_DEGREES_TO_RADIANS);
151    rotation = hdg - user_heading;
152    if (rotation < 0.0) rotation += 360.0; 
153
154 }
155