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::_self = NULL;
39 FGAIStorm::FGAIStorm(FGAIManager* mgr) {
42 _type_str = "thunderstorm";
47 FGAIStorm::~FGAIStorm() {
52 bool FGAIStorm::init() {
53 return FGAIBase::init();
56 void FGAIStorm::bind() {
60 void FGAIStorm::unbind() {
65 void FGAIStorm::update(double dt) {
72 void FGAIStorm::Run(double dt) {
76 double speed_north_deg_sec;
77 double speed_east_deg_sec;
78 double ft_per_deg_lon;
79 double ft_per_deg_lat;
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);
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;
92 pos.setlat( pos.lat() + speed_north_deg_sec * dt);
93 pos.setlon( pos.lon() + speed_east_deg_sec * dt);
95 double altitude_ft = altitude * SG_METER_TO_FEET;
97 //###########################//
98 // do calculations for radar //
99 //###########################//
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();
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;
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;
122 bearing = 270.0 + bearing;
125 bearing = atan2(lon_range, lat_range) * SG_RADIANS_TO_DEGREES;
126 if (pos.lon() >= user_longitude) {
127 bearing = 180.0 - bearing;
129 bearing = 180.0 + bearing;
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;
138 // calculate elevation to target
139 elevation = atan2( altitude_ft - user_altitude, range_ft )
140 * SG_RADIANS_TO_DEGREES;
142 // calculate look up/down to target
143 vert_offset = elevation + user_pitch;
145 // now correct look left/right for yaw
146 horiz_offset += user_yaw;
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;