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) {
39 FGAIShip::~FGAIShip() {
43 bool FGAIShip::init() {
44 return FGAIBase::init();
47 void FGAIShip::bind() {
50 props->tie("surface-positions/rudder-pos-deg",
51 SGRawValuePointer<double>(&rudder));
54 void FGAIShip::unbind() {
56 props->untie("surface-positions/rudder-pos-deg");
59 void FGAIShip::update(double dt) {
68 void FGAIShip::Run(double dt) {
70 if (fp) ProcessFlightPlan(dt);
72 double sp_turn_radius_ft;
73 double rd_turn_radius_ft;
74 double speed_north_deg_sec;
75 double speed_east_deg_sec;
76 double dist_covered_ft;
80 double speed_diff = tgt_speed - speed;
81 if (fabs(speed_diff) > 0.1) {
82 if (speed_diff > 0.0) speed += 0.1 * dt;
83 if (speed_diff < 0.0) speed -= 0.1 * dt;
86 // convert speed to degrees per second
87 speed_north_deg_sec = cos( hdg / SGD_RADIANS_TO_DEGREES )
88 * speed * 1.686 / ft_per_deg_lat;
89 speed_east_deg_sec = sin( hdg / SGD_RADIANS_TO_DEGREES )
90 * speed * 1.686 / ft_per_deg_lon;
93 pos.setlat( pos.lat() + speed_north_deg_sec * dt);
94 pos.setlon( pos.lon() + speed_east_deg_sec * dt);
97 // adjust heading based on current rudder angle
99 /* turn_radius_ft = 0.088362 * speed * speed
100 / tan( fabs(rudder) / SG_RADIANS_TO_DEGREES );
101 turn_circum_ft = SGD_2PI * turn_radius_ft;
102 dist_covered_ft = speed * 1.686 * dt;
103 alpha = dist_covered_ft / turn_circum_ft * 360.0;*/
105 if (turn_radius_ft <= 0) turn_radius_ft = 0; // don't allow nonsense values
107 // cout << "speed " << speed << " turn radius " << turn_radius_ft << endl;
109 // adjust turn radius for speed. The equation is very approximate.
110 sp_turn_radius_ft = 10 * pow ((speed - 15),2) + turn_radius_ft;
111 // cout << "speed " << speed << " speed turn radius " << sp_turn_radius_ft << endl;
113 // adjust turn radius for rudder angle. The equation is even more approximate.
114 rd_turn_radius_ft = -130 * (rudder - 15) + sp_turn_radius_ft;
115 // cout << "rudder " << rudder << " rudder turn radius " << rd_turn_radius_ft << endl;
117 // calculate the angle, alpha, subtended by the arc traversed in time dt
118 alpha = ((speed * 1.686 * dt)/rd_turn_radius_ft) * SG_RADIANS_TO_DEGREES;
120 // make sure that alpha is applied in the right direction
122 hdg += alpha * sign( rudder );
124 if ( hdg > 360.0 ) hdg -= 360.0;
125 if ( hdg < 0.0) hdg += 360.0;
127 //adjust roll for rudder angle and speed
128 roll = - ( speed / 2 - rudder / 6 );
130 // cout << " hdg " << hdg << "roll "<< roll << endl;
133 // adjust target rudder angle if heading lock engaged
135 double rudder_sense = 0.0;
136 double diff = fabs(hdg - tgt_heading);
137 if (diff > 180) diff = fabs(diff - 360);
138 double sum = hdg + diff;
139 if (sum > 360.0) sum -= 360.0;
140 if (fabs(sum - tgt_heading) < 1.0) {
145 if (diff < 30) tgt_roll = diff * rudder_sense;
148 // adjust rudder angle
149 double rudder_diff = tgt_roll - rudder;
150 if (fabs(rudder_diff) > 0.1) {
151 if (rudder_diff > 0.0) rudder += 5.0 * dt;
152 if (rudder_diff < 0.0) rudder -= 5.0 * dt;
158 void FGAIShip::AccelTo(double speed) {
163 void FGAIShip::PitchTo(double angle) {
168 void FGAIShip::RollTo(double angle) {
173 void FGAIShip::YawTo(double angle) {
177 void FGAIShip::ClimbTo(double altitude) {
181 void FGAIShip::TurnTo(double heading) {
182 tgt_heading = heading;
187 double FGAIShip::sign(double x) {
189 if ( x < 0.0 ) { return -1.0; }
193 void FGAIShip::setFlightPlan(FGAIFlightPlan* f) {
197 void FGAIShip::ProcessFlightPlan(double dt) {
198 // not implemented yet