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