1 // FGAIThermal - FGAIBase-derived class creates an AI thermal
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 "AIThermal.hxx"
37 FGAIThermal *FGAIThermal::_self = NULL;
39 FGAIThermal::FGAIThermal(FGAIManager* mgr) {
42 _type_str = "thermal";
48 FGAIThermal::~FGAIThermal() {
53 bool FGAIThermal::init() {
54 wind_from_down = fgGetNode("/environment/wind-from-down-fps", true);
55 scaler = 8.0 * strength / (diameter * diameter * diameter);
56 return FGAIBase::init();
59 void FGAIThermal::bind() {
63 void FGAIThermal::unbind() {
68 void FGAIThermal::update(double dt) {
75 void FGAIThermal::Run(double dt) {
79 double ft_per_deg_lon;
80 double ft_per_deg_lat;
82 // get size of a degree at this latitude
83 ft_per_deg_lat = 366468.96 - 3717.12 * cos(pos.lat()/SG_RADIANS_TO_DEGREES);
84 ft_per_deg_lon = 365228.16 * cos(pos.lat() / SG_RADIANS_TO_DEGREES);
86 double altitude_ft = altitude * SG_METER_TO_FEET;
88 //###########################//
89 // do calculations for range //
90 //###########################//
92 // copy values from the AIManager
93 double user_latitude = manager->get_user_latitude();
94 double user_longitude = manager->get_user_longitude();
95 double user_altitude = manager->get_user_altitude();
97 // calculate range to target in feet and nautical miles
98 double lat_range = fabs(pos.lat() - user_latitude) * ft_per_deg_lat;
99 double lon_range = fabs(pos.lon() - user_longitude) * ft_per_deg_lon;
100 double range_ft = sqrt( lat_range*lat_range + lon_range*lon_range );
101 range = range_ft / 6076.11549;
103 // Set rising air if within range.
104 // Air vertical speed is maximum at center of thermal,
105 // and decreases to zero at the edge (as distance cubed).
106 if (range < (diameter * 0.5)) {
107 wind_from_down->setDoubleValue( strength - (range * range * range * scaler) );
109 wind_from_down->setDoubleValue( 0.0 );