]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIStorm.cxx
06c5d1b3201623b7e0c056c27cc3a0a29139d145
[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(FGAIManager* mgr) {
38    manager = mgr;   
39    _type_str = "thunderstorm";
40    _otype = otStorm;
41 }
42
43
44 FGAIStorm::~FGAIStorm() {
45 }
46
47
48 bool FGAIStorm::init() {
49    return FGAIBase::init();
50 }
51
52 void FGAIStorm::bind() {
53     FGAIBase::bind();
54 }
55
56 void FGAIStorm::unbind() {
57     FGAIBase::unbind();
58 }
59
60
61 void FGAIStorm::update(double dt) {
62    Run(dt);
63    Transform();
64    FGAIBase::update(dt);
65 }
66
67
68 void FGAIStorm::Run(double dt) {
69
70    FGAIStorm::dt = dt;
71         
72    double speed_north_deg_sec;
73    double speed_east_deg_sec;
74    double ft_per_deg_lon;
75    double ft_per_deg_lat;
76
77    // get size of a degree at this latitude
78    ft_per_deg_lat = 366468.96 - 3717.12 * cos(pos.lat()/SG_RADIANS_TO_DEGREES);
79    ft_per_deg_lon = 365228.16 * cos(pos.lat() / SG_RADIANS_TO_DEGREES);
80
81    // convert speed to degrees per second
82    speed_north_deg_sec = cos( hdg / SG_RADIANS_TO_DEGREES )
83                           * speed * 1.686 / ft_per_deg_lat;
84    speed_east_deg_sec  = sin( hdg / SG_RADIANS_TO_DEGREES )
85                           * speed * 1.686 / ft_per_deg_lon;
86
87    // set new position
88    pos.setlat( pos.lat() + speed_north_deg_sec * dt);
89    pos.setlon( pos.lon() + speed_east_deg_sec * dt); 
90
91    double altitude_ft = altitude * SG_METER_TO_FEET;
92
93    //###########################//
94    // do calculations for radar //
95    //###########################//
96
97    // copy values from the AIManager
98    double user_latitude  = manager->get_user_latitude();
99    double user_longitude = manager->get_user_longitude();
100    double user_altitude  = manager->get_user_altitude();
101    double user_heading   = manager->get_user_heading();
102    double user_pitch     = manager->get_user_pitch();
103    double user_yaw       = manager->get_user_yaw();
104    // double user_speed     = manager->get_user_speed();
105
106    // calculate range to target in feet and nautical miles
107    double lat_range = fabs(pos.lat() - user_latitude) * ft_per_deg_lat;
108    double lon_range = fabs(pos.lon() - user_longitude) * ft_per_deg_lon;
109    double range_ft = sqrt( lat_range*lat_range + lon_range*lon_range );
110    range = range_ft / 6076.11549;
111
112    // calculate bearing to target
113    if (pos.lat() >= user_latitude) {   
114       bearing = atan2(lat_range, lon_range) * SG_RADIANS_TO_DEGREES;
115         if (pos.lon() >= user_longitude) {
116            bearing = 90.0 - bearing;
117         } else {
118            bearing = 270.0 + bearing;
119         }
120    } else {
121       bearing = atan2(lon_range, lat_range) * SG_RADIANS_TO_DEGREES;
122         if (pos.lon() >= user_longitude) {
123            bearing = 180.0 - bearing;
124         } else {
125            bearing = 180.0 + bearing;
126         }
127    }
128
129    // calculate look left/right to target, without yaw correction
130    horiz_offset = bearing - user_heading;
131    if (horiz_offset > 180.0) horiz_offset -= 360.0;
132    if (horiz_offset < -180.0) horiz_offset += 360.0;
133
134    // calculate elevation to target
135    elevation = atan2( altitude_ft - user_altitude, range_ft )
136                       * SG_RADIANS_TO_DEGREES;
137    
138    // calculate look up/down to target
139    vert_offset = elevation + user_pitch;
140
141    // now correct look left/right for yaw
142    horiz_offset += user_yaw;
143
144    // calculate values for radar display
145    y_shift = range * cos( horiz_offset * SG_DEGREES_TO_RADIANS);
146    x_shift = range * sin( horiz_offset * SG_DEGREES_TO_RADIANS);
147    rotation = hdg - user_heading;
148    if (rotation < 0.0) rotation += 360.0; 
149
150 }
151