]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIBallistic.cxx
Merge commit 'refs/merge-requests/1552' of git@gitorious.org:fg/flightgear into next
[flightgear.git] / src / AIModel / AIBallistic.cxx
1 // FGAIBallistic - FGAIBase-derived class creates a ballistic object
2 //
3 // Written by David Culp, started November 2003.
4 // - davidculp2@comcast.net
5 //
6 // With major additions by Mathias Froehlich & Vivian Meazza 2004-2008
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21
22 #ifdef HAVE_CONFIG_H
23 #  include <config.h>
24 #endif
25
26 #include <simgear/math/sg_random.h>
27 #include <simgear/math/sg_geodesy.hxx>
28 #include <simgear/scene/model/modellib.hxx>
29
30 #include <Scenery/scenery.hxx>
31
32 #include "AIBallistic.hxx"
33
34 #include <Main/util.hxx>
35
36 using namespace simgear;
37
38 const double FGAIBallistic::slugs_to_kgs = 14.5939029372;
39 const double FGAIBallistic::slugs_to_lbs = 32.1740485564;
40
41 FGAIBallistic::FGAIBallistic(object_type ot) :
42 FGAIBase(ot),
43 _height(0.0),
44 _ht_agl_ft(0.0),
45 _azimuth(0.0),
46 _elevation(0.0),
47 _rotation(0.0),
48 _formate_to_ac(false),
49 _aero_stabilised(false),
50 _drag_area(0.007),
51 _life_timer(0.0),
52 _gravity(32.1740485564),
53 _buoyancy(0),
54 _wind(true),
55 _mass(0),
56 _random(false),
57 _load_resistance(0),
58 _solid(false),
59 _force_stabilised(false),
60 _slave_to_ac(false),
61 _slave_load_to_ac(false),
62 _contents_lb(0),
63 _report_collision(false),
64 _report_expiry(false),
65 _report_impact(false),
66 _external_force(false),
67 _impact_report_node(fgGetNode("/ai/models/model-impact", true)),
68 _old_height(0),
69 _elapsed_time(0),
70 hs(0)
71
72 {
73     no_roll = false;
74 }
75
76 FGAIBallistic::~FGAIBallistic() {
77 }
78
79 void FGAIBallistic::readFromScenario(SGPropertyNode* scFileNode) {
80     if (!scFileNode){
81         return;
82     }
83
84     FGAIBase::readFromScenario(scFileNode);
85
86     //setPath(scFileNode->getStringValue("model", "Models/Geometry/rocket.ac")); 
87     setRandom(scFileNode->getBoolValue("random", false));
88     setAzimuth(scFileNode->getDoubleValue("azimuth", 0.0));
89     setElevation(scFileNode->getDoubleValue("elevation", 0));
90     setDragArea(scFileNode->getDoubleValue("eda", 0.007));
91     setLife(scFileNode->getDoubleValue("life", 900.0));
92     setBuoyancy(scFileNode->getDoubleValue("buoyancy", 0));
93     //setWind_from_east(scFileNode->getDoubleValue("wind_from_east", 0));
94     //setWind_from_north(scFileNode->getDoubleValue("wind_from_north", 0));
95     setWind(scFileNode->getBoolValue("wind", false));
96     setRoll(scFileNode->getDoubleValue("roll", 0.0));
97     setCd(scFileNode->getDoubleValue("cd", 0.029));
98     //setMass(scFileNode->getDoubleValue("mass", 0.007));
99     setWeight(scFileNode->getDoubleValue("weight", 0.25));
100     setStabilisation(scFileNode->getBoolValue("aero-stabilised", false));
101     setNoRoll(scFileNode->getBoolValue("no-roll", false));
102     setImpact(scFileNode->getBoolValue("impact", false));
103     setExpiry(scFileNode->getBoolValue("expiry", false));
104     setCollision(scFileNode->getBoolValue("collision", false));
105     setImpactReportNode(scFileNode->getStringValue("impact-reports"));
106     setName(scFileNode->getStringValue("name", "Rocket"));
107     setFuseRange(scFileNode->getDoubleValue("fuse-range", 0.0));
108     setSMPath(scFileNode->getStringValue("submodel-path", ""));
109     setSubID(scFileNode->getIntValue("SubID", 0));
110     setExternalForce(scFileNode->getBoolValue("external-force", false));
111     setForcePath(scFileNode->getStringValue("force-path", ""));
112     setForceStabilisation(scFileNode->getBoolValue("force-stabilised", false));
113     setXoffset(scFileNode->getDoubleValue("x-offset", 0.0));
114     setYoffset(scFileNode->getDoubleValue("y-offset", 0.0));
115     setZoffset(scFileNode->getDoubleValue("z-offset", 0.0));
116     setPitchoffset(scFileNode->getDoubleValue("pitch-offset", 0.0));
117     setRolloffset(scFileNode->getDoubleValue("roll-offset", 0.0));
118     setYawoffset(scFileNode->getDoubleValue("yaw-offset", 0.0));
119     setGroundOffset(scFileNode->getDoubleValue("ground-offset", 0.0));
120     setLoadOffset(scFileNode->getDoubleValue("load-offset", 0.0));
121     setSlaved(scFileNode->getBoolValue("slaved", false));
122     setSlavedLoad(scFileNode->getBoolValue("slaved-load", false));
123     setContentsPath(scFileNode->getStringValue("contents"));
124 }
125
126 bool FGAIBallistic::init(bool search_in_AI_path) {
127     FGAIBase::init(search_in_AI_path);
128
129     _impact_reported = false;
130     _collision_reported = false;
131     _expiry_reported = false;
132
133     _impact_lat = 0;
134     _impact_lon = 0;
135     _impact_elev = 0;
136     _impact_hdg = 0;
137     _impact_pitch = 0;
138     _impact_roll = 0;
139     _impact_speed = 0;
140
141     invisible = false;
142
143     _elapsed_time += (sg_random() * 100);
144
145     props->setStringValue("material/name", "");
146     props->setStringValue("name", _name.c_str());
147     props->setStringValue("submodels/path", _path.c_str());
148
149     if (_slave_to_ac){
150         props->setStringValue("force/path", _force_path.c_str());
151         props->setStringValue("contents/path", _contents_path.c_str());
152     }
153
154     //props->setStringValue("vector/path", _vector_path.c_str());
155
156     // start with high value so that animations don't trigger yet
157     _ht_agl_ft = 1e10;
158     hdg = _azimuth;
159     pitch = _elevation;
160     roll = _rotation;
161
162     Transform();
163
164     //cout << _name << " speed init: " << speed << endl;
165
166     return true;
167 }
168
169 void FGAIBallistic::bind() {
170     //    FGAIBase::bind();
171
172     props->tie("sim/time/elapsed-sec",
173         SGRawValueMethods<FGAIBallistic,double>(*this,
174         &FGAIBallistic::_getTime));
175     //props->tie("mass-slug",
176     //    SGRawValueMethods<FGAIBallistic,double>(*this,
177     //    &FGAIBallistic::getMass));
178
179     props->tie("material/solid",
180         SGRawValuePointer<bool>(&_solid));
181     props->tie("altitude-agl-ft",
182         SGRawValuePointer<double>(&_ht_agl_ft));
183     props->tie("controls/slave-to-ac",
184         SGRawValueMethods<FGAIBallistic,bool>
185         (*this, &FGAIBallistic::getSlaved, &FGAIBallistic::setSlaved));
186     props->tie("controls/invisible",
187         SGRawValuePointer<bool>(&invisible));
188
189     if(_external_force || _slave_to_ac){
190         props->tie("controls/force_stabilized",
191             SGRawValuePointer<bool>(&_force_stabilised));
192         props->tie("position/global-x", 
193             SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getCartPosX, 0));
194         props->tie("position/global-y",
195             SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getCartPosY, 0));
196         props->tie("position/global-z",
197             SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getCartPosZ, 0));
198         props->tie("velocities/vertical-speed-fps",
199             SGRawValuePointer<double>(&vs));
200         props->tie("velocities/true-airspeed-kt",
201             SGRawValuePointer<double>(&speed));
202         props->tie("velocities/horizontal-speed-fps",
203             SGRawValuePointer<double>(&hs));
204         props->tie("position/altitude-ft",
205             SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getElevationFt, &FGAIBase::_setAltitude));
206         props->tie("position/latitude-deg", 
207             SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getLatitude, &FGAIBase::_setLatitude));
208         props->tie("position/longitude-deg",
209             SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getLongitude, &FGAIBase::_setLongitude));
210         props->tie("orientation/hdg-deg",
211             SGRawValuePointer<double>(&hdg));
212         props->tie("orientation/pitch-deg",
213             SGRawValuePointer<double>(&pitch));
214         props->tie("orientation/roll-deg",
215             SGRawValuePointer<double>(&roll));
216         props->tie("controls/slave-load-to-ac",
217             SGRawValueMethods<FGAIBallistic,bool>
218             (*this, &FGAIBallistic::getSlavedLoad, &FGAIBallistic::setSlavedLoad));
219         props->tie("position/load-offset",
220             SGRawValueMethods<FGAIBallistic,double>
221             (*this, &FGAIBallistic::getLoadOffset, &FGAIBallistic::setLoadOffset));
222         props->tie("load/distance-to-hitch-ft",
223             SGRawValueMethods<FGAIBallistic,double>
224             (*this, &FGAIBallistic::getDistanceLoadToHitch));
225         props->tie("load/elevation-to-hitch-deg",
226             SGRawValueMethods<FGAIBallistic,double>
227             (*this, &FGAIBallistic::getElevLoadToHitch));
228         props->tie("load/bearing-to-hitch-deg",
229             SGRawValueMethods<FGAIBallistic,double>
230             (*this, &FGAIBallistic::getBearingLoadToHitch));
231         props->tie("material/load-resistance",
232         SGRawValuePointer<double>(&_load_resistance));
233     }
234
235 }
236
237 void FGAIBallistic::unbind() {
238 //    FGAIBase::unbind();
239
240     props->untie("sim/time/elapsed-sec");
241     props->untie("mass-slug");
242     props->untie("material/solid");
243     props->untie("altitude-agl-ft");
244     props->untie("controls/slave-to-ac");
245     props->untie("controls/invisible");
246
247     if(_external_force || _slave_to_ac){
248         props->untie("position/global-y");
249         props->untie("position/global-x");
250         props->untie("position/global-z");
251         props->untie("velocities/vertical-speed-fps");
252         props->untie("velocities/true-airspeed-kt");
253         props->untie("velocities/horizontal-speed-fps");
254         props->untie("position/altitude-ft");
255         props->untie("position/latitude-deg");
256         props->untie("position/longitude-deg");
257         props->untie("position/ht-agl-ft");
258         props->untie("orientation/hdg-deg");
259         props->untie("orientation/pitch-deg");
260         props->untie("orientation/roll-deg");
261         props->untie("controls/force_stabilized");
262         props->untie("position/load-offset");
263         props->untie("load/distance-to-hitch-ft");
264         props->untie("load/elevation-to-hitch-deg");
265         props->untie("load/bearing-to-hitch-deg");
266         props->untie("material/load-resistance");
267     }
268 }
269
270 void FGAIBallistic::update(double dt) {
271     FGAIBase::update(dt);
272     _setUserPos();
273
274     if (_formate_to_ac){
275         formateToAC(dt);
276         Transform();
277     } else if (_slave_to_ac){
278         slaveToAC(dt);
279         Transform();
280     } else if (!invisible){
281         Run(dt);
282         Transform();
283     }
284
285 }
286
287 void FGAIBallistic::setAzimuth(double az) {
288
289     if (_random)
290         hdg = _azimuth = (az - 5 ) + (10 * sg_random());
291     else 
292         hdg = _azimuth = az;
293
294     //cout << _name << " init hdg " << hdg << " random " << _random << endl;
295 }
296
297 void FGAIBallistic::setElevation(double el) {
298     pitch = _elevation = el;
299 }
300
301 void FGAIBallistic::setRoll(double rl) {
302     roll = _rotation = rl;
303 }
304
305 void FGAIBallistic::setStabilisation(bool val) {
306     _aero_stabilised = val;
307 }
308
309 void FGAIBallistic::setForceStabilisation(bool val) {
310     _force_stabilised = val;
311 }
312
313 void FGAIBallistic::setNoRoll(bool nr) {
314     no_roll = nr;
315 }
316
317 void FGAIBallistic::setDragArea(double a) {
318     _drag_area = a;
319 }
320
321 void FGAIBallistic::setLife(double seconds) {
322
323     if (_random){
324         life = seconds * _randomness + (seconds * (1 -_randomness) * sg_random());
325         //cout << "life " << life << endl;
326     } else
327         life = seconds;
328 }
329
330 void FGAIBallistic::setBuoyancy(double fpss) {
331     _buoyancy = fpss;
332 }
333
334 void FGAIBallistic::setWind_from_east(double fps) {
335     _wind_from_east = fps;
336 }
337
338 void FGAIBallistic::setWind_from_north(double fps) {
339     _wind_from_north = fps;
340 }
341
342 void FGAIBallistic::setWind(bool val) {
343     _wind = val;
344 }
345
346 void FGAIBallistic::setCd(double c) {
347     _Cd = c;
348 }
349
350 void FGAIBallistic::setMass(double m) {
351     _mass = m;
352 }
353
354 void FGAIBallistic::setWeight(double w) {
355     _weight_lb = w;
356 }
357
358 void FGAIBallistic::setRandomness(double r) {
359     _randomness = r;
360 }
361
362 void FGAIBallistic::setRandom(bool r) {
363     _random = r;
364 }
365
366 void FGAIBallistic::setImpact(bool i) {
367     _report_impact = i;
368 }
369
370 void FGAIBallistic::setCollision(bool c) {
371     _report_collision = c;
372 }
373
374 void FGAIBallistic::setExpiry(bool e) {
375     _report_expiry = e;
376 }
377
378 void FGAIBallistic::setExternalForce(bool f) {
379     _external_force = f;
380 }
381
382 void FGAIBallistic::setImpactReportNode(const string& path) {
383
384     if (!path.empty())
385         _impact_report_node = fgGetNode(path.c_str(), true);
386 }
387
388 void FGAIBallistic::setName(const string& n) {
389     _name = n;
390 }
391
392 void FGAIBallistic::setSMPath(const string& s) {
393     _path = s;
394     //cout << "submodel path " << _path << endl;
395 }
396
397 void FGAIBallistic::setFuseRange(double f) {
398     _fuse_range = f;
399 }
400
401 void FGAIBallistic::setSubID(int i) {
402     _subID = i;
403 }
404
405 void FGAIBallistic::setSubmodel(const string& s) {
406     _submodel = s;
407 }
408
409 void FGAIBallistic::setGroundOffset(double g) {
410     _ground_offset = g;
411 }
412
413 void FGAIBallistic::setLoadOffset(double l) {
414     _load_offset = l;
415 }
416
417 double FGAIBallistic::getLoadOffset() const {
418     return _load_offset;
419 }
420
421 void FGAIBallistic::setSlaved(bool s) {
422     _slave_to_ac = s;
423 }
424
425 void FGAIBallistic::setFormate(bool f) {
426     _formate_to_ac = f;
427 }
428
429 void FGAIBallistic::setContentsPath(const string& path) {
430
431     _contents_path = path;
432
433     if (!path.empty()) {
434         _contents_node = fgGetNode(path.c_str(), true);
435     }
436 }
437
438 void FGAIBallistic::setContentsNode(SGPropertyNode_ptr node) {
439
440     if (node != 0) {
441         _contents_node = node;
442         _contents_path = _contents_node->getDisplayName();
443     }
444 }
445
446 void FGAIBallistic::setParentNode(SGPropertyNode_ptr node) {
447     if (node != 0) {
448         _pnode = node;
449         _p_pos_node = _pnode->getChild("position", 0, true);
450         _p_lat_node = _p_pos_node->getChild("latitude-deg", 0, true);
451         _p_lon_node = _p_pos_node->getChild("longitude-deg", 0, true);
452         _p_alt_node = _p_pos_node->getChild("altitude-ft", 0, true);
453
454         _p_ori_node = _pnode->getChild("orientation", 0, true);
455         _p_pch_node = _p_ori_node->getChild("pitch-deg", 0, true);
456         _p_rll_node = _p_ori_node->getChild("roll-deg", 0, true);
457         _p_hdg_node = _p_ori_node->getChild("true-heading-deg",0, true);
458
459         _p_vel_node = _pnode->getChild("velocities", 0, true);
460         _p_spd_node = _p_vel_node->getChild("true-airspeed-kt", 0, true);
461
462     }
463 }
464
465 void FGAIBallistic::setParentPos() {
466     if (_pnode != 0) {
467         double lat = _p_lat_node->getDoubleValue();
468         double lon = _p_lon_node->getDoubleValue();
469         double alt = _p_alt_node->getDoubleValue();
470
471         _parentpos.setLongitudeDeg(lon);
472         _parentpos.setLatitudeDeg(lat);
473         _parentpos.setElevationFt(alt);
474
475     }
476 }
477
478 bool FGAIBallistic::getSlaved() const {
479     return _slave_to_ac;
480 }
481
482 bool FGAIBallistic::getFormate() const {
483     return _formate_to_ac;
484 }
485
486 double FGAIBallistic::getMass() const {
487     return _mass;
488 }
489
490 double FGAIBallistic::getContents() {
491     if(_contents_node){
492         _contents_lb = _contents_node->getChild("level-lbs",0,1)->getDoubleValue();
493     }
494     return _contents_lb;
495 }
496
497 void FGAIBallistic::setContents(double c) {
498     if(_contents_node) 
499         _contents_lb = _contents_node->getChild("level-gal_us",0,1)->setDoubleValue(c);
500 }
501
502 void FGAIBallistic::setSlavedLoad(bool l) {
503     _slave_load_to_ac = l;
504 }
505
506 bool FGAIBallistic::getSlavedLoad() const {
507     return _slave_load_to_ac;
508 }
509
510 void FGAIBallistic::setForcePath(const string& p) {
511     _force_path = p;
512     if (!_force_path.empty()) {
513         SGPropertyNode *fnode = fgGetNode(_force_path.c_str(), 0, true );
514         _force_node = fnode->getChild("force-lb", 0, true);
515         _force_azimuth_node = fnode->getChild("force-azimuth-deg", 0, true);
516         _force_elevation_node = fnode->getChild("force-elevation-deg", 0, true);
517     }
518 }
519
520 bool FGAIBallistic::getHtAGL(double start){
521
522     if (getGroundElevationM(SGGeod::fromGeodM(pos, start),
523         _elevation_m, &_material)) {
524             _ht_agl_ft = pos.getElevationFt() - _elevation_m * SG_METER_TO_FEET;
525
526             if (_material) {
527                 const vector<string>& names = _material->get_names();
528                 _solid = _material->get_solid();
529                 _load_resistance = _material->get_load_resistance();
530                 _frictionFactor =_material->get_friction_factor();
531
532                 if (!names.empty())
533                     props->setStringValue("material/name", names[0].c_str());
534                 else
535                     props->setStringValue("material/name", "");
536
537                 _mat_name = names[0];
538
539                 //cout << "material " << _mat_name 
540                 //<< " solid " << _solid 
541                 //<< " load " << _load_resistance
542                 //<< " frictionFactor " << _frictionFactor
543                 //<< endl;
544
545             }
546
547             return true;
548     } else {
549         return false;
550     }
551
552 }
553
554 double FGAIBallistic::getRecip(double az){
555     // calculate the reciprocal of the input azimuth 
556     if(az - 180 < 0){
557         return az + 180;
558     } else {
559         return az - 180; 
560     }
561 }
562
563 void FGAIBallistic::setPch(double e, double dt, double coeff){
564     double c = dt / (coeff + dt);
565     pitch = (e * c) + (pitch * (1 - c));
566 }
567
568 void FGAIBallistic::setBnk(double r, double dt, double coeff){
569     double c = dt / (coeff + dt);
570     roll = (r * c) + (roll * (1 - c));
571 }
572
573 void FGAIBallistic::setHt(double h, double dt, double coeff){
574     double c = dt / (coeff + dt);
575     _height = (h * c) + (_height * (1 - c));
576 }
577
578 void FGAIBallistic::setHdg(double az, double dt, double coeff){
579     double recip = getRecip(hdg);
580     double c = dt / (coeff + dt);
581     //we need to ensure that we turn the short way to the new hdg
582     if (az < recip && az < hdg && hdg > 180) {
583         hdg = ((az + 360) * c) + (hdg * (1 - c));
584     } else if (az > recip && az > hdg && hdg <= 180){
585         hdg = ((az - 360) * c) + (hdg * (1 - c));
586     } else {
587         hdg = (az * c) + (hdg * (1 - c));
588     }
589 }
590
591 double  FGAIBallistic::getTgtXOffset() const {
592     return _tgt_x_offset;
593 }
594
595 double  FGAIBallistic::getTgtYOffset() const {
596     return _tgt_y_offset;
597
598
599 double  FGAIBallistic::getTgtZOffset() const {
600     return _tgt_z_offset;
601 }
602
603 void FGAIBallistic::setTgtXOffset(double x){
604     _tgt_x_offset = x;
605 }
606
607 void FGAIBallistic::setTgtYOffset(double y){
608     _tgt_y_offset = y;
609 }
610
611 void FGAIBallistic::setTgtZOffset(double z){
612     _tgt_z_offset = z;
613 }
614
615 void FGAIBallistic::slaveToAC(double dt){
616
617     double hdg, pch, rll = 0;
618
619     if (_pnode != 0) {
620         setParentPos();
621         hdg = _p_hdg_node->getDoubleValue();
622         pch = _p_pch_node->getDoubleValue();
623         rll = _p_rll_node->getDoubleValue();
624         setOffsetPos(_parentpos, hdg, pch, rll);
625         setSpeed(_p_spd_node->getDoubleValue());
626     }else {
627         hdg = manager->get_user_heading();
628         pch = manager->get_user_pitch();
629         rll = manager->get_user_roll();
630         setOffsetPos(userpos, hdg, pch, rll);
631         setSpeed(manager->get_user_speed());
632     }
633
634     pos.setLatitudeDeg(_offsetpos.getLatitudeDeg());
635     pos.setLongitudeDeg(_offsetpos.getLongitudeDeg());
636     pos.setElevationFt(_offsetpos.getElevationFt());
637     setHeading(hdg);
638     setPitch(pch + _pitch_offset);
639     setBank(rll + _roll_offset);
640     setOffsetVelocity(dt, pos);
641
642     //update the mass (slugs)
643     _mass = (_weight_lb + getContents()) / slugs_to_lbs;
644
645     _impact_reported = false;
646
647     //cout << _name << " _mass "<<_mass <<" " << getContents() 
648     //<< " " << getContents() / slugs_to_lbs << " weight " << _weight_lb << endl;
649     //    cout << _name << " update hs " << hs << " vs " << vs << endl;
650 }
651
652 void FGAIBallistic::Run(double dt) {
653     _life_timer += dt;
654
655     // if life = -1 the object does not die
656     if (_life_timer > life && life != -1){
657
658         if (_report_expiry && !_expiry_reported && !_impact_reported && !_collision_reported){
659             //cout<<"AIBallistic: expiry"<< endl;
660             handle_expiry();
661         } else
662             setDie(true);
663
664     }
665
666     //set the contents in the appropriate tank or other property in the parent to zero
667     setContents(0);
668
669     //randomise Cd by +- 10%
670     if (_random)
671         _Cd = _Cd * 0.90 + (0.10 * sg_random());
672
673     // Adjust Cd by Mach number. The equations are based on curves
674     // for a conventional shell/bullet (no boat-tail).
675     double Cdm;
676
677     if (Mach < 0.7)
678         Cdm = 0.0125 * Mach + _Cd;
679     else if (Mach < 1.2 )
680         Cdm = 0.3742 * pow(Mach, 2) - 0.252 * Mach + 0.0021 + _Cd;
681     else
682         Cdm = 0.2965 * pow(Mach, -1.1506) + _Cd;
683
684     //cout <<_name << " Mach " << Mach << " Cdm " << Cdm 
685     //    << " ballistic speed kts "<< speed <<  endl;
686
687     // drag = Cd * 0.5 * rho * speed * speed * drag_area;
688     // rho is adjusted for altitude in void FGAIBase::update,
689     // using Standard Atmosphere (sealevel temperature 15C)
690     // acceleration = drag/mass;
691     // adjust speed by drag
692     speed -= (Cdm * 0.5 * rho * speed * speed * _drag_area/_mass) * dt;
693
694     // don't let speed become negative
695     if ( speed < 0.0 )
696         speed = 0.0;
697
698     double speed_fps = speed * SG_KT_TO_FPS;
699     //double hs;
700
701     // calculate vertical and horizontal speed components
702     if (speed == 0.0) {
703         hs = vs = 0.0;
704     } else {
705         vs = sin( _elevation * SG_DEGREES_TO_RADIANS ) * speed_fps;
706         hs = cos( _elevation * SG_DEGREES_TO_RADIANS ) * speed_fps;
707     }
708
709     //resolve horizontal speed into north and east components:
710     double speed_north_fps = cos(_azimuth / SG_RADIANS_TO_DEGREES) * hs;
711     double speed_east_fps = sin(_azimuth / SG_RADIANS_TO_DEGREES) * hs;
712
713     // convert horizontal speed (fps) to degrees per second
714     double speed_north_deg_sec = speed_north_fps / ft_per_deg_lat;
715     double speed_east_deg_sec  = speed_east_fps / ft_per_deg_lon;
716
717     // if wind not required, set to zero
718     if (!_wind) {
719         _wind_from_north = 0;
720         _wind_from_east = 0;
721     } else {
722         _wind_from_north = manager->get_wind_from_north();
723         _wind_from_east = manager->get_wind_from_east();
724     }
725
726     //calculate velocity due to external force
727     double force_speed_north_deg_sec = 0;
728     double force_speed_east_deg_sec = 0;
729 //    double vs_force_fps = 0;
730     double hs_force_fps = 0;
731     double v_force_acc_fpss = 0;
732     double force_speed_north_fps = 0;
733     double force_speed_east_fps = 0;
734     double h_force_lbs = 0;
735     double normal_force_lbs = 0;
736     double normal_force_fpss = 0;
737     double static_friction_force_lbs = 0;
738     double dynamic_friction_force_lbs = 0;
739     double friction_force_speed_north_fps = 0;
740     double friction_force_speed_east_fps = 0;
741     double friction_force_speed_north_deg_sec = 0;
742     double friction_force_speed_east_deg_sec = 0;
743     double force_elevation_deg = 0;
744
745     if (_external_force) {
746         //cout << _name << " external force" << endl;
747
748         SGPropertyNode *n = fgGetNode(_force_path.c_str(), true);
749         double force_lbs            = n->getChild("force-lb", 0, true)->getDoubleValue();
750         force_elevation_deg         = n->getChild("force-elevation-deg", 0, true)->getDoubleValue();
751         double force_azimuth_deg    = n->getChild("force-azimuth-deg", 0, true)->getDoubleValue();
752         
753         //resolve force into vertical and horizontal components:
754         double v_force_lbs = force_lbs * sin( force_elevation_deg * SG_DEGREES_TO_RADIANS );
755         h_force_lbs = force_lbs * cos( force_elevation_deg * SG_DEGREES_TO_RADIANS );
756
757         //ground interaction 
758
759         if (getHtAGL(10000)){
760             double deadzone = 0.1;
761
762             if (_ht_agl_ft <= (0 + _ground_offset + deadzone) && _solid){
763                 normal_force_lbs = (_mass * slugs_to_lbs) - v_force_lbs;
764
765                 if ( normal_force_lbs < 0 )
766                     normal_force_lbs = 0;
767
768                 pos.setElevationFt(0 + _ground_offset);
769                 if (vs < 0) 
770                     vs = -vs * 0.5;
771
772                 // calculate friction
773                 // we assume a static Coefficient of Friction (mu) of 0.62 (wood on concrete)
774                 double mu = 0.62;
775
776                 static_friction_force_lbs = mu * normal_force_lbs * _frictionFactor;
777
778                 //adjust horizontal force. We assume that a speed of <= 5 fps is static 
779                 if (h_force_lbs <= static_friction_force_lbs && hs <= 5){
780                     h_force_lbs = hs = 0;
781                     speed_north_fps = speed_east_fps = 0;
782                 } else
783                     dynamic_friction_force_lbs = (static_friction_force_lbs * 0.95);
784
785                 //ignore wind when on the ground for now
786                 //TODO fix this
787                 _wind_from_north = 0;
788                 _wind_from_east = 0;
789
790             }
791
792         }
793
794         //acceleration = (force(lbsf)/mass(slugs))
795         v_force_acc_fpss = v_force_lbs/_mass;
796         normal_force_fpss = normal_force_lbs/_mass;
797         double h_force_acc_fpss = h_force_lbs/_mass;
798         double dynamic_friction_acc_fpss = dynamic_friction_force_lbs/_mass;
799
800         // velocity = acceleration * dt
801         hs_force_fps = h_force_acc_fpss * dt;
802         double friction_force_fps = dynamic_friction_acc_fpss * dt;
803
804         //resolve horizontal speeds into north and east components:
805         force_speed_north_fps   = cos(force_azimuth_deg * SG_DEGREES_TO_RADIANS) * hs_force_fps;
806         force_speed_east_fps    = sin(force_azimuth_deg * SG_DEGREES_TO_RADIANS) * hs_force_fps;
807
808         friction_force_speed_north_fps = cos(getRecip(hdg) * SG_DEGREES_TO_RADIANS) * friction_force_fps;
809         friction_force_speed_east_fps  = sin(getRecip(hdg) * SG_DEGREES_TO_RADIANS) * friction_force_fps;
810
811         // convert horizontal speed (fps) to degrees per second
812         force_speed_north_deg_sec = force_speed_north_fps / ft_per_deg_lat;
813         force_speed_east_deg_sec  = force_speed_east_fps / ft_per_deg_lon;
814
815         friction_force_speed_north_deg_sec = friction_force_speed_north_fps / ft_per_deg_lat;
816         friction_force_speed_east_deg_sec  = friction_force_speed_east_fps / ft_per_deg_lon;
817     }
818
819     // convert wind speed (fps) to degrees lat/lon per second
820     double wind_speed_from_north_deg_sec = _wind_from_north / ft_per_deg_lat;
821     double wind_speed_from_east_deg_sec  = _wind_from_east / ft_per_deg_lon;
822
823     //recombine the horizontal velocity components
824     hs = sqrt(((speed_north_fps + force_speed_north_fps + friction_force_speed_north_fps) 
825         * (speed_north_fps + force_speed_north_fps + friction_force_speed_north_fps))
826         + ((speed_east_fps + force_speed_east_fps + friction_force_speed_east_fps) 
827         * (speed_east_fps + force_speed_east_fps + friction_force_speed_east_fps)));
828
829     if (hs <= 0.00001)
830         hs = 0;
831
832     // adjust vertical speed for acceleration of gravity, buoyancy, and vertical force
833     vs -= (_gravity - _buoyancy - v_force_acc_fpss - normal_force_fpss) * dt;
834
835     if (vs <= 0.00001 && vs >= -0.00001)
836         vs = 0;
837
838     // set new position
839     if(_slave_load_to_ac) {
840         setOffsetPos(pos, 
841             manager->get_user_heading(),
842             manager->get_user_pitch(), 
843             manager->get_user_roll()
844             );
845         pos.setLatitudeDeg(_offsetpos.getLatitudeDeg());
846         pos.setLongitudeDeg(_offsetpos.getLongitudeDeg());
847         pos.setElevationFt(_offsetpos.getElevationFt());
848
849         if (getHtAGL(10000)){
850             double deadzone = 0.1;
851
852             if (_ht_agl_ft <= (0 + _ground_offset + deadzone) && _solid){
853                 pos.setElevationFt(0 + _ground_offset);
854             } else {
855                 pos.setElevationFt(_offsetpos.getElevationFt() + _load_offset);
856             }
857
858         }
859     } else {
860         pos.setLatitudeDeg( pos.getLatitudeDeg()
861             + (speed_north_deg_sec - wind_speed_from_north_deg_sec 
862             + force_speed_north_deg_sec + friction_force_speed_north_deg_sec) * dt );
863         pos.setLongitudeDeg( pos.getLongitudeDeg()
864             + (speed_east_deg_sec - wind_speed_from_east_deg_sec 
865             + force_speed_east_deg_sec + friction_force_speed_east_deg_sec) * dt );
866         pos.setElevationFt(pos.getElevationFt() + vs * dt);
867     }
868
869 //    cout << _name << " run hs " << hs << " vs " << vs << endl;
870
871     // recalculate total speed
872     if ( vs == 0 && hs == 0)
873         speed = 0;
874     else
875         speed = sqrt( vs * vs + hs * hs) / SG_KT_TO_FPS;
876
877     // recalculate elevation and azimuth (velocity vectors)
878     _elevation = atan2( vs, hs ) * SG_RADIANS_TO_DEGREES;
879     _azimuth =  atan2((speed_east_fps + force_speed_east_fps + friction_force_speed_east_fps), 
880         (speed_north_fps + force_speed_north_fps + friction_force_speed_north_fps))
881         * SG_RADIANS_TO_DEGREES;
882
883     // rationalise azimuth
884     if (_azimuth < 0)
885         _azimuth += 360;
886
887     if (_aero_stabilised) { // we simulate rotational moment of inertia by using a filter
888         //cout<< "_aero_stabilised "<< endl;
889         const double coeff = 0.9;
890
891         // we assume a symetrical MI about the pitch and yaw axis
892         setPch(_elevation, dt, coeff);
893         setHdg(_azimuth, dt, coeff);
894     } else if (_force_stabilised) { // we simulate rotational moment of inertia by using a filter
895         //cout<< "_force_stabilised "<< endl;
896         
897         const double coeff = 0.9;
898         double ratio = h_force_lbs/(_mass * slugs_to_lbs);
899
900         if (ratio >  1) ratio =  1;
901         if (ratio < -1) ratio = -1;
902
903         double force_pitch = acos(ratio) * SG_RADIANS_TO_DEGREES;
904
905         if (force_pitch <= force_elevation_deg)
906             force_pitch = force_elevation_deg;
907
908         // we assume a symetrical MI about the pitch and yaw axis
909         setPch(force_pitch,dt, coeff);
910         setHdg(_azimuth, dt, coeff);
911     }
912
913     //do impacts and collisions
914     if (_report_impact && !_impact_reported)
915         handle_impact();
916
917     if (_report_collision && !_collision_reported)
918         handle_collision();
919
920     // set destruction flag if altitude less than sea level -1000
921     if (altitude_ft < -1000.0 && life != -1)
922         setDie(true);
923
924 }  // end Run
925
926 double FGAIBallistic::_getTime() const {
927     return _life_timer;
928 }
929
930 void FGAIBallistic::handle_impact() {
931
932     // try terrain intersection
933     double start = pos.getElevationM() + 10;
934
935     if(!getHtAGL(start)) 
936         return;
937
938     if (_ht_agl_ft <= 0) {
939         SG_LOG(SG_GENERAL, SG_DEBUG, "AIBallistic: terrain impact");
940         report_impact(_elevation_m);
941         _impact_reported = true;
942
943         if (life == -1){
944             invisible = true;
945         } else if (_subID == 0)  // kill the AIObject if there is no subsubmodel
946             setDie(true);
947     } 
948 }
949
950 void FGAIBallistic::handle_expiry() {
951
952     SG_LOG(SG_GENERAL, SG_DEBUG, "AIBallistic: handle_expiry " << pos.getElevationM());
953
954     report_impact(pos.getElevationM());
955     _expiry_reported = true;
956
957     if (life == -1){
958         invisible = true;
959     } else if (_subID == 0){  // kill the AIObject if there is no subsubmodel
960         setDie(true);
961     }
962
963 }
964
965 void FGAIBallistic::handle_collision()
966 {
967     const FGAIBase *object = manager->calcCollision(pos.getElevationFt(),
968         pos.getLatitudeDeg(),pos.getLongitudeDeg(), _fuse_range);
969
970     if (object) {
971         report_impact(pos.getElevationM(), object);
972         _collision_reported = true;
973     }
974 }
975
976 void FGAIBallistic::report_impact(double elevation, const FGAIBase *object)
977 {
978     _impact_lat    = pos.getLatitudeDeg();
979     _impact_lon    = pos.getLongitudeDeg();
980     _impact_elev   = elevation;
981     _impact_speed  = speed * SG_KT_TO_MPS;
982     _impact_hdg    = hdg;
983     _impact_pitch  = pitch;
984     _impact_roll   = roll;
985
986     SGPropertyNode *n = props->getNode("impact", true);
987
988     if (object)
989         n->setStringValue("type", object->getTypeString());
990     else
991         n->setStringValue("type", "terrain");
992
993     SG_LOG(SG_GENERAL, SG_DEBUG, "AIBallistic: object impact" << _name << " lon " <<_impact_lon);
994
995     n->setDoubleValue("longitude-deg", _impact_lon);
996     n->setDoubleValue("latitude-deg", _impact_lat);
997     n->setDoubleValue("elevation-m", _impact_elev);
998     n->setDoubleValue("heading-deg", _impact_hdg);
999     n->setDoubleValue("pitch-deg", _impact_pitch);
1000     n->setDoubleValue("roll-deg", _impact_roll);
1001     n->setDoubleValue("speed-mps", _impact_speed);
1002
1003     _impact_report_node->setStringValue(props->getPath());
1004 }
1005
1006 SGVec3d FGAIBallistic::getCartUserPos() const {
1007     SGVec3d cartUserPos = SGVec3d::fromGeod(userpos);
1008     return cartUserPos;
1009 }
1010
1011 SGVec3d FGAIBallistic::getCartHitchPos() const{
1012
1013     // convert geodetic positions to geocentered
1014     SGVec3d cartuserPos = SGVec3d::fromGeod(userpos);
1015     //SGVec3d cartPos = getCartPos();
1016
1017     // Transform to the right coordinate frame, configuration is done in
1018     // the x-forward, y-right, z-up coordinates (feet), computation
1019     // in the simulation usual body x-forward, y-right, z-down coordinates
1020     // (meters) )
1021     SGVec3d _off(_x_offset * SG_FEET_TO_METER,
1022             _y_offset * SG_FEET_TO_METER,
1023             -_z_offset * SG_FEET_TO_METER);
1024
1025     // Transform the user position to the horizontal local coordinate system.
1026     SGQuatd hlTrans = SGQuatd::fromLonLat(userpos);
1027
1028     // and postrotate the orientation of the user model wrt the horizontal
1029     // local frame
1030     hlTrans *= SGQuatd::fromYawPitchRollDeg(
1031         manager->get_user_heading(),
1032         manager->get_user_pitch(),
1033         manager->get_user_roll());
1034
1035     // The offset converted to the usual body fixed coordinate system
1036     // rotated to the earth-fixed coordinates axis
1037     SGVec3d off = hlTrans.backTransform(_off);
1038
1039     // Add the position offset of the user model to get the geocentered position
1040     SGVec3d offsetPos = cartuserPos + off;
1041
1042     return offsetPos;
1043 }
1044
1045 void FGAIBallistic::setOffsetPos(SGGeod inpos, double heading, double pitch, double roll){
1046     // convert the hitch geocentered position to geodetic
1047
1048     SGVec3d cartoffsetPos = getCartOffsetPos(inpos, heading, pitch, roll);
1049
1050     //SGVec3d cartoffsetPos = getCartHitchPos();
1051
1052     //SGGeodesy::SGCartToGeod(cartoffsetPos, hitchpos);
1053     SGGeodesy::SGCartToGeod(cartoffsetPos, _offsetpos);
1054
1055 }
1056
1057 double FGAIBallistic::getDistanceLoadToHitch() const {
1058     //calculate the distance load to hitch 
1059     SGVec3d carthitchPos = getCartHitchPos();
1060     SGVec3d cartPos = getCartPos();
1061
1062     SGVec3d diff = carthitchPos - cartPos;
1063     double distance = norm(diff);
1064     return distance * SG_METER_TO_FEET;
1065 }
1066
1067
1068 double FGAIBallistic::getElevLoadToHitch() const {
1069     // now the angle, positive angles are upwards
1070     double distance = getDistanceLoadToHitch() * SG_FEET_TO_METER;
1071     double angle = 0;
1072     double daltM = _offsetpos.getElevationM() - pos.getElevationM();
1073
1074     if (fabs(distance) < SGLimits<float>::min()) {
1075         angle = 0;
1076     } else {
1077         double sAngle = daltM/distance;
1078         sAngle = SGMiscd::min(1, SGMiscd::max(-1, sAngle));
1079         angle = SGMiscd::rad2deg(asin(sAngle));
1080     }
1081
1082     return angle;
1083 }
1084
1085 double FGAIBallistic::getBearingLoadToHitch() const {
1086     //calculate the bearing and range of the second pos from the first
1087     double az1, az2, distance;
1088
1089     geo_inverse_wgs_84(pos, _offsetpos, &az1, &az2, &distance);
1090
1091     return az1;
1092 }
1093
1094 double FGAIBallistic::getRelBrgHitchToUser() const {
1095     //calculate the relative bearing 
1096     double az1, az2, distance;
1097
1098     geo_inverse_wgs_84(_offsetpos, userpos, &az1, &az2, &distance);
1099
1100     double rel_brg = az1 - hdg;
1101
1102     if (rel_brg > 180)
1103         rel_brg -= 360;
1104
1105     return rel_brg;
1106 }
1107
1108 double FGAIBallistic::getElevHitchToUser() const {
1109
1110     //calculate the distance from the user position
1111     SGVec3d carthitchPos = getCartHitchPos();
1112     SGVec3d cartuserPos = getCartUserPos();
1113
1114     SGVec3d diff = cartuserPos - carthitchPos;
1115
1116     double distance = norm(diff);
1117     double angle = 0;
1118
1119     double daltM = userpos.getElevationM() - _offsetpos.getElevationM();
1120
1121     // now the angle, positive angles are upwards
1122     if (fabs(distance) < SGLimits<float>::min()) {
1123         angle = 0;
1124     } else {
1125         double sAngle = daltM/distance;
1126         sAngle = SGMiscd::min(1, SGMiscd::max(-1, sAngle));
1127         angle = SGMiscd::rad2deg(asin(sAngle));
1128     }
1129
1130     return angle;
1131 }
1132
1133 void FGAIBallistic::setTgtOffsets(double dt, double coeff){
1134     double c = dt / (coeff + dt);
1135
1136     _x_offset = (_tgt_x_offset * c) + (_x_offset * (1 - c));
1137     _y_offset = (_tgt_y_offset * c) + (_y_offset * (1 - c));
1138     _z_offset = (_tgt_z_offset * c) + (_z_offset * (1 - c));
1139 }
1140
1141 void FGAIBallistic::formateToAC(double dt){
1142
1143     setTgtOffsets(dt, 25);
1144     setOffsetPos(userpos,
1145             manager->get_user_heading(),
1146             manager->get_user_pitch(), 
1147             manager->get_user_roll()
1148             );
1149
1150     // elapsed time has a random initialisation so that each 
1151     // wingman moves differently
1152     _elapsed_time += dt;
1153
1154     // we derive a sine based factor to give us smoothly 
1155     // varying error between -1 and 1
1156     double factor  = sin(SGMiscd::deg2rad(_elapsed_time * 10));
1157     double r_angle = 5 * factor;
1158     double p_angle = 2.5 * factor;
1159     double h_angle = 5 * factor;
1160     double h_feet  = 3 * factor;
1161
1162     pos.setLatitudeDeg(_offsetpos.getLatitudeDeg());
1163     pos.setLongitudeDeg(_offsetpos.getLongitudeDeg());
1164
1165     if (getHtAGL(10000)){
1166
1167         if(_ht_agl_ft <= 10) {
1168             _height = userpos.getElevationFt();
1169         } else if (_ht_agl_ft > 10 && _ht_agl_ft <= 150 ) {
1170             setHt(userpos.getElevationFt(), dt, 1.0);
1171         } else if (_ht_agl_ft > 150 && _ht_agl_ft <= 250) {
1172             setHt(_offsetpos.getElevationFt()+ h_feet, dt, 0.75);
1173         } else
1174             setHt(_offsetpos.getElevationFt()+ h_feet, dt, 0.5);
1175
1176         pos.setElevationFt(_height);
1177     }
1178
1179     // these calculations are unreliable at slow speeds
1180     if(speed >= 10) {
1181         setHdg(_azimuth + h_angle, dt, 0.9);
1182         setPch(_elevation + p_angle + _pitch_offset, dt, 0.9);
1183
1184         if (roll <= 115 && roll >= -115)
1185             setBnk(manager->get_user_roll() + r_angle + _roll_offset, dt, 0.5);
1186         else
1187             roll = manager->get_user_roll() + r_angle + _roll_offset;
1188
1189     } else {
1190         setHdg(manager->get_user_heading(), dt, 0.9);
1191         setPch(manager->get_user_pitch() + _pitch_offset, dt, 0.9);
1192         setBnk(manager->get_user_roll() + _roll_offset, dt, 0.9);
1193     }
1194
1195         setOffsetVelocity(dt, pos);
1196 }
1197 void FGAIBallistic::calcVSHS(){
1198     // calculate vertical and horizontal speed components
1199     double speed_fps = speed * SG_KT_TO_FPS;
1200
1201     if (speed == 0.0) {
1202         hs = vs = 0.0;
1203     } else {
1204         vs = sin( _elevation * SG_DEGREES_TO_RADIANS ) * speed_fps;
1205         hs = cos( _elevation * SG_DEGREES_TO_RADIANS ) * speed_fps;
1206     }
1207 }
1208
1209 void FGAIBallistic::calcNE(){
1210     //resolve horizontal speed into north and east components:
1211     _speed_north_fps = cos(_azimuth / SG_RADIANS_TO_DEGREES) * hs;
1212     _speed_east_fps = sin(_azimuth / SG_RADIANS_TO_DEGREES) * hs;
1213
1214     // convert horizontal speed (fps) to degrees per second
1215     speed_north_deg_sec = _speed_north_fps / ft_per_deg_lat;
1216     speed_east_deg_sec  = _speed_east_fps / ft_per_deg_lon;
1217
1218 }
1219
1220 SGVec3d FGAIBallistic::getCartOffsetPos(SGGeod inpos, double user_heading, 
1221                                         double user_pitch, double user_roll
1222                                         ) const{
1223
1224     // convert geodetic positions to geocentered
1225      SGVec3d cartuserPos = SGVec3d::fromGeod(inpos);
1226     //SGVec3d cartuserPos = getCartUserPos();
1227     //SGVec3d cartPos = getCartPos();
1228
1229     // Transform to the right coordinate frame, configuration is done in
1230     // the x-forward, y-right, z-up coordinates (feet), computation
1231     // in the simulation usual body x-forward, y-right, z-down coordinates
1232     // (meters) )
1233     SGVec3d _off(_x_offset * SG_FEET_TO_METER,
1234             _y_offset * SG_FEET_TO_METER,
1235             -_z_offset * SG_FEET_TO_METER);
1236
1237     // Transform the user position to the horizontal local coordinate system.
1238     SGQuatd hlTrans = SGQuatd::fromLonLat(userpos);
1239
1240     // and postrotate the orientation of the user model wrt the horizontal
1241     // local frame
1242     hlTrans *= SGQuatd::fromYawPitchRollDeg(
1243         user_heading,
1244         user_pitch,
1245         user_roll);
1246
1247     // The offset converted to the usual body fixed coordinate system
1248     // rotated to the earth-fixed coordinates axis
1249     SGVec3d off = hlTrans.backTransform(_off);
1250
1251     // Add the position offset of the user model to get the geocentered position
1252     SGVec3d offsetPos = cartuserPos + off;
1253
1254     return offsetPos;
1255 }
1256
1257 void FGAIBallistic::setOffsetVelocity(double dt, SGGeod offsetpos) {
1258     //calculate the distance from the previous offset position
1259     SGVec3d cartoffsetPos = SGVec3d::fromGeod(offsetpos);
1260     SGVec3d diff = cartoffsetPos - _oldcartoffsetPos;
1261
1262     double distance = norm(diff);
1263     //calculate speed knots
1264     speed = (distance/dt) * SG_MPS_TO_KT;
1265
1266     //now calulate the angle between the old and current postion positions (degrees)
1267     double angle = 0;
1268     double daltM = offsetpos.getElevationM() - _oldoffsetpos.getElevationM();
1269
1270     if (fabs(distance) < SGLimits<float>::min()) {
1271         angle = 0;
1272     } else {
1273         double sAngle = daltM/distance;
1274         sAngle = SGMiscd::min(1, SGMiscd::max(-1, sAngle));
1275         angle = SGMiscd::rad2deg(asin(sAngle));
1276     }
1277
1278     _elevation = angle;
1279
1280     //calculate vertical and horizontal speed components
1281     calcVSHS();
1282
1283     //calculate the bearing of the new offset position from the old
1284     double az1, az2, dist;
1285     geo_inverse_wgs_84(_oldoffsetpos, offsetpos, &az1, &az2, &dist);
1286     _azimuth = az1;
1287
1288     //resolve horizontal speed into north and east components:
1289     calcNE();
1290
1291     // and finally store the new values
1292     _oldcartoffsetPos = cartoffsetPos;
1293     _oldoffsetpos = offsetpos;
1294 }
1295
1296 // end AIBallistic