1 // FGAIBallistic - FGAIBase-derived class creates a ballistic object
3 // Written by David Culp, started November 2003.
4 // - davidculp2@comcast.net
6 // With major additions by Mathias Froehlich & Vivian Meazza 2004-2008
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.
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.
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.
26 #include <simgear/math/sg_random.h>
27 #include <simgear/math/sg_geodesy.hxx>
28 #include <simgear/scene/model/modellib.hxx>
30 #include <Scenery/scenery.hxx>
32 #include "AIBallistic.hxx"
34 #include <Main/util.hxx>
36 using namespace simgear;
38 const double FGAIBallistic::slugs_to_kgs = 14.5939029372;
39 const double FGAIBallistic::slugs_to_lbs = 32.1740485564;
41 FGAIBallistic::FGAIBallistic(object_type ot) :
48 _formate_to_ac(false),
49 _aero_stabilised(false),
52 _gravity(32.1740485564),
59 _force_stabilised(false),
61 _slave_load_to_ac(false),
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)),
75 FGAIBallistic::~FGAIBallistic() {
78 void FGAIBallistic::readFromScenario(SGPropertyNode* scFileNode) {
83 FGAIBase::readFromScenario(scFileNode);
85 //setPath(scFileNode->getStringValue("model", "Models/Geometry/rocket.ac"));
86 setRandom(scFileNode->getBoolValue("random", false));
87 setAzimuth(scFileNode->getDoubleValue("azimuth", 0.0));
88 setElevation(scFileNode->getDoubleValue("elevation", 0));
89 setDragArea(scFileNode->getDoubleValue("eda", 0.007));
90 setLife(scFileNode->getDoubleValue("life", 900.0));
91 setBuoyancy(scFileNode->getDoubleValue("buoyancy", 0));
92 setWind_from_east(scFileNode->getDoubleValue("wind_from_east", 0));
93 setWind_from_north(scFileNode->getDoubleValue("wind_from_north", 0));
94 setWind(scFileNode->getBoolValue("wind", false));
95 setRoll(scFileNode->getDoubleValue("roll", 0.0));
96 setCd(scFileNode->getDoubleValue("cd", 0.029));
97 //setMass(scFileNode->getDoubleValue("mass", 0.007));
98 setWeight(scFileNode->getDoubleValue("weight", 0.25));
99 setStabilisation(scFileNode->getBoolValue("aero-stabilised", false));
100 setNoRoll(scFileNode->getBoolValue("no-roll", false));
101 setImpact(scFileNode->getBoolValue("impact", false));
102 setExpiry(scFileNode->getBoolValue("expiry", false));
103 setCollision(scFileNode->getBoolValue("collision", false));
104 setImpactReportNode(scFileNode->getStringValue("impact-reports"));
105 setName(scFileNode->getStringValue("name", "Rocket"));
106 setFuseRange(scFileNode->getDoubleValue("fuse-range", 0.0));
107 setSMPath(scFileNode->getStringValue("submodel-path", ""));
108 setSubID(scFileNode->getIntValue("SubID", 0));
109 setExternalForce(scFileNode->getBoolValue("external-force", false));
110 setForcePath(scFileNode->getStringValue("force-path", ""));
111 setForceStabilisation(scFileNode->getBoolValue("force-stabilised", false));
112 setXoffset(scFileNode->getDoubleValue("x-offset", 0.0));
113 setYoffset(scFileNode->getDoubleValue("y-offset", 0.0));
114 setZoffset(scFileNode->getDoubleValue("z-offset", 0.0));
115 setPitchoffset(scFileNode->getDoubleValue("pitch-offset", 0.0));
116 setRolloffset(scFileNode->getDoubleValue("roll-offset", 0.0));
117 setYawoffset(scFileNode->getDoubleValue("yaw-offset", 0.0));
118 setGroundOffset(scFileNode->getDoubleValue("ground-offset", 0.0));
119 setLoadOffset(scFileNode->getDoubleValue("load-offset", 0.0));
120 setSlaved(scFileNode->getBoolValue("slaved", false));
121 setSlavedLoad(scFileNode->getBoolValue("slaved-load", false));
122 setContentsNode(scFileNode->getStringValue("contents"));
125 bool FGAIBallistic::init(bool search_in_AI_path) {
126 FGAIBase::init(search_in_AI_path);
128 _impact_reported = false;
129 _collision_reported = false;
130 _expiry_reported = false;
142 _elapsed_time += (sg_random() * 100);
144 props->setStringValue("material/name", "");
145 props->setStringValue("name", _name.c_str());
146 props->setStringValue("submodels/path", _submodel.c_str());
147 props->setStringValue("force/path", _force_path.c_str());
148 //props->setStringValue("vector/path", _vector_path.c_str());
150 // start with high value so that animations don't trigger yet
161 void FGAIBallistic::bind() {
164 props->tie("sim/time/elapsed-sec",
165 SGRawValueMethods<FGAIBallistic,double>(*this,
166 &FGAIBallistic::_getTime));
167 props->tie("mass-slug",
168 SGRawValueMethods<FGAIBallistic,double>(*this,
169 &FGAIBallistic::getMass));
170 props->tie("material/load-resistance",
171 SGRawValuePointer<double>(&_load_resistance));
172 props->tie("material/solid",
173 SGRawValuePointer<bool>(&_solid));
174 props->tie("altitude-agl-ft",
175 SGRawValuePointer<double>(&_ht_agl_ft));
176 props->tie("controls/slave-to-ac",
177 SGRawValueMethods<FGAIBallistic,bool>
178 (*this, &FGAIBallistic::getSlaved, &FGAIBallistic::setSlaved));
179 props->tie("controls/invisible",
180 SGRawValuePointer<bool>(&invisible));
183 props->tie("controls/force_stabilized",
184 SGRawValuePointer<bool>(&_force_stabilised));
185 props->tie("position/global-x",
186 SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getCartPosX, 0));
187 props->tie("position/global-y",
188 SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getCartPosY, 0));
189 props->tie("position/global-z",
190 SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getCartPosZ, 0));
191 props->tie("velocities/vertical-speed-fps",
192 SGRawValuePointer<double>(&vs));
193 props->tie("velocities/true-airspeed-kt",
194 SGRawValuePointer<double>(&speed));
195 props->tie("velocities/horizontal-speed-fps",
196 SGRawValuePointer<double>(&hs));
197 props->tie("position/altitude-ft",
198 SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getAltitude, &FGAIBase::_setAltitude));
199 props->tie("position/latitude-deg",
200 SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getLatitude, &FGAIBase::_setLatitude));
201 props->tie("position/longitude-deg",
202 SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getLongitude, &FGAIBase::_setLongitude));
203 props->tie("orientation/hdg-deg",
204 SGRawValuePointer<double>(&hdg));
205 props->tie("orientation/pitch-deg",
206 SGRawValuePointer<double>(&pitch));
207 props->tie("orientation/roll-deg",
208 SGRawValuePointer<double>(&roll));
209 props->tie("controls/slave-load-to-ac",
210 SGRawValueMethods<FGAIBallistic,bool>
211 (*this, &FGAIBallistic::getSlavedLoad, &FGAIBallistic::setSlavedLoad));
212 props->tie("position/load-offset",
213 SGRawValueMethods<FGAIBallistic,double>
214 (*this, &FGAIBallistic::getLoadOffset, &FGAIBallistic::setLoadOffset));
215 props->tie("load/distance-to-hitch-ft",
216 SGRawValueMethods<FGAIBallistic,double>
217 (*this, &FGAIBallistic::getDistanceLoadToHitch));
218 props->tie("load/elevation-to-hitch-deg",
219 SGRawValueMethods<FGAIBallistic,double>
220 (*this, &FGAIBallistic::getElevLoadToHitch));
221 props->tie("load/bearing-to-hitch-deg",
222 SGRawValueMethods<FGAIBallistic,double>
223 (*this, &FGAIBallistic::getBearingLoadToHitch));
228 void FGAIBallistic::unbind() {
229 // FGAIBase::unbind();
231 props->untie("sim/time/elapsed-sec");
232 props->untie("mass-slug");
233 props->untie("material/load-resistance");
234 props->untie("material/solid");
235 props->untie("altitude-agl-ft");
236 props->untie("controls/slave-to-ac");
237 props->untie("controls/invisible");
240 props->untie("position/global-y");
241 props->untie("position/global-x");
242 props->untie("position/global-z");
243 props->untie("velocities/vertical-speed-fps");
244 props->untie("velocities/true-airspeed-kt");
245 props->untie("velocities/horizontal-speed-fps");
246 props->untie("position/altitude-ft");
247 props->untie("position/latitude-deg");
248 props->untie("position/longitude-deg");
249 props->untie("position/ht-agl-ft");
250 props->untie("orientation/hdg-deg");
251 props->untie("orientation/pitch-deg");
252 props->untie("orientation/roll-deg");
253 props->untie("controls/force_stabilized");
254 props->untie("position/load-offset");
255 props->untie("load/distance-to-hitch-ft");
256 props->untie("load/elevation-to-hitch-deg");
257 props->untie("load/bearing-to-hitch-deg");
261 void FGAIBallistic::update(double dt) {
262 FGAIBase::update(dt);
268 setHitchVelocity(dt);
269 } else if (_formate_to_ac){
272 setHitchVelocity(dt);
273 } else if (!invisible){
280 void FGAIBallistic::setAzimuth(double az) {
284 hdg = _azimuth = (az - 5 ) + (10 * sg_random());
288 //cout << _name << " init hdg " << hdg << " random " << _random << endl;
291 void FGAIBallistic::setElevation(double el) {
292 pitch = _elevation = el;
295 void FGAIBallistic::setRoll(double rl) {
296 roll = _rotation = rl;
299 void FGAIBallistic::setStabilisation(bool val) {
300 _aero_stabilised = val;
303 void FGAIBallistic::setForceStabilisation(bool val) {
304 _force_stabilised = val;
307 void FGAIBallistic::setNoRoll(bool nr) {
311 void FGAIBallistic::setDragArea(double a) {
315 void FGAIBallistic::setLife(double seconds) {
318 life = seconds * _randomness + (seconds * (1 -_randomness) * sg_random());
319 //cout << "life " << life << endl;
324 void FGAIBallistic::setBuoyancy(double fpss) {
328 void FGAIBallistic::setWind_from_east(double fps) {
329 _wind_from_east = fps;
332 void FGAIBallistic::setWind_from_north(double fps) {
333 _wind_from_north = fps;
336 void FGAIBallistic::setWind(bool val) {
340 void FGAIBallistic::setCd(double c) {
344 void FGAIBallistic::setMass(double m) {
348 void FGAIBallistic::setWeight(double w) {
352 void FGAIBallistic::setRandomness(double r) {
356 void FGAIBallistic::setRandom(bool r) {
360 void FGAIBallistic::setImpact(bool i) {
364 void FGAIBallistic::setCollision(bool c) {
365 _report_collision = c;
368 void FGAIBallistic::setExpiry(bool e) {
370 //cout << "_report_expiry " << _report_expiry << endl;
373 void FGAIBallistic::setExternalForce(bool f) {
377 void FGAIBallistic::setImpactReportNode(const string& path) {
380 _impact_report_node = fgGetNode(path.c_str(), true);
383 void FGAIBallistic::setName(const string& n) {
387 void FGAIBallistic::setSMPath(const string& s) {
391 void FGAIBallistic::setFuseRange(double f) {
395 void FGAIBallistic::setSubID(int i) {
399 void FGAIBallistic::setSubmodel(const string& s) {
403 void FGAIBallistic::setGroundOffset(double g) {
407 void FGAIBallistic::setLoadOffset(double l) {
411 double FGAIBallistic::getLoadOffset() const {
415 void FGAIBallistic::setSlaved(bool s) {
419 void FGAIBallistic::setFormate(bool f) {
423 void FGAIBallistic::setContentsNode(const string& path) {
425 _contents_node = fgGetNode(path.c_str(), true);
429 bool FGAIBallistic::getSlaved() const {
433 double FGAIBallistic::getMass() const {
437 double FGAIBallistic::getContents() {
439 _contents_lb = _contents_node->getChild("level-lbs",0,1)->getDoubleValue();
443 void FGAIBallistic::setContents(double c) {
445 _contents_lb = _contents_node->getChild("level-gal_us",0,1)->setDoubleValue(c);
448 void FGAIBallistic::setSlavedLoad(bool l) {
449 _slave_load_to_ac = l;
452 bool FGAIBallistic::getSlavedLoad() const {
453 return _slave_load_to_ac;
456 void FGAIBallistic::setForcePath(const string& p) {
458 if (!_force_path.empty()) {
459 SGPropertyNode *fnode = fgGetNode(_force_path.c_str(), 0, true );
460 _force_node = fnode->getChild("force-lb", 0, true);
461 _force_azimuth_node = fnode->getChild("force-azimuth-deg", 0, true);
462 _force_elevation_node = fnode->getChild("force-elevation-deg", 0, true);
466 bool FGAIBallistic::getHtAGL(){
468 if (getGroundElevationM(SGGeod::fromGeodM(pos, 10000),
469 _elevation_m, &_material)) {
470 _ht_agl_ft = pos.getElevationFt() - _elevation_m * SG_METER_TO_FEET;
472 const vector<string>& names = _material->get_names();
474 _solid = _material->get_solid();
475 _load_resistance = _material->get_load_resistance();
476 _frictionFactor =_material->get_friction_factor();
478 props->setStringValue("material/name", names[0].c_str());
480 props->setStringValue("material/name", "");
481 /*cout << "material " << mat_name
482 << " solid " << _solid
483 << " load " << _load_resistance
484 << " frictionFactor " << frictionFactor
494 double FGAIBallistic::getRecip(double az){
495 // calculate the reciprocal of the input azimuth
503 void FGAIBallistic::setPch(double e, double dt, double coeff){
504 double c = dt / (coeff + dt);
505 pitch = (e * c) + (pitch * (1 - c));
508 void FGAIBallistic::setBnk(double r, double dt, double coeff){
509 double c = dt / (coeff + dt);
510 roll = (r * c) + (roll * (1 - c));
513 void FGAIBallistic::setHt(double h, double dt, double coeff){
514 double c = dt / (coeff + dt);
515 _height = (h * c) + (_height * (1 - c));
518 void FGAIBallistic::setHdg(double az, double dt, double coeff){
519 double recip = getRecip(hdg);
520 double c = dt / (coeff + dt);
521 //we need to ensure that we turn the short way to the new hdg
522 if (az < recip && az < hdg && hdg > 180) {
523 hdg = ((az + 360) * c) + (hdg * (1 - c));
524 } else if (az > recip && az > hdg && hdg <= 180){
525 hdg = ((az - 360) * c) + (hdg * (1 - c));
527 hdg = (az * c) + (hdg * (1 - c));
531 double FGAIBallistic::getTgtXOffset() const {
532 return _tgt_x_offset;
535 double FGAIBallistic::getTgtYOffset() const {
536 return _tgt_y_offset;
539 double FGAIBallistic::getTgtZOffset() const {
540 return _tgt_z_offset;
543 void FGAIBallistic::setTgtXOffset(double x){
547 void FGAIBallistic::setTgtYOffset(double y){
551 void FGAIBallistic::setTgtZOffset(double z){
555 void FGAIBallistic::slaveToAC(double dt){
558 pos.setLatitudeDeg(hitchpos.getLatitudeDeg());
559 pos.setLongitudeDeg(hitchpos.getLongitudeDeg());
560 pos.setElevationFt(hitchpos.getElevationFt());
561 setHeading(manager->get_user_heading());
562 setPitch(manager->get_user_pitch() + _pitch_offset);
563 setBank(manager->get_user_roll() + _roll_offset);
564 setSpeed(manager->get_user_speed());
565 //update the mass (slugs)
566 _mass = (_weight_lb + getContents()) / slugs_to_lbs;
568 /*cout <<"_mass "<<_mass <<" " << getContents()
569 <<" " << getContents() / slugs_to_lbs << endl;*/
572 void FGAIBallistic::Run(double dt) {
575 // if life = -1 the object does not die
576 if (_life_timer > life && life != -1){
578 if (_report_expiry && !_expiry_reported){
579 //cout<<"AIBallistic: expiry"<< endl;
586 //set the contents in the appropriate tank or other property in the parent to zero
589 //randomise Cd by +- 10%
591 _Cd = _Cd * 0.90 + (0.10 * sg_random());
593 // Adjust Cd by Mach number. The equations are based on curves
594 // for a conventional shell/bullet (no boat-tail).
598 Cdm = 0.0125 * Mach + _Cd;
599 else if (Mach < 1.2 )
600 Cdm = 0.3742 * pow(Mach, 2) - 0.252 * Mach + 0.0021 + _Cd;
602 Cdm = 0.2965 * pow(Mach, -1.1506) + _Cd;
604 //cout << "Mach " << Mach << " Cdm " << Cdm << "// ballistic speed kts "<< speed << endl;
606 // drag = Cd * 0.5 * rho * speed * speed * drag_area;
607 // rho is adjusted for altitude in void FGAIBase::update,
608 // using Standard Atmosphere (sealevel temperature 15C)
609 // acceleration = drag/mass;
610 // adjust speed by drag
611 speed -= (Cdm * 0.5 * rho * speed * speed * _drag_area/_mass) * dt;
613 // don't let speed become negative
617 double speed_fps = speed * SG_KT_TO_FPS;
620 // calculate vertical and horizontal speed components
624 vs = sin( _elevation * SG_DEGREES_TO_RADIANS ) * speed_fps;
625 hs = cos( _elevation * SG_DEGREES_TO_RADIANS ) * speed_fps;
628 //resolve horizontal speed into north and east components:
629 double speed_north_fps = cos(_azimuth / SG_RADIANS_TO_DEGREES) * hs;
630 double speed_east_fps = sin(_azimuth / SG_RADIANS_TO_DEGREES) * hs;
632 // convert horizontal speed (fps) to degrees per second
633 double speed_north_deg_sec = speed_north_fps / ft_per_deg_lat;
634 double speed_east_deg_sec = speed_east_fps / ft_per_deg_lon;
636 // if wind not required, set to zero
638 _wind_from_north = 0;
641 _wind_from_north = manager->get_wind_from_north();
642 _wind_from_east = manager->get_wind_from_east();
645 //calculate velocity due to external force
646 double force_speed_north_deg_sec = 0;
647 double force_speed_east_deg_sec = 0;
648 // double vs_force_fps = 0;
649 double hs_force_fps = 0;
650 double v_force_acc_fpss = 0;
651 double force_speed_north_fps = 0;
652 double force_speed_east_fps = 0;
653 double h_force_lbs = 0;
654 double normal_force_lbs = 0;
655 double normal_force_fpss = 0;
656 double static_friction_force_lbs = 0;
657 double dynamic_friction_force_lbs = 0;
658 double friction_force_speed_north_fps = 0;
659 double friction_force_speed_east_fps = 0;
660 double friction_force_speed_north_deg_sec = 0;
661 double friction_force_speed_east_deg_sec = 0;
662 double force_elevation_deg = 0;
664 if (_external_force) {
666 SGPropertyNode *n = fgGetNode(_force_path.c_str(), true);
667 double force_lbs = n->getChild("force-lb", 0, true)->getDoubleValue();
668 force_elevation_deg = n->getChild("force-elevation-deg", 0, true)->getDoubleValue();
669 double force_azimuth_deg = n->getChild("force-azimuth-deg", 0, true)->getDoubleValue();
671 //resolve force into vertical and horizontal components:
672 double v_force_lbs = force_lbs * sin( force_elevation_deg * SG_DEGREES_TO_RADIANS );
673 h_force_lbs = force_lbs * cos( force_elevation_deg * SG_DEGREES_TO_RADIANS );
678 double deadzone = 0.1;
680 if (_ht_agl_ft <= (0 + _ground_offset + deadzone) && _solid){
681 normal_force_lbs = (_mass * slugs_to_lbs) - v_force_lbs;
683 if ( normal_force_lbs < 0 )
684 normal_force_lbs = 0;
686 pos.setElevationFt(0 + _ground_offset);
690 // calculate friction
691 // we assume a static Coefficient of Friction (mu) of 0.62 (wood on concrete)
694 static_friction_force_lbs = mu * normal_force_lbs * _frictionFactor;
696 //adjust horizontal force. We assume that a speed of <= 5 fps is static
697 if (h_force_lbs <= static_friction_force_lbs && hs <= 5){
698 h_force_lbs = hs = 0;
699 speed_north_fps = speed_east_fps = 0;
701 dynamic_friction_force_lbs = (static_friction_force_lbs * 0.95);
703 //ignore wind when on the ground for now
705 _wind_from_north = 0;
712 //acceleration = (force(lbsf)/mass(slugs))
713 v_force_acc_fpss = v_force_lbs/_mass;
714 normal_force_fpss = normal_force_lbs/_mass;
715 double h_force_acc_fpss = h_force_lbs/_mass;
716 double dynamic_friction_acc_fpss = dynamic_friction_force_lbs/_mass;
718 // velocity = acceleration * dt
719 hs_force_fps = h_force_acc_fpss * dt;
720 double friction_force_fps = dynamic_friction_acc_fpss * dt;
722 //resolve horizontal speeds into north and east components:
723 force_speed_north_fps = cos(force_azimuth_deg * SG_DEGREES_TO_RADIANS) * hs_force_fps;
724 force_speed_east_fps = sin(force_azimuth_deg * SG_DEGREES_TO_RADIANS) * hs_force_fps;
726 friction_force_speed_north_fps = cos(getRecip(hdg) * SG_DEGREES_TO_RADIANS) * friction_force_fps;
727 friction_force_speed_east_fps = sin(getRecip(hdg) * SG_DEGREES_TO_RADIANS) * friction_force_fps;
729 // convert horizontal speed (fps) to degrees per second
730 force_speed_north_deg_sec = force_speed_north_fps / ft_per_deg_lat;
731 force_speed_east_deg_sec = force_speed_east_fps / ft_per_deg_lon;
733 friction_force_speed_north_deg_sec = friction_force_speed_north_fps / ft_per_deg_lat;
734 friction_force_speed_east_deg_sec = friction_force_speed_east_fps / ft_per_deg_lon;
737 // convert wind speed (fps) to degrees lat/lon per second
738 double wind_speed_from_north_deg_sec = _wind_from_north / ft_per_deg_lat;
739 double wind_speed_from_east_deg_sec = _wind_from_east / ft_per_deg_lon;
741 //recombine the horizontal velocity components
742 hs = sqrt(((speed_north_fps + force_speed_north_fps + friction_force_speed_north_fps)
743 * (speed_north_fps + force_speed_north_fps + friction_force_speed_north_fps))
744 + ((speed_east_fps + force_speed_east_fps + friction_force_speed_east_fps)
745 * (speed_east_fps + force_speed_east_fps + friction_force_speed_east_fps)));
750 // adjust vertical speed for acceleration of gravity, buoyancy, and vertical force
751 vs -= (_gravity - _buoyancy - v_force_acc_fpss - normal_force_fpss) * dt;
753 if (vs <= 0.00001 && vs >= -0.00001)
757 if(_slave_load_to_ac) {
759 pos.setLatitudeDeg(hitchpos.getLatitudeDeg());
760 pos.setLongitudeDeg(hitchpos.getLongitudeDeg());
761 pos.setElevationFt(hitchpos.getElevationFt());
764 double deadzone = 0.1;
766 if (_ht_agl_ft <= (0 + _ground_offset + deadzone) && _solid){
767 pos.setElevationFt(0 + _ground_offset);
769 pos.setElevationFt(hitchpos.getElevationFt() + _load_offset);
774 pos.setLatitudeDeg( pos.getLatitudeDeg()
775 + (speed_north_deg_sec - wind_speed_from_north_deg_sec
776 + force_speed_north_deg_sec + friction_force_speed_north_deg_sec) * dt );
777 pos.setLongitudeDeg( pos.getLongitudeDeg()
778 + (speed_east_deg_sec - wind_speed_from_east_deg_sec
779 + force_speed_east_deg_sec + friction_force_speed_east_deg_sec) * dt );
780 pos.setElevationFt(pos.getElevationFt() + vs * dt);
783 // recalculate total speed
784 if ( vs == 0 && hs == 0)
787 speed = sqrt( vs * vs + hs * hs) / SG_KT_TO_FPS;
789 // recalculate elevation and azimuth (velocity vectors)
790 _elevation = atan2( vs, hs ) * SG_RADIANS_TO_DEGREES;
791 _azimuth = atan2((speed_east_fps + force_speed_east_fps + friction_force_speed_east_fps),
792 (speed_north_fps + force_speed_north_fps + friction_force_speed_north_fps))
793 * SG_RADIANS_TO_DEGREES;
795 // rationalise azimuth
799 //cout << "_azimuth " << _azimuth << " hdg "<< hdg << endl;
801 if (_aero_stabilised) { // we simulate rotational moment of inertia by using a filter
802 cout<< "_aero_stabilised "<< endl;
803 const double coeff = 0.9;
805 // we assume a symetrical MI about the pitch and yaw axis
806 setPch(_elevation, dt, coeff);
807 setHdg(_azimuth, dt, coeff);
808 } else if (_force_stabilised) { // we simulate rotational moment of inertia by using a filter
809 //cout<< "_force_stabilised "<< endl;
811 const double coeff = 0.9;
812 double ratio = h_force_lbs/(_mass * slugs_to_lbs);
814 if (ratio > 1) ratio = 1;
815 if (ratio < -1) ratio = -1;
817 double force_pitch = acos(ratio) * SG_RADIANS_TO_DEGREES;
819 if (force_pitch <= force_elevation_deg)
820 force_pitch = force_elevation_deg;
822 // we assume a symetrical MI about the pitch and yaw axis
823 setPch(force_pitch,dt, coeff);
824 setHdg(_azimuth, dt, coeff);
827 //do impacts and collisions
828 if (_report_impact && !_impact_reported)
831 if (_report_collision && !_collision_reported)
834 // set destruction flag if altitude less than sea level -1000
835 if (altitude_ft < -1000.0 && life != -1)
840 double FGAIBallistic::_getTime() const {
844 void FGAIBallistic::handle_impact() {
846 // try terrain intersection
850 if (_ht_agl_ft <= 0) {
851 SG_LOG(SG_GENERAL, SG_DEBUG, "AIBallistic: terrain impact");
852 report_impact(_elevation_m);
853 _impact_reported = true;
857 } else if (_subID == 0) // kill the AIObject if there is no subsubmodel
862 void FGAIBallistic::handle_expiry() {
864 SG_LOG(SG_GENERAL, SG_DEBUG, "AIBallistic: handle_expiry " << pos.getElevationM());
866 report_impact(pos.getElevationM());
867 _expiry_reported = true;
871 //} else if (_subID == 0) // kill the AIObject if there is no subsubmodel
876 void FGAIBallistic::handle_collision()
878 const FGAIBase *object = manager->calcCollision(pos.getElevationFt(),
879 pos.getLatitudeDeg(),pos.getLongitudeDeg(), _fuse_range);
882 SG_LOG(SG_GENERAL, SG_DEBUG, "AIBallistic: object hit");
883 report_impact(pos.getElevationM(), object);
884 _collision_reported = true;
888 void FGAIBallistic::report_impact(double elevation, const FGAIBase *object)
890 _impact_lat = pos.getLatitudeDeg();
891 _impact_lon = pos.getLongitudeDeg();
892 _impact_elev = elevation;
893 _impact_speed = speed * SG_KT_TO_MPS;
895 _impact_pitch = pitch;
898 SGPropertyNode *n = props->getNode("impact", true);
900 n->setStringValue("type", object->getTypeString());
902 n->setStringValue("type", "terrain");
904 n->setDoubleValue("longitude-deg", _impact_lon);
905 n->setDoubleValue("latitude-deg", _impact_lat);
906 n->setDoubleValue("elevation-m", _impact_elev);
907 n->setDoubleValue("heading-deg", _impact_hdg);
908 n->setDoubleValue("pitch-deg", _impact_pitch);
909 n->setDoubleValue("roll-deg", _impact_roll);
910 n->setDoubleValue("speed-mps", _impact_speed);
912 _impact_report_node->setStringValue(props->getPath());
915 SGVec3d FGAIBallistic::getCartUserPos() const {
916 SGVec3d cartUserPos = SGVec3d::fromGeod(userpos);
920 SGVec3d FGAIBallistic::getCartHitchPos() const{
922 // convert geodetic positions to geocentered
923 SGVec3d cartuserPos = getCartUserPos();
924 //SGVec3d cartPos = getCartPos();
926 // Transform to the right coordinate frame, configuration is done in
927 // the x-forward, y-right, z-up coordinates (feet), computation
928 // in the simulation usual body x-forward, y-right, z-down coordinates
930 SGVec3d _off(_x_offset * SG_FEET_TO_METER,
931 _y_offset * SG_FEET_TO_METER,
932 -_z_offset * SG_FEET_TO_METER);
934 // Transform the user position to the horizontal local coordinate system.
935 SGQuatd hlTrans = SGQuatd::fromLonLat(userpos);
937 // and postrotate the orientation of the user model wrt the horizontal
939 hlTrans *= SGQuatd::fromYawPitchRollDeg(
940 manager->get_user_heading(),
941 manager->get_user_pitch(),
942 manager->get_user_roll());
944 // The offset converted to the usual body fixed coordinate system
945 // rotated to the earth-fixed coordinates axis
946 SGVec3d off = hlTrans.backTransform(_off);
948 // Add the position offset of the user model to get the geocentered position
949 SGVec3d offsetPos = cartuserPos + off;
954 void FGAIBallistic::setHitchPos(){
955 // convert the hitch geocentered position to geodetic
956 SGVec3d carthitchPos = getCartHitchPos();
958 SGGeodesy::SGCartToGeod(carthitchPos, hitchpos);
961 double FGAIBallistic::getDistanceLoadToHitch() const {
962 //calculate the distance load to hitch
963 SGVec3d carthitchPos = getCartHitchPos();
964 SGVec3d cartPos = getCartPos();
966 SGVec3d diff = carthitchPos - cartPos;
967 double distance = norm(diff);
968 return distance * SG_METER_TO_FEET;
971 void FGAIBallistic::setHitchVelocity(double dt) {
972 //calculate the distance from the previous hitch position
973 SGVec3d carthitchPos = getCartHitchPos();
974 SGVec3d diff = carthitchPos - _oldcarthitchPos;
976 double distance = norm(diff);
978 //calculate speed knots
979 speed = (distance/dt) * SG_MPS_TO_KT;
981 //now calulate the angle between the old and current hitch positions (degrees)
983 double daltM = hitchpos.getElevationM() - oldhitchpos.getElevationM();
985 if (fabs(distance) < SGLimits<float>::min()) {
988 double sAngle = daltM/distance;
989 sAngle = SGMiscd::min(1, SGMiscd::max(-1, sAngle));
990 angle = SGMiscd::rad2deg(asin(sAngle));
995 //calculate the bearing of the new hitch position from the old
996 double az1, az2, dist;
998 geo_inverse_wgs_84(oldhitchpos, hitchpos, &az1, &az2, &dist);
1002 // and finally store the new values
1003 _oldcarthitchPos = carthitchPos;
1004 oldhitchpos = hitchpos;
1007 double FGAIBallistic::getElevLoadToHitch() const {
1008 // now the angle, positive angles are upwards
1009 double distance = getDistanceLoadToHitch() * SG_FEET_TO_METER;
1011 double daltM = hitchpos.getElevationM() - pos.getElevationM();
1013 if (fabs(distance) < SGLimits<float>::min()) {
1016 double sAngle = daltM/distance;
1017 sAngle = SGMiscd::min(1, SGMiscd::max(-1, sAngle));
1018 angle = SGMiscd::rad2deg(asin(sAngle));
1024 double FGAIBallistic::getBearingLoadToHitch() const {
1025 //calculate the bearing and range of the second pos from the first
1026 double az1, az2, distance;
1028 geo_inverse_wgs_84(pos, hitchpos, &az1, &az2, &distance);
1033 double FGAIBallistic::getRelBrgHitchToUser() const {
1034 //calculate the relative bearing
1035 double az1, az2, distance;
1037 geo_inverse_wgs_84(hitchpos, userpos, &az1, &az2, &distance);
1039 double rel_brg = az1 - hdg;
1047 double FGAIBallistic::getElevHitchToUser() const {
1049 //calculate the distance from the user position
1050 SGVec3d carthitchPos = getCartHitchPos();
1051 SGVec3d cartuserPos = getCartUserPos();
1053 SGVec3d diff = cartuserPos - carthitchPos;
1055 double distance = norm(diff);
1058 double daltM = userpos.getElevationM() - hitchpos.getElevationM();
1060 // now the angle, positive angles are upwards
1061 if (fabs(distance) < SGLimits<float>::min()) {
1064 double sAngle = daltM/distance;
1065 sAngle = SGMiscd::min(1, SGMiscd::max(-1, sAngle));
1066 angle = SGMiscd::rad2deg(asin(sAngle));
1072 void FGAIBallistic::setTgtOffsets(double dt, double coeff){
1073 double c = dt / (coeff + dt);
1075 _x_offset = (_tgt_x_offset * c) + (_x_offset * (1 - c));
1076 _y_offset = (_tgt_y_offset * c) + (_y_offset * (1 - c));
1077 _z_offset = (_tgt_z_offset * c) + (_z_offset * (1 - c));
1080 void FGAIBallistic::formateToAC(double dt){
1082 setTgtOffsets(dt, 25);
1084 setHitchVelocity(dt);
1086 // elapsed time has a random initialisation so that each
1087 // wingman moves differently
1088 _elapsed_time += dt;
1090 // we derive a sine based factor to give us smoothly
1091 // varying error between -1 and 1
1092 double factor = sin(SGMiscd::deg2rad(_elapsed_time * 10));
1093 double r_angle = 5 * factor;
1094 double p_angle = 2.5 * factor;
1095 double h_angle = 5 * factor;
1096 double h_feet = 3 * factor;
1098 pos.setLatitudeDeg(hitchpos.getLatitudeDeg());
1099 pos.setLongitudeDeg(hitchpos.getLongitudeDeg());
1103 if(_ht_agl_ft <= 10) {
1104 _height = userpos.getElevationFt();
1105 } else if (_ht_agl_ft > 10 && _ht_agl_ft <= 150 ) {
1106 setHt(userpos.getElevationFt(), dt, 1.0);
1107 } else if (_ht_agl_ft > 150 && _ht_agl_ft <= 250) {
1108 setHt(hitchpos.getElevationFt()+ h_feet, dt, 0.75);
1110 setHt(hitchpos.getElevationFt()+ h_feet, dt, 0.5);
1112 pos.setElevationFt(_height);
1115 // these calculations are unreliable at slow speeds
1117 setHdg(_azimuth + h_angle, dt, 0.9);
1118 setPch(_elevation + p_angle + _pitch_offset, dt, 0.9);
1120 if (roll <= 115 && roll >= -115)
1121 setBnk(manager->get_user_roll() + r_angle + _roll_offset, dt, 0.5);
1123 roll = manager->get_user_roll() + r_angle + _roll_offset;
1126 setHdg(manager->get_user_heading(), dt, 0.9);
1127 setPch(manager->get_user_pitch() + _pitch_offset, dt, 0.9);
1128 setBnk(manager->get_user_roll() + _roll_offset, dt, 0.9);