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)),
76 FGAIBallistic::~FGAIBallistic() {
79 void FGAIBallistic::readFromScenario(SGPropertyNode* scFileNode) {
84 FGAIBase::readFromScenario(scFileNode);
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"));
126 bool FGAIBallistic::init(bool search_in_AI_path) {
127 FGAIBase::init(search_in_AI_path);
129 _impact_reported = false;
130 _collision_reported = false;
131 _expiry_reported = false;
143 _elapsed_time += (sg_random() * 100);
145 props->setStringValue("material/name", "");
146 props->setStringValue("name", _name.c_str());
147 props->setStringValue("submodels/path", _path.c_str());
150 props->setStringValue("force/path", _force_path.c_str());
151 props->setStringValue("contents/path", _contents_path.c_str());
154 //props->setStringValue("vector/path", _vector_path.c_str());
156 // start with high value so that animations don't trigger yet
164 //cout << _name << " speed init: " << speed << endl;
169 void FGAIBallistic::bind() {
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));
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));
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));
237 void FGAIBallistic::unbind() {
238 // FGAIBase::unbind();
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");
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");
270 void FGAIBallistic::update(double dt) {
271 FGAIBase::update(dt);
277 } else if (_slave_to_ac){
280 } else if (!invisible){
287 void FGAIBallistic::setAzimuth(double az) {
290 hdg = _azimuth = (az - 5 ) + (10 * sg_random());
294 //cout << _name << " init hdg " << hdg << " random " << _random << endl;
297 void FGAIBallistic::setElevation(double el) {
298 pitch = _elevation = el;
301 void FGAIBallistic::setRoll(double rl) {
302 roll = _rotation = rl;
305 void FGAIBallistic::setStabilisation(bool val) {
306 _aero_stabilised = val;
309 void FGAIBallistic::setForceStabilisation(bool val) {
310 _force_stabilised = val;
313 void FGAIBallistic::setNoRoll(bool nr) {
317 void FGAIBallistic::setDragArea(double a) {
321 void FGAIBallistic::setLife(double seconds) {
324 life = seconds * _randomness + (seconds * (1 -_randomness) * sg_random());
325 //cout << "life " << life << endl;
330 void FGAIBallistic::setBuoyancy(double fpss) {
334 void FGAIBallistic::setWind_from_east(double fps) {
335 _wind_from_east = fps;
338 void FGAIBallistic::setWind_from_north(double fps) {
339 _wind_from_north = fps;
342 void FGAIBallistic::setWind(bool val) {
346 void FGAIBallistic::setCd(double c) {
350 void FGAIBallistic::setMass(double m) {
354 void FGAIBallistic::setWeight(double w) {
358 void FGAIBallistic::setRandomness(double r) {
362 void FGAIBallistic::setRandom(bool r) {
366 void FGAIBallistic::setImpact(bool i) {
370 void FGAIBallistic::setCollision(bool c) {
371 _report_collision = c;
374 void FGAIBallistic::setExpiry(bool e) {
378 void FGAIBallistic::setExternalForce(bool f) {
382 void FGAIBallistic::setImpactReportNode(const string& path) {
385 _impact_report_node = fgGetNode(path.c_str(), true);
388 void FGAIBallistic::setName(const string& n) {
392 void FGAIBallistic::setSMPath(const string& s) {
394 //cout << "submodel path " << _path << endl;
397 void FGAIBallistic::setFuseRange(double f) {
401 void FGAIBallistic::setSubID(int i) {
405 void FGAIBallistic::setSubmodel(const string& s) {
409 void FGAIBallistic::setGroundOffset(double g) {
413 void FGAIBallistic::setLoadOffset(double l) {
417 double FGAIBallistic::getLoadOffset() const {
421 void FGAIBallistic::setSlaved(bool s) {
425 void FGAIBallistic::setFormate(bool f) {
429 void FGAIBallistic::setContentsPath(const string& path) {
431 _contents_path = path;
434 _contents_node = fgGetNode(path.c_str(), true);
438 void FGAIBallistic::setContentsNode(SGPropertyNode_ptr node) {
441 _contents_node = node;
442 _contents_path = _contents_node->getDisplayName();
446 void FGAIBallistic::setParentNode(SGPropertyNode_ptr 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);
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);
459 _p_vel_node = _pnode->getChild("velocities", 0, true);
460 _p_spd_node = _p_vel_node->getChild("true-airspeed-kt", 0, true);
465 void FGAIBallistic::setParentPos() {
467 double lat = _p_lat_node->getDoubleValue();
468 double lon = _p_lon_node->getDoubleValue();
469 double alt = _p_alt_node->getDoubleValue();
471 _parentpos.setLongitudeDeg(lon);
472 _parentpos.setLatitudeDeg(lat);
473 _parentpos.setElevationFt(alt);
478 bool FGAIBallistic::getSlaved() const {
482 double FGAIBallistic::getMass() const {
486 double FGAIBallistic::getContents() {
488 _contents_lb = _contents_node->getChild("level-lbs",0,1)->getDoubleValue();
493 void FGAIBallistic::setContents(double c) {
495 _contents_lb = _contents_node->getChild("level-gal_us",0,1)->setDoubleValue(c);
498 void FGAIBallistic::setSlavedLoad(bool l) {
499 _slave_load_to_ac = l;
502 bool FGAIBallistic::getSlavedLoad() const {
503 return _slave_load_to_ac;
506 void FGAIBallistic::setForcePath(const string& p) {
508 if (!_force_path.empty()) {
509 SGPropertyNode *fnode = fgGetNode(_force_path.c_str(), 0, true );
510 _force_node = fnode->getChild("force-lb", 0, true);
511 _force_azimuth_node = fnode->getChild("force-azimuth-deg", 0, true);
512 _force_elevation_node = fnode->getChild("force-elevation-deg", 0, true);
516 bool FGAIBallistic::getHtAGL(double start){
518 if (getGroundElevationM(SGGeod::fromGeodM(pos, start),
519 _elevation_m, &_material)) {
520 _ht_agl_ft = pos.getElevationFt() - _elevation_m * SG_METER_TO_FEET;
523 const vector<string>& names = _material->get_names();
524 _solid = _material->get_solid();
525 _load_resistance = _material->get_load_resistance();
526 _frictionFactor =_material->get_friction_factor();
529 props->setStringValue("material/name", names[0].c_str());
531 props->setStringValue("material/name", "");
533 _mat_name = names[0];
535 //cout << "material " << _mat_name
536 //<< " solid " << _solid
537 //<< " load " << _load_resistance
538 //<< " frictionFactor " << _frictionFactor
550 double FGAIBallistic::getRecip(double az){
551 // calculate the reciprocal of the input azimuth
559 void FGAIBallistic::setPch(double e, double dt, double coeff){
560 double c = dt / (coeff + dt);
561 pitch = (e * c) + (pitch * (1 - c));
564 void FGAIBallistic::setBnk(double r, double dt, double coeff){
565 double c = dt / (coeff + dt);
566 roll = (r * c) + (roll * (1 - c));
569 void FGAIBallistic::setHt(double h, double dt, double coeff){
570 double c = dt / (coeff + dt);
571 _height = (h * c) + (_height * (1 - c));
574 void FGAIBallistic::setHdg(double az, double dt, double coeff){
575 double recip = getRecip(hdg);
576 double c = dt / (coeff + dt);
577 //we need to ensure that we turn the short way to the new hdg
578 if (az < recip && az < hdg && hdg > 180) {
579 hdg = ((az + 360) * c) + (hdg * (1 - c));
580 } else if (az > recip && az > hdg && hdg <= 180){
581 hdg = ((az - 360) * c) + (hdg * (1 - c));
583 hdg = (az * c) + (hdg * (1 - c));
587 double FGAIBallistic::getTgtXOffset() const {
588 return _tgt_x_offset;
591 double FGAIBallistic::getTgtYOffset() const {
592 return _tgt_y_offset;
595 double FGAIBallistic::getTgtZOffset() const {
596 return _tgt_z_offset;
599 void FGAIBallistic::setTgtXOffset(double x){
603 void FGAIBallistic::setTgtYOffset(double y){
607 void FGAIBallistic::setTgtZOffset(double z){
611 void FGAIBallistic::slaveToAC(double dt){
613 double hdg, pch, rll = 0;
617 hdg = _p_hdg_node->getDoubleValue();
618 pch = _p_pch_node->getDoubleValue();
619 rll = _p_rll_node->getDoubleValue();
620 setOffsetPos(_parentpos, hdg, pch, rll);
621 setSpeed(_p_spd_node->getDoubleValue());
623 hdg = manager->get_user_heading();
624 pch = manager->get_user_pitch();
625 rll = manager->get_user_roll();
626 setOffsetPos(userpos, hdg, pch, rll);
627 setSpeed(manager->get_user_speed());
630 pos.setLatitudeDeg(_offsetpos.getLatitudeDeg());
631 pos.setLongitudeDeg(_offsetpos.getLongitudeDeg());
632 pos.setElevationFt(_offsetpos.getElevationFt());
634 setPitch(pch + _pitch_offset);
635 setBank(rll + _roll_offset);
636 setOffsetVelocity(dt, pos);
638 //update the mass (slugs)
639 _mass = (_weight_lb + getContents()) / slugs_to_lbs;
641 _impact_reported = false;
643 //cout << _name << " _mass "<<_mass <<" " << getContents()
644 //<< " " << getContents() / slugs_to_lbs << " weight " << _weight_lb << endl;
645 // cout << _name << " update hs " << hs << " vs " << vs << endl;
648 void FGAIBallistic::Run(double dt) {
651 // if life = -1 the object does not die
652 if (_life_timer > life && life != -1){
654 if (_report_expiry && !_expiry_reported && !_impact_reported && !_collision_reported){
655 //cout<<"AIBallistic: expiry"<< endl;
662 //set the contents in the appropriate tank or other property in the parent to zero
665 //randomise Cd by +- 10%
667 _Cd = _Cd * 0.90 + (0.10 * sg_random());
669 // Adjust Cd by Mach number. The equations are based on curves
670 // for a conventional shell/bullet (no boat-tail).
674 Cdm = 0.0125 * Mach + _Cd;
675 else if (Mach < 1.2 )
676 Cdm = 0.3742 * pow(Mach, 2) - 0.252 * Mach + 0.0021 + _Cd;
678 Cdm = 0.2965 * pow(Mach, -1.1506) + _Cd;
680 //cout <<_name << " Mach " << Mach << " Cdm " << Cdm
681 // << " ballistic speed kts "<< speed << endl;
683 // drag = Cd * 0.5 * rho * speed * speed * drag_area;
684 // rho is adjusted for altitude in void FGAIBase::update,
685 // using Standard Atmosphere (sealevel temperature 15C)
686 // acceleration = drag/mass;
687 // adjust speed by drag
688 speed -= (Cdm * 0.5 * rho * speed * speed * _drag_area/_mass) * dt;
690 // don't let speed become negative
694 double speed_fps = speed * SG_KT_TO_FPS;
697 // calculate vertical and horizontal speed components
701 vs = sin( _elevation * SG_DEGREES_TO_RADIANS ) * speed_fps;
702 hs = cos( _elevation * SG_DEGREES_TO_RADIANS ) * speed_fps;
705 //resolve horizontal speed into north and east components:
706 double speed_north_fps = cos(_azimuth / SG_RADIANS_TO_DEGREES) * hs;
707 double speed_east_fps = sin(_azimuth / SG_RADIANS_TO_DEGREES) * hs;
709 // convert horizontal speed (fps) to degrees per second
710 double speed_north_deg_sec = speed_north_fps / ft_per_deg_lat;
711 double speed_east_deg_sec = speed_east_fps / ft_per_deg_lon;
713 // if wind not required, set to zero
715 _wind_from_north = 0;
718 _wind_from_north = manager->get_wind_from_north();
719 _wind_from_east = manager->get_wind_from_east();
722 //calculate velocity due to external force
723 double force_speed_north_deg_sec = 0;
724 double force_speed_east_deg_sec = 0;
725 // double vs_force_fps = 0;
726 double hs_force_fps = 0;
727 double v_force_acc_fpss = 0;
728 double force_speed_north_fps = 0;
729 double force_speed_east_fps = 0;
730 double h_force_lbs = 0;
731 double normal_force_lbs = 0;
732 double normal_force_fpss = 0;
733 double static_friction_force_lbs = 0;
734 double dynamic_friction_force_lbs = 0;
735 double friction_force_speed_north_fps = 0;
736 double friction_force_speed_east_fps = 0;
737 double friction_force_speed_north_deg_sec = 0;
738 double friction_force_speed_east_deg_sec = 0;
739 double force_elevation_deg = 0;
741 if (_external_force) {
742 //cout << _name << " external force" << endl;
744 SGPropertyNode *n = fgGetNode(_force_path.c_str(), true);
745 double force_lbs = n->getChild("force-lb", 0, true)->getDoubleValue();
746 force_elevation_deg = n->getChild("force-elevation-deg", 0, true)->getDoubleValue();
747 double force_azimuth_deg = n->getChild("force-azimuth-deg", 0, true)->getDoubleValue();
749 //resolve force into vertical and horizontal components:
750 double v_force_lbs = force_lbs * sin( force_elevation_deg * SG_DEGREES_TO_RADIANS );
751 h_force_lbs = force_lbs * cos( force_elevation_deg * SG_DEGREES_TO_RADIANS );
755 if (getHtAGL(10000)){
756 double deadzone = 0.1;
758 if (_ht_agl_ft <= (0 + _ground_offset + deadzone) && _solid){
759 normal_force_lbs = (_mass * slugs_to_lbs) - v_force_lbs;
761 if ( normal_force_lbs < 0 )
762 normal_force_lbs = 0;
764 pos.setElevationFt(0 + _ground_offset);
768 // calculate friction
769 // we assume a static Coefficient of Friction (mu) of 0.62 (wood on concrete)
772 static_friction_force_lbs = mu * normal_force_lbs * _frictionFactor;
774 //adjust horizontal force. We assume that a speed of <= 5 fps is static
775 if (h_force_lbs <= static_friction_force_lbs && hs <= 5){
776 h_force_lbs = hs = 0;
777 speed_north_fps = speed_east_fps = 0;
779 dynamic_friction_force_lbs = (static_friction_force_lbs * 0.95);
781 //ignore wind when on the ground for now
783 _wind_from_north = 0;
790 //acceleration = (force(lbsf)/mass(slugs))
791 v_force_acc_fpss = v_force_lbs/_mass;
792 normal_force_fpss = normal_force_lbs/_mass;
793 double h_force_acc_fpss = h_force_lbs/_mass;
794 double dynamic_friction_acc_fpss = dynamic_friction_force_lbs/_mass;
796 // velocity = acceleration * dt
797 hs_force_fps = h_force_acc_fpss * dt;
798 double friction_force_fps = dynamic_friction_acc_fpss * dt;
800 //resolve horizontal speeds into north and east components:
801 force_speed_north_fps = cos(force_azimuth_deg * SG_DEGREES_TO_RADIANS) * hs_force_fps;
802 force_speed_east_fps = sin(force_azimuth_deg * SG_DEGREES_TO_RADIANS) * hs_force_fps;
804 friction_force_speed_north_fps = cos(getRecip(hdg) * SG_DEGREES_TO_RADIANS) * friction_force_fps;
805 friction_force_speed_east_fps = sin(getRecip(hdg) * SG_DEGREES_TO_RADIANS) * friction_force_fps;
807 // convert horizontal speed (fps) to degrees per second
808 force_speed_north_deg_sec = force_speed_north_fps / ft_per_deg_lat;
809 force_speed_east_deg_sec = force_speed_east_fps / ft_per_deg_lon;
811 friction_force_speed_north_deg_sec = friction_force_speed_north_fps / ft_per_deg_lat;
812 friction_force_speed_east_deg_sec = friction_force_speed_east_fps / ft_per_deg_lon;
815 // convert wind speed (fps) to degrees lat/lon per second
816 double wind_speed_from_north_deg_sec = _wind_from_north / ft_per_deg_lat;
817 double wind_speed_from_east_deg_sec = _wind_from_east / ft_per_deg_lon;
819 //recombine the horizontal velocity components
820 hs = sqrt(((speed_north_fps + force_speed_north_fps + friction_force_speed_north_fps)
821 * (speed_north_fps + force_speed_north_fps + friction_force_speed_north_fps))
822 + ((speed_east_fps + force_speed_east_fps + friction_force_speed_east_fps)
823 * (speed_east_fps + force_speed_east_fps + friction_force_speed_east_fps)));
828 // adjust vertical speed for acceleration of gravity, buoyancy, and vertical force
829 vs -= (_gravity - _buoyancy - v_force_acc_fpss - normal_force_fpss) * dt;
831 if (vs <= 0.00001 && vs >= -0.00001)
835 if(_slave_load_to_ac) {
837 manager->get_user_heading(),
838 manager->get_user_pitch(),
839 manager->get_user_roll()
841 pos.setLatitudeDeg(_offsetpos.getLatitudeDeg());
842 pos.setLongitudeDeg(_offsetpos.getLongitudeDeg());
843 pos.setElevationFt(_offsetpos.getElevationFt());
845 if (getHtAGL(10000)){
846 double deadzone = 0.1;
848 if (_ht_agl_ft <= (0 + _ground_offset + deadzone) && _solid){
849 pos.setElevationFt(0 + _ground_offset);
851 pos.setElevationFt(_offsetpos.getElevationFt() + _load_offset);
856 pos.setLatitudeDeg( pos.getLatitudeDeg()
857 + (speed_north_deg_sec - wind_speed_from_north_deg_sec
858 + force_speed_north_deg_sec + friction_force_speed_north_deg_sec) * dt );
859 pos.setLongitudeDeg( pos.getLongitudeDeg()
860 + (speed_east_deg_sec - wind_speed_from_east_deg_sec
861 + force_speed_east_deg_sec + friction_force_speed_east_deg_sec) * dt );
862 pos.setElevationFt(pos.getElevationFt() + vs * dt);
865 // cout << _name << " run hs " << hs << " vs " << vs << endl;
867 // recalculate total speed
868 if ( vs == 0 && hs == 0)
871 speed = sqrt( vs * vs + hs * hs) / SG_KT_TO_FPS;
873 // recalculate elevation and azimuth (velocity vectors)
874 _elevation = atan2( vs, hs ) * SG_RADIANS_TO_DEGREES;
875 _azimuth = atan2((speed_east_fps + force_speed_east_fps + friction_force_speed_east_fps),
876 (speed_north_fps + force_speed_north_fps + friction_force_speed_north_fps))
877 * SG_RADIANS_TO_DEGREES;
879 // rationalise azimuth
883 if (_aero_stabilised) { // we simulate rotational moment of inertia by using a filter
884 //cout<< "_aero_stabilised "<< endl;
885 const double coeff = 0.9;
887 // we assume a symetrical MI about the pitch and yaw axis
888 setPch(_elevation, dt, coeff);
889 setHdg(_azimuth, dt, coeff);
890 } else if (_force_stabilised) { // we simulate rotational moment of inertia by using a filter
891 //cout<< "_force_stabilised "<< endl;
893 const double coeff = 0.9;
894 double ratio = h_force_lbs/(_mass * slugs_to_lbs);
896 if (ratio > 1) ratio = 1;
897 if (ratio < -1) ratio = -1;
899 double force_pitch = acos(ratio) * SG_RADIANS_TO_DEGREES;
901 if (force_pitch <= force_elevation_deg)
902 force_pitch = force_elevation_deg;
904 // we assume a symetrical MI about the pitch and yaw axis
905 setPch(force_pitch,dt, coeff);
906 setHdg(_azimuth, dt, coeff);
909 //do impacts and collisions
910 if (_report_impact && !_impact_reported)
913 if (_report_collision && !_collision_reported)
916 // set destruction flag if altitude less than sea level -1000
917 if (altitude_ft < -1000.0 && life != -1)
922 double FGAIBallistic::_getTime() const {
926 void FGAIBallistic::handle_impact() {
928 // try terrain intersection
929 double start = pos.getElevationM() + 10;
934 if (_ht_agl_ft <= 0) {
935 SG_LOG(SG_GENERAL, SG_DEBUG, "AIBallistic: terrain impact");
936 report_impact(_elevation_m);
937 _impact_reported = true;
941 } else if (_subID == 0) // kill the AIObject if there is no subsubmodel
946 void FGAIBallistic::handle_expiry() {
948 SG_LOG(SG_GENERAL, SG_DEBUG, "AIBallistic: handle_expiry " << pos.getElevationM());
950 report_impact(pos.getElevationM());
951 _expiry_reported = true;
955 } else if (_subID == 0){ // kill the AIObject if there is no subsubmodel
961 void FGAIBallistic::handle_collision()
963 const FGAIBase *object = manager->calcCollision(pos.getElevationFt(),
964 pos.getLatitudeDeg(),pos.getLongitudeDeg(), _fuse_range);
967 report_impact(pos.getElevationM(), object);
968 _collision_reported = true;
972 void FGAIBallistic::report_impact(double elevation, const FGAIBase *object)
974 _impact_lat = pos.getLatitudeDeg();
975 _impact_lon = pos.getLongitudeDeg();
976 _impact_elev = elevation;
977 _impact_speed = speed * SG_KT_TO_MPS;
979 _impact_pitch = pitch;
982 SGPropertyNode *n = props->getNode("impact", true);
985 n->setStringValue("type", object->getTypeString());
987 n->setStringValue("type", "terrain");
989 SG_LOG(SG_GENERAL, SG_DEBUG, "AIBallistic: object impact" << _name << " lon " <<_impact_lon);
991 n->setDoubleValue("longitude-deg", _impact_lon);
992 n->setDoubleValue("latitude-deg", _impact_lat);
993 n->setDoubleValue("elevation-m", _impact_elev);
994 n->setDoubleValue("heading-deg", _impact_hdg);
995 n->setDoubleValue("pitch-deg", _impact_pitch);
996 n->setDoubleValue("roll-deg", _impact_roll);
997 n->setDoubleValue("speed-mps", _impact_speed);
999 _impact_report_node->setStringValue(props->getPath());
1002 SGVec3d FGAIBallistic::getCartUserPos() const {
1003 SGVec3d cartUserPos = SGVec3d::fromGeod(userpos);
1007 SGVec3d FGAIBallistic::getCartHitchPos() const{
1009 // convert geodetic positions to geocentered
1010 SGVec3d cartuserPos = SGVec3d::fromGeod(userpos);
1011 //SGVec3d cartPos = getCartPos();
1013 // Transform to the right coordinate frame, configuration is done in
1014 // the x-forward, y-right, z-up coordinates (feet), computation
1015 // in the simulation usual body x-forward, y-right, z-down coordinates
1017 SGVec3d _off(_x_offset * SG_FEET_TO_METER,
1018 _y_offset * SG_FEET_TO_METER,
1019 -_z_offset * SG_FEET_TO_METER);
1021 // Transform the user position to the horizontal local coordinate system.
1022 SGQuatd hlTrans = SGQuatd::fromLonLat(userpos);
1024 // and postrotate the orientation of the user model wrt the horizontal
1026 hlTrans *= SGQuatd::fromYawPitchRollDeg(
1027 manager->get_user_heading(),
1028 manager->get_user_pitch(),
1029 manager->get_user_roll());
1031 // The offset converted to the usual body fixed coordinate system
1032 // rotated to the earth-fixed coordinates axis
1033 SGVec3d off = hlTrans.backTransform(_off);
1035 // Add the position offset of the user model to get the geocentered position
1036 SGVec3d offsetPos = cartuserPos + off;
1041 void FGAIBallistic::setOffsetPos(SGGeod inpos, double heading, double pitch, double roll){
1042 // convert the hitch geocentered position to geodetic
1044 SGVec3d cartoffsetPos = getCartOffsetPos(inpos, heading, pitch, roll);
1046 //SGVec3d cartoffsetPos = getCartHitchPos();
1048 //SGGeodesy::SGCartToGeod(cartoffsetPos, hitchpos);
1049 SGGeodesy::SGCartToGeod(cartoffsetPos, _offsetpos);
1053 double FGAIBallistic::getDistanceLoadToHitch() const {
1054 //calculate the distance load to hitch
1055 SGVec3d carthitchPos = getCartHitchPos();
1056 SGVec3d cartPos = getCartPos();
1058 SGVec3d diff = carthitchPos - cartPos;
1059 double distance = norm(diff);
1060 return distance * SG_METER_TO_FEET;
1064 double FGAIBallistic::getElevLoadToHitch() const {
1065 // now the angle, positive angles are upwards
1066 double distance = getDistanceLoadToHitch() * SG_FEET_TO_METER;
1068 double daltM = _offsetpos.getElevationM() - pos.getElevationM();
1070 if (fabs(distance) < SGLimits<float>::min()) {
1073 double sAngle = daltM/distance;
1074 sAngle = SGMiscd::min(1, SGMiscd::max(-1, sAngle));
1075 angle = SGMiscd::rad2deg(asin(sAngle));
1081 double FGAIBallistic::getBearingLoadToHitch() const {
1082 //calculate the bearing and range of the second pos from the first
1083 double az1, az2, distance;
1085 geo_inverse_wgs_84(pos, _offsetpos, &az1, &az2, &distance);
1090 double FGAIBallistic::getRelBrgHitchToUser() const {
1091 //calculate the relative bearing
1092 double az1, az2, distance;
1094 geo_inverse_wgs_84(_offsetpos, userpos, &az1, &az2, &distance);
1096 double rel_brg = az1 - hdg;
1104 double FGAIBallistic::getElevHitchToUser() const {
1106 //calculate the distance from the user position
1107 SGVec3d carthitchPos = getCartHitchPos();
1108 SGVec3d cartuserPos = getCartUserPos();
1110 SGVec3d diff = cartuserPos - carthitchPos;
1112 double distance = norm(diff);
1115 double daltM = userpos.getElevationM() - _offsetpos.getElevationM();
1117 // now the angle, positive angles are upwards
1118 if (fabs(distance) < SGLimits<float>::min()) {
1121 double sAngle = daltM/distance;
1122 sAngle = SGMiscd::min(1, SGMiscd::max(-1, sAngle));
1123 angle = SGMiscd::rad2deg(asin(sAngle));
1129 void FGAIBallistic::setTgtOffsets(double dt, double coeff){
1130 double c = dt / (coeff + dt);
1132 _x_offset = (_tgt_x_offset * c) + (_x_offset * (1 - c));
1133 _y_offset = (_tgt_y_offset * c) + (_y_offset * (1 - c));
1134 _z_offset = (_tgt_z_offset * c) + (_z_offset * (1 - c));
1137 void FGAIBallistic::formateToAC(double dt){
1139 setTgtOffsets(dt, 25);
1140 setOffsetPos(userpos,
1141 manager->get_user_heading(),
1142 manager->get_user_pitch(),
1143 manager->get_user_roll()
1146 // elapsed time has a random initialisation so that each
1147 // wingman moves differently
1148 _elapsed_time += dt;
1150 // we derive a sine based factor to give us smoothly
1151 // varying error between -1 and 1
1152 double factor = sin(SGMiscd::deg2rad(_elapsed_time * 10));
1153 double r_angle = 5 * factor;
1154 double p_angle = 2.5 * factor;
1155 double h_angle = 5 * factor;
1156 double h_feet = 3 * factor;
1158 pos.setLatitudeDeg(_offsetpos.getLatitudeDeg());
1159 pos.setLongitudeDeg(_offsetpos.getLongitudeDeg());
1161 if (getHtAGL(10000)){
1163 if(_ht_agl_ft <= 10) {
1164 _height = userpos.getElevationFt();
1165 } else if (_ht_agl_ft > 10 && _ht_agl_ft <= 150 ) {
1166 setHt(userpos.getElevationFt(), dt, 1.0);
1167 } else if (_ht_agl_ft > 150 && _ht_agl_ft <= 250) {
1168 setHt(_offsetpos.getElevationFt()+ h_feet, dt, 0.75);
1170 setHt(_offsetpos.getElevationFt()+ h_feet, dt, 0.5);
1172 pos.setElevationFt(_height);
1175 // these calculations are unreliable at slow speeds
1177 setHdg(_azimuth + h_angle, dt, 0.9);
1178 setPch(_elevation + p_angle + _pitch_offset, dt, 0.9);
1180 if (roll <= 115 && roll >= -115)
1181 setBnk(manager->get_user_roll() + r_angle + _roll_offset, dt, 0.5);
1183 roll = manager->get_user_roll() + r_angle + _roll_offset;
1186 setHdg(manager->get_user_heading(), dt, 0.9);
1187 setPch(manager->get_user_pitch() + _pitch_offset, dt, 0.9);
1188 setBnk(manager->get_user_roll() + _roll_offset, dt, 0.9);
1191 setOffsetVelocity(dt, pos);
1193 void FGAIBallistic::calcVSHS(){
1194 // calculate vertical and horizontal speed components
1195 double speed_fps = speed * SG_KT_TO_FPS;
1200 vs = sin( _elevation * SG_DEGREES_TO_RADIANS ) * speed_fps;
1201 hs = cos( _elevation * SG_DEGREES_TO_RADIANS ) * speed_fps;
1205 void FGAIBallistic::calcNE(){
1206 //resolve horizontal speed into north and east components:
1207 _speed_north_fps = cos(_azimuth / SG_RADIANS_TO_DEGREES) * hs;
1208 _speed_east_fps = sin(_azimuth / SG_RADIANS_TO_DEGREES) * hs;
1210 // convert horizontal speed (fps) to degrees per second
1211 speed_north_deg_sec = _speed_north_fps / ft_per_deg_lat;
1212 speed_east_deg_sec = _speed_east_fps / ft_per_deg_lon;
1216 SGVec3d FGAIBallistic::getCartOffsetPos(SGGeod inpos, double user_heading,
1217 double user_pitch, double user_roll
1220 // convert geodetic positions to geocentered
1221 SGVec3d cartuserPos = SGVec3d::fromGeod(inpos);
1222 //SGVec3d cartuserPos = getCartUserPos();
1223 //SGVec3d cartPos = getCartPos();
1225 // Transform to the right coordinate frame, configuration is done in
1226 // the x-forward, y-right, z-up coordinates (feet), computation
1227 // in the simulation usual body x-forward, y-right, z-down coordinates
1229 SGVec3d _off(_x_offset * SG_FEET_TO_METER,
1230 _y_offset * SG_FEET_TO_METER,
1231 -_z_offset * SG_FEET_TO_METER);
1233 // Transform the user position to the horizontal local coordinate system.
1234 SGQuatd hlTrans = SGQuatd::fromLonLat(userpos);
1236 // and postrotate the orientation of the user model wrt the horizontal
1238 hlTrans *= SGQuatd::fromYawPitchRollDeg(
1243 // The offset converted to the usual body fixed coordinate system
1244 // rotated to the earth-fixed coordinates axis
1245 SGVec3d off = hlTrans.backTransform(_off);
1247 // Add the position offset of the user model to get the geocentered position
1248 SGVec3d offsetPos = cartuserPos + off;
1253 void FGAIBallistic::setOffsetVelocity(double dt, SGGeod offsetpos) {
1254 //calculate the distance from the previous offset position
1255 SGVec3d cartoffsetPos = SGVec3d::fromGeod(offsetpos);
1256 SGVec3d diff = cartoffsetPos - _oldcartoffsetPos;
1258 double distance = norm(diff);
1259 //calculate speed knots
1260 speed = (distance/dt) * SG_MPS_TO_KT;
1262 //now calulate the angle between the old and current postion positions (degrees)
1264 double daltM = offsetpos.getElevationM() - _oldoffsetpos.getElevationM();
1266 if (fabs(distance) < SGLimits<float>::min()) {
1269 double sAngle = daltM/distance;
1270 sAngle = SGMiscd::min(1, SGMiscd::max(-1, sAngle));
1271 angle = SGMiscd::rad2deg(asin(sAngle));
1276 //calculate vertical and horizontal speed components
1279 //calculate the bearing of the new offset position from the old
1280 double az1, az2, dist;
1281 geo_inverse_wgs_84(_oldoffsetpos, offsetpos, &az1, &az2, &dist);
1284 //resolve horizontal speed into north and east components:
1287 // and finally store the new values
1288 _oldcartoffsetPos = cartoffsetPos;
1289 _oldoffsetpos = offsetpos;