]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIShip.cxx
Make it optional whether a dialog can be dragged or not.
[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-deg",
51                 SGRawValuePointer<double>(&rudder));
52
53     props->setStringValue("name", name.c_str());
54 }
55
56 void FGAIShip::unbind() {
57     FGAIBase::unbind();
58     props->untie("surface-positions/rudder-pos-deg");
59 }
60
61 void FGAIShip::update(double dt) {
62
63    FGAIBase::update(dt);
64    Run(dt);
65    Transform();
66 }
67
68
69
70 void FGAIShip::Run(double dt) {
71
72    if (fp) ProcessFlightPlan(dt);
73
74    double sp_turn_radius_ft;
75    double rd_turn_radius_ft;
76    double speed_north_deg_sec;
77    double speed_east_deg_sec;
78    double dist_covered_ft;
79    double alpha;
80
81    // adjust speed
82    double speed_diff = tgt_speed - speed;
83    if (fabs(speed_diff) > 0.1) {
84      if (speed_diff > 0.0) speed += 0.1 * dt;
85      if (speed_diff < 0.0) speed -= 0.1 * dt;
86    } 
87    
88    // convert speed to degrees per second
89    speed_north_deg_sec = cos( hdg / SGD_RADIANS_TO_DEGREES )
90                           * speed * 1.686 / ft_per_deg_lat;
91    speed_east_deg_sec  = sin( hdg / SGD_RADIANS_TO_DEGREES )
92                           * speed * 1.686 / ft_per_deg_lon;
93
94    // set new position
95    pos.setlat( pos.lat() + speed_north_deg_sec * dt);
96    pos.setlon( pos.lon() + speed_east_deg_sec * dt); 
97
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      
107      if (turn_radius_ft <= 0) turn_radius_ft = 0; // don't allow nonsense values
108      
109 //     cout << "speed " << speed << " turn radius " << turn_radius_ft << endl;
110
111 // adjust turn radius for speed. The equation is very approximate.
112      sp_turn_radius_ft = 10 * pow ((speed - 15),2) + turn_radius_ft;
113 //     cout << "speed " << speed << " speed turn radius " << sp_turn_radius_ft << endl; 
114
115 // adjust turn radius for rudder angle. The equation is even more approximate.     
116      rd_turn_radius_ft = -130 * (rudder - 15) + sp_turn_radius_ft;
117 //     cout << "rudder " << rudder << " rudder turn radius " << rd_turn_radius_ft << endl;
118           
119 // calculate the angle, alpha, subtended by the arc traversed in time dt        
120      alpha = ((speed * 1.686 * dt)/rd_turn_radius_ft) * SG_RADIANS_TO_DEGREES;
121
122 // make sure that alpha is applied in the right direction   
123    
124      hdg += alpha * sign( rudder );
125
126      if ( hdg > 360.0 ) hdg -= 360.0;
127      if ( hdg < 0.0) hdg += 360.0;
128
129 //adjust roll for rudder angle and speed     
130      roll = - (  speed / 2 - rudder / 6 );
131      
132 //    cout << " hdg " << hdg  << "roll "<< roll << endl;
133    }
134
135    // adjust target rudder angle if heading lock engaged
136    if (hdg_lock) {
137      double rudder_sense = 0.0;
138      double diff = fabs(hdg - tgt_heading);
139      if (diff > 180) diff = fabs(diff - 360);
140      double sum = hdg + diff;
141      if (sum > 360.0) sum -= 360.0;
142      if (fabs(sum - tgt_heading) < 1.0) { 
143        rudder_sense = 1.0;
144      } else {
145        rudder_sense = -1.0;
146      } 
147      if (diff < 30) tgt_roll = diff * rudder_sense; 
148    }
149
150    // adjust rudder angle
151    double rudder_diff = tgt_roll - rudder;
152    if (fabs(rudder_diff) > 0.1) {
153      if (rudder_diff > 0.0) rudder += 5.0 * dt;
154      if (rudder_diff < 0.0) rudder -= 5.0 * dt;
155    }
156
157 }
158
159
160 void FGAIShip::AccelTo(double speed) {
161    tgt_speed = speed;
162 }
163
164
165 void FGAIShip::PitchTo(double angle) {
166    tgt_pitch = angle;
167 }
168
169
170 void FGAIShip::RollTo(double angle) {
171    tgt_roll = angle;
172 }
173
174
175 void FGAIShip::YawTo(double angle) {
176 }
177
178
179 void FGAIShip::ClimbTo(double altitude) {
180 }
181
182
183 void FGAIShip::TurnTo(double heading) {
184    tgt_heading = heading;
185    hdg_lock = true;
186 }
187
188
189 double FGAIShip::sign(double x) {
190
191   if ( x < 0.0 ) { return -1.0; }
192   else { return 1.0; }
193 }
194
195 void FGAIShip::setFlightPlan(FGAIFlightPlan* f) {
196   fp = f;
197 }
198
199 void FGAIShip::setName(const string& n) {
200   name = n;
201 }
202
203 void FGAIShip::ProcessFlightPlan(double dt) {
204   // not implemented yet
205 }
206