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 const FGAIAircraft::PERF_STRUCT FGAIAircraft::settings[] = {
39 {2.0, 2.0, 450.0, 1000.0, 70.0, 80.0, 100.0, 80.0, 60.0},
41 {4.0, 2.0, 3000.0, 1500.0, 110.0, 180.0, 250.0, 200.0, 100.0},
43 {5.0, 2.0, 3000.0, 1500.0, 140.0, 300.0, 430.0, 300.0, 130.0},
45 {7.0, 3.0, 4000.0, 2000.0, 150.0, 350.0, 500.0, 350.0, 150.0}
49 FGAIAircraft::FGAIAircraft() {
51 // set heading and altitude locks
54 _type_str = "aircraft";
58 FGAIAircraft::~FGAIAircraft() {
62 bool FGAIAircraft::init() {
63 return FGAIBase::init();
66 void FGAIAircraft::bind() {
70 void FGAIAircraft::unbind() {
75 void FGAIAircraft::update(double dt) {
82 void FGAIAircraft::SetPerformance(const PERF_STRUCT *ps) {
88 void FGAIAircraft::Run(double dt) {
90 FGAIAircraft::dt = dt;
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;
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);
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;
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;
119 pos.setlat( pos.lat() + speed_north_deg_sec * dt);
120 pos.setlon( pos.lon() + speed_east_deg_sec * dt);
122 // adjust heading based on current bank angle
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;
134 // adjust target bank angle if heading lock engaged
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) {
146 if (diff < 30) tgt_roll = diff * bank_sense;
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;
156 // adjust altitude (meters) based on current vertical speed (fpm)
157 altitude += vs * 0.0166667 * dt * SG_FEET_TO_METER;
159 // find target vertical speed if altitude lock engaged
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;
167 tgt_vs = tgt_altitude - altitude_ft;
168 if (tgt_vs < (-performance->descent_rate))
169 tgt_vs = -performance->descent_rate;
173 // adjust vertical speed
174 double vs_diff = tgt_vs - vs;
175 if (fabs(vs_diff) > 1.0) {
178 if (vs > tgt_vs) vs = tgt_vs;
181 if (vs < tgt_vs) vs = tgt_vs;
185 // match pitch angle to vertical speed
191 void FGAIAircraft::AccelTo(double speed) {
196 void FGAIAircraft::PitchTo(double angle) {
202 void FGAIAircraft::RollTo(double angle) {
208 void FGAIAircraft::YawTo(double angle) {
213 void FGAIAircraft::ClimbTo(double altitude) {
214 tgt_altitude = altitude;
219 void FGAIAircraft::TurnTo(double heading) {
220 tgt_heading = heading;
225 double FGAIAircraft::sign(double x) {
227 if ( x < 0.0 ) { return -1.0; }