1 // FGAIAircraft - FGAIBase-derived class creates an AI airplane
3 // Written by David Culp, started October 2003.
5 // Copyright (C) 2003 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 "AIAircraft.hxx"
37 // accel, decel, climb_rate, descent_rate, takeoff_speed, climb_speed,
38 // cruise_speed, descent_speed, land_speed
40 const FGAIAircraft::PERF_STRUCT FGAIAircraft::settings[] = {
42 {2.0, 2.0, 450.0, 1000.0, 70.0, 80.0, 100.0, 80.0, 60.0},
44 {4.0, 2.0, 3000.0, 1500.0, 110.0, 180.0, 250.0, 200.0, 100.0},
46 {5.0, 2.0, 3000.0, 1500.0, 140.0, 300.0, 430.0, 300.0, 130.0},
48 {7.0, 3.0, 4000.0, 2000.0, 150.0, 350.0, 500.0, 350.0, 150.0}
52 FGAIAircraft *FGAIAircraft::_self = NULL;
54 FGAIAircraft::FGAIAircraft() {
57 // set heading and altitude locks
60 _type_str = "aircraft";
64 FGAIAircraft::~FGAIAircraft() {
69 bool FGAIAircraft::init() {
70 return FGAIBase::init();
73 void FGAIAircraft::bind() {
76 props->tie("controls/gear/gear-down",
77 SGRawValueFunctions<bool>(FGAIAircraft::_getGearDown));
80 props->getNode("controls/lighting/landing-lights", true)
81 ->alias("controls/gear/gear-down");
85 void FGAIAircraft::unbind() {
88 props->untie("controls/gear/gear-down");
89 // props->getNode("controls/lighting/landing-lights")->unalias();
93 void FGAIAircraft::update(double dt) {
100 void FGAIAircraft::SetPerformance(const PERF_STRUCT *ps) {
106 void FGAIAircraft::Run(double dt) {
108 FGAIAircraft::dt = dt;
110 double turn_radius_ft;
111 double turn_circum_ft;
112 double speed_north_deg_sec;
113 double speed_east_deg_sec;
114 double ft_per_deg_lon;
115 double ft_per_deg_lat;
116 double dist_covered_ft;
119 // get size of a degree at this latitude
120 ft_per_deg_lat = 366468.96 - 3717.12 * cos(pos.lat()/SG_RADIANS_TO_DEGREES);
121 ft_per_deg_lon = 365228.16 * cos(pos.lat() / SG_RADIANS_TO_DEGREES);
124 double speed_diff = tgt_speed - speed;
125 if (fabs(speed_diff) > 0.2) {
126 if (speed_diff > 0.0) speed += performance->accel * dt;
127 if (speed_diff < 0.0) speed -= performance->decel * dt;
130 // convert speed to degrees per second
131 speed_north_deg_sec = cos( hdg / SG_RADIANS_TO_DEGREES )
132 * speed * 1.686 / ft_per_deg_lat;
133 speed_east_deg_sec = sin( hdg / SG_RADIANS_TO_DEGREES )
134 * speed * 1.686 / ft_per_deg_lon;
137 pos.setlat( pos.lat() + speed_north_deg_sec * dt);
138 pos.setlon( pos.lon() + speed_east_deg_sec * dt);
140 // adjust heading based on current bank angle
142 turn_radius_ft = 0.088362 * speed * speed
143 / tan( fabs(roll) / SG_RADIANS_TO_DEGREES );
144 turn_circum_ft = SGD_2PI * turn_radius_ft;
145 dist_covered_ft = speed * 1.686 * dt;
146 alpha = dist_covered_ft / turn_circum_ft * 360.0;
147 hdg += alpha * sign( roll );
148 if ( hdg > 360.0 ) hdg -= 360.0;
149 if ( hdg < 0.0) hdg += 360.0;
152 // adjust target bank angle if heading lock engaged
154 double bank_sense = 0.0;
155 double diff = fabs(hdg - tgt_heading);
156 if (diff > 180) diff = fabs(diff - 360);
157 double sum = hdg + diff;
158 if (sum > 360.0) sum -= 360.0;
159 if (fabs(sum - tgt_heading) < 1.0) {
164 if (diff < 30) tgt_roll = diff * bank_sense;
168 double bank_diff = tgt_roll - roll;
169 if (fabs(bank_diff) > 0.2) {
170 if (bank_diff > 0.0) roll += 5.0 * dt;
171 if (bank_diff < 0.0) roll -= 5.0 * dt;
174 // adjust altitude (meters) based on current vertical speed (fpm)
175 altitude += vs * 0.0166667 * dt * SG_FEET_TO_METER;
177 // find target vertical speed if altitude lock engaged
179 double altitude_ft = altitude * SG_METER_TO_FEET;
180 if (altitude_ft < tgt_altitude) {
181 tgt_vs = tgt_altitude - altitude_ft;
182 if (tgt_vs > performance->climb_rate)
183 tgt_vs = performance->climb_rate;
185 tgt_vs = tgt_altitude - altitude_ft;
186 if (tgt_vs < (-performance->descent_rate))
187 tgt_vs = -performance->descent_rate;
191 // adjust vertical speed
192 double vs_diff = tgt_vs - vs;
193 if (fabs(vs_diff) > 1.0) {
196 if (vs > tgt_vs) vs = tgt_vs;
199 if (vs < tgt_vs) vs = tgt_vs;
203 // match pitch angle to vertical speed
209 void FGAIAircraft::AccelTo(double speed) {
214 void FGAIAircraft::PitchTo(double angle) {
220 void FGAIAircraft::RollTo(double angle) {
226 void FGAIAircraft::YawTo(double angle) {
231 void FGAIAircraft::ClimbTo(double altitude) {
232 tgt_altitude = altitude;
237 void FGAIAircraft::TurnTo(double heading) {
238 tgt_heading = heading;
243 double FGAIAircraft::sign(double x) {
245 if ( x < 0.0 ) { return -1.0; }