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-norm",
51 SGRawValuePointer<double>(&rudder));
54 void FGAIShip::unbind() {
58 void FGAIShip::update(double dt) {
67 void FGAIShip::Run(double dt) {
69 if (fp) ProcessFlightPlan(dt);
71 double turn_radius_ft;
72 double turn_circum_ft;
73 double speed_north_deg_sec;
74 double speed_east_deg_sec;
75 double ft_per_deg_lon;
76 double ft_per_deg_lat;
77 double dist_covered_ft;
80 // get size of a degree at this latitude
81 ft_per_deg_lat = 366468.96 - 3717.12 * cos(pos.lat()/SG_RADIANS_TO_DEGREES);
82 ft_per_deg_lon = 365228.16 * cos(pos.lat() / SG_RADIANS_TO_DEGREES);
85 double speed_diff = tgt_speed - speed;
86 if (fabs(speed_diff) > 0.1) {
87 if (speed_diff > 0.0) speed += 0.1 * dt;
88 if (speed_diff < 0.0) speed -= 0.1 * dt;
91 // convert speed to degrees per second
92 speed_north_deg_sec = cos( hdg / SG_RADIANS_TO_DEGREES )
93 * speed * 1.686 / ft_per_deg_lat;
94 speed_east_deg_sec = sin( hdg / SG_RADIANS_TO_DEGREES )
95 * speed * 1.686 / ft_per_deg_lon;
98 pos.setlat( pos.lat() + speed_north_deg_sec * dt);
99 pos.setlon( pos.lon() + speed_east_deg_sec * dt);
101 // adjust heading based on current rudder angle
103 turn_radius_ft = 0.088362 * speed * speed
104 / tan( fabs(rudder) / SG_RADIANS_TO_DEGREES );
105 turn_circum_ft = SGD_2PI * turn_radius_ft;
106 dist_covered_ft = speed * 1.686 * dt;
107 alpha = dist_covered_ft / turn_circum_ft * 360.0;
108 hdg += alpha * sign( rudder );
109 if ( hdg > 360.0 ) hdg -= 360.0;
110 if ( hdg < 0.0) hdg += 360.0;
113 // adjust target rudder angle if heading lock engaged
115 double rudder_sense = 0.0;
116 double diff = fabs(hdg - tgt_heading);
117 if (diff > 180) diff = fabs(diff - 360);
118 double sum = hdg + diff;
119 if (sum > 360.0) sum -= 360.0;
120 if (fabs(sum - tgt_heading) < 1.0) {
125 if (diff < 30) tgt_roll = diff * rudder_sense;
128 // adjust rudder angle
129 double rudder_diff = tgt_roll - rudder;
130 if (fabs(rudder_diff) > 0.1) {
131 if (rudder_diff > 0.0) rudder += 5.0 * dt;
132 if (rudder_diff < 0.0) rudder -= 5.0 * dt;
138 void FGAIShip::AccelTo(double speed) {
143 void FGAIShip::PitchTo(double angle) {
148 void FGAIShip::RollTo(double angle) {
153 void FGAIShip::YawTo(double angle) {
157 void FGAIShip::ClimbTo(double altitude) {
161 void FGAIShip::TurnTo(double heading) {
162 tgt_heading = heading;
167 double FGAIShip::sign(double x) {
169 if ( x < 0.0 ) { return -1.0; }
173 void FGAIShip::setFlightPlan(FGAIFlightPlan* f) {
177 void FGAIShip::ProcessFlightPlan(double dt) {
178 // not implemented yet