1 // FGAIShip - FGAIBase-derived class creates an AI ship
3 // Written by David Culp, started October 2003.
4 // - davidculp2@comcast.net
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License as
8 // published by the Free Software Foundation; either version 2 of the
9 // License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <simgear/math/point3d.hxx>
30 FGAIShip::FGAIShip(FGAIManager* mgr) {
37 FGAIShip::~FGAIShip() {
41 bool FGAIShip::init() {
47 rudder_constant = 0.5;
48 roll_constant = 0.001;
49 speed_constant = 0.05;
52 return FGAIBase::init();
55 void FGAIShip::bind() {
58 props->tie("surface-positions/rudder-pos-deg",
59 SGRawValuePointer<float>(&rudder));
60 props->tie("controls/heading-lock",
61 SGRawValuePointer<bool>(&hdg_lock));
62 props->tie("controls/tgt-speed-kts",
63 SGRawValuePointer<double>(&tgt_speed));
64 props->tie("controls/tgt-heading-degs",
65 SGRawValuePointer<double>(&tgt_heading));
66 props->tie("controls/constants/rudder",
67 SGRawValuePointer<double>(&rudder_constant));
68 props->tie("controls/constants/roll",
69 SGRawValuePointer<double>(&roll_constant));
70 props->tie("controls/constants/rudder",
71 SGRawValuePointer<double>(&rudder_constant));
72 props->tie("controls/constants/speed",
73 SGRawValuePointer<double>(&speed_constant));
75 props->setStringValue("name", name.c_str());
78 void FGAIShip::unbind() {
80 props->untie("surface-positions/rudder-pos-deg");
81 props->untie("controls/heading-lock");
82 props->untie("controls/tgt-speed-kts");
83 props->untie("controls/tgt-heading-degs");
84 props->untie("controls/constants/roll");
85 props->untie("controls/constants/rudder");
86 props->untie("controls/constants/speed");
90 void FGAIShip::update(double dt) {
99 void FGAIShip::Run(double dt) {
101 if (fp) ProcessFlightPlan(dt);
103 double sp_turn_radius_ft;
104 double rd_turn_radius_ft;
105 double speed_north_deg_sec;
106 double speed_east_deg_sec;
107 double dist_covered_ft;
113 double speed_diff = tgt_speed - speed;
114 if (fabs(speed_diff) > 0.1) {
115 if (speed_diff > 0.0) speed += speed_constant * dt;
116 if (speed_diff < 0.0) speed -= speed_constant * dt;
119 // convert speed to degrees per second
120 speed_north_deg_sec = cos( hdg / SGD_RADIANS_TO_DEGREES )
121 * speed * 1.686 / ft_per_deg_lat;
122 speed_east_deg_sec = sin( hdg / SGD_RADIANS_TO_DEGREES )
123 * speed * 1.686 / ft_per_deg_lon;
126 pos.setlat( pos.lat() + speed_north_deg_sec * dt);
127 pos.setlon( pos.lon() + speed_east_deg_sec * dt);
130 // adjust heading based on current rudder angle
131 if (rudder <= -0.25 || rudder >= 0.25) {
132 /* turn_radius_ft = 0.088362 * speed * speed
133 / tan( fabs(rudder) / SG_RADIANS_TO_DEGREES );
134 turn_circum_ft = SGD_2PI * turn_radius_ft;
135 dist_covered_ft = speed * 1.686 * dt;
136 alpha = dist_covered_ft / turn_circum_ft * 360.0;*/
138 if (turn_radius_ft <= 0) turn_radius_ft = 0; // don't allow nonsense values
139 if (rudder > 45) rudder = 45;
140 if (rudder < -45) rudder = -45;
142 // adjust turn radius for speed. The equation is very approximate.
143 sp_turn_radius_ft = 10 * pow ((speed - 15),2) + turn_radius_ft;
144 // cout << " speed turn radius " << sp_turn_radius_ft ;
146 // adjust turn radius for rudder angle. The equation is even more approximate.
151 rd_turn_radius_ft = (a * exp(b * fabs(rudder)) + c) * sp_turn_radius_ft;
153 // cout <<" rudder turn radius " << rd_turn_radius_ft << endl;
155 // calculate the angle, alpha, subtended by the arc traversed in time dt
156 alpha = ((speed * 1.686 * dt)/rd_turn_radius_ft) * SG_RADIANS_TO_DEGREES;
159 // make sure that alpha is applied in the right direction
160 hdg += alpha * sign( rudder );
161 if ( hdg > 360.0 ) hdg -= 360.0;
162 if ( hdg < 0.0) hdg += 360.0;
164 //adjust roll for rudder angle and speed. Another bit of voodoo
165 raw_roll = -0.0166667 * speed * rudder;
171 // cout << " roll "<< roll << endl;
175 roll = (raw_roll * roll_constant) + (roll * (1 - roll_constant));
177 /*cout << " rudder: " << rudder << " raw roll: "<< raw_roll<<" roll: " << roll ;
178 cout << " hdg: " << hdg << endl ;*/
180 // adjust target rudder angle if heading lock engaged
182 double rudder_sense = 0.0;
183 double diff = fabs(hdg - tgt_heading);
184 if (diff > 180) diff = fabs(diff - 360);
185 double sum = hdg + diff;
186 if (sum > 360.0) sum -= 360.0;
187 if (fabs(sum - tgt_heading) < 1.0) {
193 tgt_rudder = diff * rudder_sense;
197 tgt_rudder = 45 * rudder_sense;
201 // adjust rudder angle
202 double rudder_diff = tgt_rudder - rudder;
203 // set the rudder limit by speed
205 rudder_limit = (-0.825 * speed) + 35;
210 if (fabs(rudder_diff) > 0.1) {
211 if (rudder_diff > 0.0){
212 rudder += rudder_constant * dt;
213 if (rudder > rudder_limit) rudder = rudder_limit;// apply the rudder limit
214 } else if (rudder_diff < 0.0){
215 rudder -= rudder_constant * dt;
216 if (rudder < -rudder_limit) rudder = -rudder_limit;
225 void FGAIShip::AccelTo(double speed) {
229 void FGAIShip::PitchTo(double angle) {
234 void FGAIShip::RollTo(double angle) {
239 void FGAIShip::YawTo(double angle) {
243 void FGAIShip::ClimbTo(double altitude) {
247 void FGAIShip::TurnTo(double heading) {
248 tgt_heading = heading;
253 double FGAIShip::sign(double x) {
255 if ( x < 0.0 ) { return -1.0; }
259 void FGAIShip::setFlightPlan(FGAIFlightPlan* f) {
263 void FGAIShip::setName(const string& n) {
267 void FGAIShip::ProcessFlightPlan(double dt) {
268 // not implemented yet
271 void FGAIShip::setRudder(float r) {
275 void FGAIShip::setRoll(double rl) {