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