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