1 // FGAIStorm - FGAIBase-derived class creates an AI thunderstorm or cloud
3 // Written by David Culp, started Feb 2004.
5 // Copyright (C) 2004 David P. Culp - davidculp2@comcast.net
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.
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.
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.
25 #include <simgear/math/point3d.hxx>
26 #include <Main/fg_props.hxx>
27 #include <Main/globals.hxx>
28 #include <Scenery/scenery.hxx>
34 #include "AIStorm.hxx"
37 FGAIStorm::FGAIStorm(FGAIManager* mgr) {
39 _type_str = "thunderstorm";
44 FGAIStorm::~FGAIStorm() {
48 bool FGAIStorm::init() {
49 return FGAIBase::init();
52 void FGAIStorm::bind() {
56 void FGAIStorm::unbind() {
61 void FGAIStorm::update(double dt) {
68 void FGAIStorm::Run(double dt) {
72 double speed_north_deg_sec;
73 double speed_east_deg_sec;
74 double ft_per_deg_lon;
75 double ft_per_deg_lat;
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);
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;
88 pos.setlat( pos.lat() + speed_north_deg_sec * dt);
89 pos.setlon( pos.lon() + speed_east_deg_sec * dt);
91 double altitude_ft = altitude * SG_METER_TO_FEET;
93 //###########################//
94 // do calculations for radar //
95 //###########################//
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();
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;
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;
118 bearing = 270.0 + bearing;
121 bearing = atan2(lon_range, lat_range) * SG_RADIANS_TO_DEGREES;
122 if (pos.lon() >= user_longitude) {
123 bearing = 180.0 - bearing;
125 bearing = 180.0 + bearing;
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;
134 // calculate elevation to target
135 elevation = atan2( altitude_ft - user_altitude, range_ft )
136 * SG_RADIANS_TO_DEGREES;
138 // calculate look up/down to target
139 vert_offset = elevation + user_pitch;
141 // now correct look left/right for yaw
142 horiz_offset += user_yaw;
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;