1 // FGAICarrier - FGAIShip-derived class creates an AI aircraft carrier
3 // Written by David Culp, started October 2004.
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.
27 #include <simgear/math/point3d.hxx>
28 #include <simgear/math/sg_geodesy.hxx>
30 #include <Main/util.hxx>
31 #include <Main/viewer.hxx>
33 #include "AICarrier.hxx"
36 FGAICarrier::FGAICarrier(FGAIManager* mgr) : FGAIShip(mgr) {
39 FGAICarrier::~FGAICarrier() {
42 void FGAICarrier::setSolidObjects(const list<string>& so) {
46 void FGAICarrier::setWireObjects(const list<string>& wo) {
50 void FGAICarrier::setCatapultObjects(const list<string>& co) {
51 catapult_objects = co;
54 void FGAICarrier::getVelocityWrtEarth(sgVec3 v) {
55 sgCopyVec3(v, vel_wrt_earth );
58 void FGAICarrier::update(double dt) {
62 // Update the velocity information stored in those nodes.
63 double v_north = 0.51444444*speed*cos(hdg * SGD_DEGREES_TO_RADIANS);
64 double v_east = 0.51444444*speed*sin(hdg * SGD_DEGREES_TO_RADIANS);
66 double sin_lat = sin(pos.lat() * SGD_DEGREES_TO_RADIANS);
67 double cos_lat = cos(pos.lat() * SGD_DEGREES_TO_RADIANS);
68 double sin_lon = sin(pos.lon() * SGD_DEGREES_TO_RADIANS);
69 double cos_lon = cos(pos.lon() * SGD_DEGREES_TO_RADIANS);
70 sgSetVec3( vel_wrt_earth,
71 - cos_lon*sin_lat*v_north - sin_lon*v_east,
72 - sin_lon*sin_lat*v_north + cos_lon*v_east,
77 bool FGAICarrier::init() {
78 if (!FGAIShip::init())
81 // process the 3d model here
82 // mark some objects solid, mark the wires ...
84 // The model should be used for altitude computations.
85 // To avoid that every detail in a carrier 3D model will end into
86 // the aircraft local cache, only set the HOT traversal bit on
88 ssgEntity *sel = aip.getSceneGraph();
89 // Clear the HOT traversal flag
91 // Selectively set that flag again for wires/cats/solid objects.
92 // Attach a pointer to this carrier class to those objects.
93 mark_wires(sel, wire_objects);
94 mark_cat(sel, catapult_objects);
95 mark_solid(sel, solid_objects, false);
99 void FGAICarrier::bind() {
102 props->tie("controls/flols/source-lights",
103 SGRawValuePointer<int>(&source));
104 props->tie("controls/flols/distance-m",
105 SGRawValuePointer<double>(&dist));
106 props->setBoolValue("controls/flols/cut-lights", false);
107 props->setBoolValue("controls/flols/wave-off-lights", false);
108 props->setBoolValue("controls/flols/cond-datum-lights", true);
111 void FGAICarrier::unbind() {
113 props->untie("controls/flols/source-lights");
116 void FGAICarrier::mark_nohot(ssgEntity* e) {
117 if (e->isAKindOf(ssgTypeBranch())) {
118 ssgBranch* br = (ssgBranch*)e;
120 for ( kid = br->getKid(0); kid != NULL ; kid = br->getNextKid() )
123 br->clrTraversalMaskBits(SSGTRAV_HOT);
125 } else if (e->isAKindOf(ssgTypeLeaf())) {
127 e->clrTraversalMaskBits(SSGTRAV_HOT);
132 bool FGAICarrier::mark_wires(ssgEntity* e, const list<string>& wire_objects) {
134 if (e->isAKindOf(ssgTypeBranch())) {
136 ssgBranch* br = (ssgBranch*)e;
138 for ( kid = br->getKid(0); kid != NULL ; kid = br->getNextKid() )
139 found = mark_wires(kid, wire_objects) || found;
142 br->setTraversalMaskBits(SSGTRAV_HOT);
144 } else if (e->isAKindOf(ssgTypeLeaf())) {
145 list<string>::const_iterator it;
146 for (it = wire_objects.begin(); it != wire_objects.end(); ++it) {
147 if (e->getName() && (*it) == e->getName()) {
148 e->setTraversalMaskBits(SSGTRAV_HOT);
149 e->setUserData( FGAICarrierHardware::newWire( this ) );
150 ssgLeaf *l = (ssgLeaf*)e;
151 if ( l->getNumLines() != 1 ) {
152 SG_LOG(SG_GENERAL, SG_ALERT,
153 "AICarrier: Found wires not modelled with exactly one line!");
163 bool FGAICarrier::mark_solid(ssgEntity* e, const list<string>& solid_objects, bool mark) {
165 if (e->isAKindOf(ssgTypeBranch())) {
166 ssgBranch* br = (ssgBranch*)e;
169 list<string>::const_iterator it;
170 for (it = solid_objects.begin(); it != solid_objects.end(); ++it)
171 mark = mark || e->getName() && (*it) == e->getName();
173 for ( kid = br->getKid(0); kid != NULL ; kid = br->getNextKid() )
174 found = mark_solid(kid, solid_objects, mark) || found;
177 br->setTraversalMaskBits(SSGTRAV_HOT);
179 } else if (e->isAKindOf(ssgTypeLeaf())) {
180 list<string>::const_iterator it;
181 for (it = solid_objects.begin(); it != solid_objects.end(); ++it) {
182 if (mark || (e->getName() && (*it) == e->getName())) {
183 e->setTraversalMaskBits(SSGTRAV_HOT);
184 e->setUserData( FGAICarrierHardware::newSolid( this ) );
192 bool FGAICarrier::mark_cat(ssgEntity* e, const list<string>& cat_objects) {
194 if (e->isAKindOf(ssgTypeBranch())) {
195 ssgBranch* br = (ssgBranch*)e;
197 for ( kid = br->getKid(0); kid != NULL ; kid = br->getNextKid() )
198 found = mark_cat(kid, cat_objects) || found;
201 br->setTraversalMaskBits(SSGTRAV_HOT);
203 } else if (e->isAKindOf(ssgTypeLeaf())) {
204 list<string>::const_iterator it;
205 for (it = cat_objects.begin(); it != cat_objects.end(); ++it) {
206 if (e->getName() && (*it) == e->getName()) {
207 e->setTraversalMaskBits(SSGTRAV_HOT);
208 e->setUserData( FGAICarrierHardware::newCatapult( this ) );
209 ssgLeaf *l = (ssgLeaf*)e;
210 if ( l->getNumLines() != 1 ) {
211 SG_LOG(SG_GENERAL, SG_ALERT,
212 "AICarrier: Found a cat not modelled with exactly one line!");
214 // Now some special code to make sure the cat points in the right
215 // direction. The 0 index must be the backward end, the 1 index
217 // Forward is positive x-direction in our 3D model, also the model
218 // as such is flattened when it is loaded, so we do not need to care
219 // for transforms ...
221 l->getLine(0, v, v+1 );
223 for (int k=0; k<2; ++k)
224 sgCopyVec3( ends[k], l->getVertex( v[k] ) );
226 // When the 1 end is behind the 0 end, swap the coordinates.
227 if (ends[0][0] < ends[1][0]) {
228 sgCopyVec3( l->getVertex( v[0] ), ends[1] );
229 sgCopyVec3( l->getVertex( v[1] ), ends[0] );
239 void FGAICarrier::UpdateFlols( double dt) {
240 /* cout << "x_offset " << flols_x_offset
241 << " y_offset " << flols_y_offset
242 << " z_offset " << flols_z_offset << endl;
244 cout << "roll " << roll
245 << " heading " << hdg
246 << " pitch " << pitch << endl;
248 cout << "carrier lon " << pos[0]
250 << " alt " << pos[2] << endl;*/
252 // set the Flols intitial position to the carrier position
256 /* cout << "flols lon " << flolspos[0]
257 << " lat " << flolspos[1]
258 << " alt " << flolspos[2] << endl;*/
260 // set the offsets in metres
262 /* cout << "flols_x_offset " << flols_x_offset << endl
263 << "flols_y_offset " << flols_y_offset << endl
264 << "flols_z_offset " << flols_z_offset << endl;*/
266 in[0] = flols_x_offset;
267 in[1] = flols_y_offset;
268 in[2] = flols_z_offset;
270 // pre-process the trig functions
272 cosRx = cos(roll * SG_DEGREES_TO_RADIANS);
273 sinRx = sin(roll * SG_DEGREES_TO_RADIANS);
274 cosRy = cos(pitch * SG_DEGREES_TO_RADIANS);
275 sinRy = sin(pitch * SG_DEGREES_TO_RADIANS);
276 cosRz = cos(hdg * SG_DEGREES_TO_RADIANS);
277 sinRz = sin(hdg * SG_DEGREES_TO_RADIANS);
279 // set up the transform matrix
281 trans[0][0] = cosRy * cosRz;
282 trans[0][1] = -1 * cosRx * sinRz + sinRx * sinRy * cosRz ;
283 trans[0][2] = sinRx * sinRz + cosRx * sinRy * cosRz;
285 trans[1][0] = cosRy * sinRz;
286 trans[1][1] = cosRx * cosRz + sinRx * sinRy * sinRz;
287 trans[1][2] = -1 * sinRx * cosRx + cosRx * sinRy * sinRz;
289 trans[2][0] = -1 * sinRy;
290 trans[2][1] = sinRx * cosRy;
291 trans[2][2] = cosRx * cosRy;
293 // multiply the input and transform matrices
295 out[0] = in[0] * trans[0][0] + in[1] * trans[0][1] + in[2] * trans[0][2];
296 out[1] = in[0] * trans[1][0] + in[1] * trans[1][1] + in[2] * trans[1][2];
297 out[2] = in[0] * trans[2][0] + in[1] * trans[2][1] + in[2] * trans[2][2];
299 // convert meters to ft to degrees of latitude
300 out[0] = (out[0] * 3.28083989501) /(366468.96 - 3717.12 * cos(flolspos[0] * SG_DEGREES_TO_RADIANS));
302 // convert meters to ft to degrees of longitude
303 out[1] = (out[1] * 3.28083989501)/(365228.16 * cos(flolspos[1] * SG_DEGREES_TO_RADIANS));
305 //print out the result
306 /* cout << "lat adjust deg" << out[0]
307 << " lon adjust deg " << out[1]
308 << " alt adjust m " << out[2] << endl;*/
310 // adjust Flols position
311 flolspos[0] += out[0];
312 flolspos[1] += out[1];
313 flolspos[2] += out[2];
315 // convert flols position to cartesian co-ordinates
317 sgGeodToCart(flolspos[1] * SG_DEGREES_TO_RADIANS,
318 flolspos[0] * SG_DEGREES_TO_RADIANS,
319 flolspos[2] , flolsXYZ );
322 /* cout << "flols X " << flolsXYZ[0]
323 << " Y " << flolsXYZ[1]
324 << " Z " << flolsXYZ[2] << endl;
326 // check the conversion
328 sgCartToGeod(flolsXYZ, &lat, &lon, &alt);
330 cout << "flols check lon " << lon
332 << " alt " << alt << endl; */
334 //get the current position of the pilot's eyepoint (cartesian cordinates)
336 sgdCopyVec3( eyeXYZ, globals->get_current_view()->get_absolute_view_pos() );
338 /* cout << "Eye_X " << eyeXYZ[0]
339 << " Eye_Y " << eyeXYZ[1]
340 << " Eye_Z " << eyeXYZ[2] << endl; */
342 sgCartToGeod(eyeXYZ, &lat, &lon, &alt);
344 eyepos[0] = lon * SG_RADIANS_TO_DEGREES;
345 eyepos[1] = lat * SG_RADIANS_TO_DEGREES;
348 /* cout << "eye lon " << eyepos[0]
349 << " eye lat " << eyepos[1]
350 << " eye alt " << eyepos[2] << endl; */
352 //calculate the ditance from eye to flols
354 dist = sgdDistanceVec3( flolsXYZ, eyeXYZ );
356 //cout << "distance " << dist << endl;
359 // calculate height above FLOLS
360 double y = eyepos[2] - flolspos[2];
362 // calculate the angle from the flols to eye
363 // above the horizontal
366 angle = asin( y / dist );
371 angle *= SG_RADIANS_TO_DEGREES;
374 // cout << " height " << y << " angle " << angle ;
376 // set the value of source
378 if ( angle <= 4.35 && angle > 4.01 )
380 else if ( angle <= 4.01 && angle > 3.670 )
382 else if ( angle <= 3.670 && angle > 3.330 )
384 else if ( angle <= 3.330 && angle > 2.990 )
386 else if ( angle <= 2.990 && angle > 2.650 )
388 else if ( angle <= 2.650 )
393 // cout << " source " << source << endl;
398 int FGAICarrierHardware::unique_id = 1;