]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIThermal.cxx
Roy Vegard Ovesen:
[flightgear.git] / src / AIModel / AIThermal.cxx
1 // FGAIThermal - FGAIBase-derived class creates an AI thermal
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 "AIThermal.hxx"
35
36
37 FGAIThermal *FGAIThermal::_self = NULL;
38
39 FGAIThermal::FGAIThermal(FGAIManager* mgr) {
40    manager = mgr;   
41    _self = this;
42    _type_str = "thermal";
43    _otype = otThermal;
44    max_strength = 6.0;
45    diameter = 0.5;
46    strength = factor = 0.0;
47 }
48
49
50 FGAIThermal::~FGAIThermal() {
51     _self = NULL;
52 }
53
54
55 bool FGAIThermal::init() {
56    factor = 8.0 * max_strength / (diameter * diameter * diameter);
57    return FGAIBase::init();
58 }
59
60 void FGAIThermal::bind() {
61     FGAIBase::bind();
62 }
63
64 void FGAIThermal::unbind() {
65     FGAIBase::unbind();
66 }
67
68
69 void FGAIThermal::update(double dt) {
70    Run(dt);
71    Transform();
72    FGAIBase::update(dt);
73 }
74
75
76 void FGAIThermal::Run(double dt) {
77
78    FGAIThermal::dt = dt;
79         
80    double ft_per_deg_lon;
81    double ft_per_deg_lat;
82
83    // get size of a degree at this latitude
84    ft_per_deg_lat = 366468.96 - 3717.12 * cos(pos.lat()/SG_RADIANS_TO_DEGREES);
85    ft_per_deg_lon = 365228.16 * cos(pos.lat() / SG_RADIANS_TO_DEGREES);
86
87    double altitude_ft = altitude * SG_METER_TO_FEET;
88
89    //###########################//
90    // do calculations for range //
91    //###########################//
92
93    // copy values from the AIManager
94    double user_latitude  = manager->get_user_latitude();
95    double user_longitude = manager->get_user_longitude();
96    double user_altitude  = manager->get_user_altitude();
97
98    // calculate range to target in feet and nautical miles
99    double lat_range = fabs(pos.lat() - user_latitude) * ft_per_deg_lat;
100    double lon_range = fabs(pos.lon() - user_longitude) * ft_per_deg_lon;
101    double range_ft = sqrt( lat_range*lat_range + lon_range*lon_range );
102    range = range_ft / 6076.11549;
103
104    // Calculate speed of rising air if within range. 
105    // Air vertical speed is maximum at center of thermal,
106    // and decreases to zero at the edge (as distance cubed).
107    if (range < (diameter * 0.5)) {
108      strength = max_strength - ( range * range * range * factor );
109    } else {
110      strength = 0.0;
111    }
112 }
113