]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIShip.cxx
d9fc379fe3092e24448ce362578cfe7c5c50c59f
[flightgear.git] / src / AIModel / AIShip.cxx
1 // FGAIShip - FGAIBase-derived class creates an AI ship
2 //
3 // Written by David Culp, started October 2003.
4 // - davidculp2@comcast.net
5 //
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.
10 //
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.
15 //
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.
19
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23
24 #include <simgear/math/point3d.hxx>
25 #include <math.h>
26
27 #include "AIShip.hxx"
28
29
30 FGAIShip::FGAIShip(FGAIManager* mgr) {
31    manager = mgr;
32    hdg_lock = false;
33    rudder = 0.0;
34    _type_str = "ship";
35 }
36
37 FGAIShip::~FGAIShip() {
38 }
39
40
41 bool FGAIShip::init() {
42    return FGAIBase::init();
43 }
44
45 void FGAIShip::bind() {
46     FGAIBase::bind();
47
48     props->tie("surface-positions/rudder-pos-norm",
49                 SGRawValuePointer<double>(&rudder));
50 }
51
52 void FGAIShip::unbind() {
53     FGAIBase::unbind();
54 }
55
56 void FGAIShip::update(double dt) {
57
58    Run(dt);
59    Transform();
60    FGAIBase::update(dt);
61 }
62
63
64
65 void FGAIShip::Run(double dt) {
66
67    double turn_radius_ft;
68    double turn_circum_ft;
69    double speed_north_deg_sec;
70    double speed_east_deg_sec;
71    double ft_per_deg_lon;
72    double ft_per_deg_lat;
73    double dist_covered_ft;
74    double alpha;
75
76    // get size of a degree at this latitude
77    ft_per_deg_lat = 366468.96 - 3717.12 * cos(pos.lat()/SG_RADIANS_TO_DEGREES);
78    ft_per_deg_lon = 365228.16 * cos(pos.lat() / SG_RADIANS_TO_DEGREES);
79
80    // adjust speed
81    double speed_diff = tgt_speed - speed;
82    if (fabs(speed_diff) > 0.1) {
83      if (speed_diff > 0.0) speed += 0.1 * dt;
84      if (speed_diff < 0.0) speed -= 0.1 * dt;
85    } 
86    
87    // convert speed to degrees per second
88    speed_north_deg_sec = cos( hdg / SG_RADIANS_TO_DEGREES )
89                           * speed * 1.686 / ft_per_deg_lat;
90    speed_east_deg_sec  = sin( hdg / SG_RADIANS_TO_DEGREES )
91                           * speed * 1.686 / ft_per_deg_lon;
92
93    // set new position
94    pos.setlat( pos.lat() + speed_north_deg_sec * dt);
95    pos.setlon( pos.lon() + speed_east_deg_sec * dt); 
96
97    // adjust heading based on current rudder angle
98    if (rudder != 0.0) {
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;
104      hdg += alpha * sign( rudder );
105      if ( hdg > 360.0 ) hdg -= 360.0;
106      if ( hdg < 0.0) hdg += 360.0;
107    }
108
109    // adjust target rudder angle if heading lock engaged
110    if (hdg_lock) {
111      double rudder_sense = 0.0;
112      double diff = fabs(hdg - tgt_heading);
113      if (diff > 180) diff = fabs(diff - 360);
114      double sum = hdg + diff;
115      if (sum > 360.0) sum -= 360.0;
116      if (fabs(sum - tgt_heading) < 1.0) { 
117        rudder_sense = 1.0;
118      } else {
119        rudder_sense = -1.0;
120      } 
121      if (diff < 30) tgt_roll = diff * rudder_sense; 
122    }
123
124    // adjust rudder angle
125    double rudder_diff = tgt_roll - rudder;
126    if (fabs(rudder_diff) > 0.1) {
127      if (rudder_diff > 0.0) rudder += 5.0 * dt;
128      if (rudder_diff < 0.0) rudder -= 5.0 * dt;
129    }
130
131 }
132
133
134 void FGAIShip::AccelTo(double speed) {
135    tgt_speed = speed;
136 }
137
138
139 void FGAIShip::PitchTo(double angle) {
140    tgt_pitch = angle;
141 }
142
143
144 void FGAIShip::RollTo(double angle) {
145    tgt_roll = angle;
146 }
147
148
149 void FGAIShip::YawTo(double angle) {
150 }
151
152
153 void FGAIShip::ClimbTo(double altitude) {
154 }
155
156
157 void FGAIShip::TurnTo(double heading) {
158    tgt_heading = heading;
159    hdg_lock = true;
160 }
161
162
163 double FGAIShip::sign(double x) {
164
165   if ( x < 0.0 ) { return -1.0; }
166   else { return 1.0; }
167 }