]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIAircraft.cxx
Oops, make sure that one of the default splash screens gets used when the splash...
[flightgear.git] / src / AIModel / AIAircraft.cxx
1 // FGAIAircraft - FGAIBase-derived class creates an AI airplane
2 //
3 // Written by David Culp, started October 2003.
4 //
5 // Copyright (C) 2003  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 "AIAircraft.hxx"
35
36
37 const FGAIAircraft::PERF_STRUCT FGAIAircraft::settings[] = {
38     // light aircraft
39     {2.0, 2.0,  450.0, 1000.0,  70.0,  80.0, 100.0,  80.0,  60.0},
40     // ww2_fighter
41     {4.0, 2.0, 3000.0, 1500.0, 110.0, 180.0, 250.0, 200.0, 100.0},
42     // jet_transport
43     {5.0, 2.0, 3000.0, 1500.0, 140.0, 300.0, 430.0, 300.0, 130.0},
44     // jet_fighter
45     {7.0, 3.0, 4000.0, 2000.0, 150.0, 350.0, 500.0, 350.0, 150.0}
46 };
47
48
49 FGAIAircraft::FGAIAircraft() {
50
51    // set heading and altitude locks
52    hdg_lock = false;
53    alt_lock = false;
54    _type_str = "aircraft";
55 }
56
57
58 FGAIAircraft::~FGAIAircraft() {
59 }
60
61
62 bool FGAIAircraft::init() {
63    return FGAIBase::init();
64 }
65
66 void FGAIAircraft::bind() {
67     FGAIBase::bind();
68 }
69
70 void FGAIAircraft::unbind() {
71     FGAIBase::unbind();
72 }
73
74
75 void FGAIAircraft::update(double dt) {
76
77    Run(dt);
78    Transform();
79    FGAIBase::update(dt);
80 }
81
82 void FGAIAircraft::SetPerformance(const PERF_STRUCT *ps) {
83    
84    performance = ps;
85
86
87
88 void FGAIAircraft::Run(double dt) {
89
90    FGAIAircraft::dt = dt;
91         
92    double turn_radius_ft;
93    double turn_circum_ft;
94    double speed_north_deg_sec;
95    double speed_east_deg_sec;
96    double ft_per_deg_lon;
97    double ft_per_deg_lat;
98    double dist_covered_ft;
99    double alpha;
100
101    // get size of a degree at this latitude
102    ft_per_deg_lat = 366468.96 - 3717.12 * cos(pos.lat()/SG_RADIANS_TO_DEGREES);
103    ft_per_deg_lon = 365228.16 * cos(pos.lat() / SG_RADIANS_TO_DEGREES);
104
105    // adjust speed
106    double speed_diff = tgt_speed - speed;
107    if (fabs(speed_diff) > 0.2) {
108      if (speed_diff > 0.0) speed += performance->accel * dt;
109      if (speed_diff < 0.0) speed -= performance->decel * dt;
110    } 
111    
112    // convert speed to degrees per second
113    speed_north_deg_sec = cos( hdg / SG_RADIANS_TO_DEGREES )
114                           * speed * 1.686 / ft_per_deg_lat;
115    speed_east_deg_sec  = sin( hdg / SG_RADIANS_TO_DEGREES )
116                           * speed * 1.686 / ft_per_deg_lon;
117
118    // set new position
119    pos.setlat( pos.lat() + speed_north_deg_sec * dt);
120    pos.setlon( pos.lon() + speed_east_deg_sec * dt); 
121
122    // adjust heading based on current bank angle
123    if (roll != 0.0) {
124      turn_radius_ft = 0.088362 * speed * speed
125                        / tan( fabs(roll) / SG_RADIANS_TO_DEGREES );
126      turn_circum_ft = SGD_2PI * turn_radius_ft;
127      dist_covered_ft = speed * 1.686 * dt; 
128      alpha = dist_covered_ft / turn_circum_ft * 360.0;
129      hdg += alpha * sign( roll );
130      if ( hdg > 360.0 ) hdg -= 360.0;
131      if ( hdg < 0.0) hdg += 360.0;
132    }
133
134    // adjust target bank angle if heading lock engaged
135    if (hdg_lock) {
136      double bank_sense = 0.0;
137      double diff = fabs(hdg - tgt_heading);
138      if (diff > 180) diff = fabs(diff - 360);
139      double sum = hdg + diff;
140      if (sum > 360.0) sum -= 360.0;
141      if (fabs(sum - tgt_heading) < 1.0) { 
142        bank_sense = 1.0;
143      } else {
144        bank_sense = -1.0;
145      } 
146      if (diff < 30) tgt_roll = diff * bank_sense; 
147    }
148
149    // adjust bank angle
150    double bank_diff = tgt_roll - roll;
151    if (fabs(bank_diff) > 0.2) {
152      if (bank_diff > 0.0) roll += 5.0 * dt;
153      if (bank_diff < 0.0) roll -= 5.0 * dt;
154    }
155
156    // adjust altitude (meters) based on current vertical speed (fpm)
157    altitude += vs * 0.0166667 * dt * SG_FEET_TO_METER;  
158
159    // find target vertical speed if altitude lock engaged
160    if (alt_lock) {
161      double altitude_ft = altitude * SG_METER_TO_FEET;
162      if (altitude_ft < tgt_altitude) {
163        tgt_vs = tgt_altitude - altitude_ft;
164        if (tgt_vs > performance->climb_rate)
165          tgt_vs = performance->climb_rate;
166      } else {
167        tgt_vs = tgt_altitude - altitude_ft;
168        if (tgt_vs  < (-performance->descent_rate))
169          tgt_vs = -performance->descent_rate;
170      }
171    }
172
173    // adjust vertical speed
174    double vs_diff = tgt_vs - vs;
175    if (fabs(vs_diff) > 1.0) {
176      if (vs_diff > 0.0) {
177        vs += 400.0 * dt;
178        if (vs > tgt_vs) vs = tgt_vs;
179      } else {
180        vs -= 300.0 * dt;
181        if (vs < tgt_vs) vs = tgt_vs;
182      }
183    }   
184    
185    // match pitch angle to vertical speed
186    pitch = vs * 0.005;
187
188 }
189
190
191 void FGAIAircraft::AccelTo(double speed) {
192    tgt_speed = speed;
193 }
194
195
196 void FGAIAircraft::PitchTo(double angle) {
197    tgt_pitch = angle;
198    alt_lock = false;
199 }
200
201
202 void FGAIAircraft::RollTo(double angle) {
203    tgt_roll = angle;
204    hdg_lock = false; 
205 }
206
207
208 void FGAIAircraft::YawTo(double angle) {
209    tgt_yaw = angle;
210 }
211
212
213 void FGAIAircraft::ClimbTo(double altitude) {
214    tgt_altitude = altitude;
215    alt_lock = true;
216 }
217
218
219 void FGAIAircraft::TurnTo(double heading) {
220    tgt_heading = heading;
221    hdg_lock = true;
222 }
223
224
225 double FGAIAircraft::sign(double x) {
226
227   if ( x < 0.0 ) { return -1.0; }
228   else { return 1.0; }
229 }