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>
35 #include <Environment/gravity.hxx>
37 using namespace simgear;
39 const double FGAIBallistic::slugs_to_kgs = 14.5939029372;
40 const double FGAIBallistic::slugs_to_lbs = 32.1740485564;
42 FGAIBallistic::FGAIBallistic(object_type ot) :
52 _aero_stabilised(false),
61 _force_stabilised(false),
63 _slave_load_to_ac(false),
65 _report_collision(false),
66 _report_impact(false),
67 _external_force(false),
68 _report_expiry(false),
69 _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"));
124 setParentName(scFileNode->getStringValue("parent"));
127 bool FGAIBallistic::init(bool search_in_AI_path) {
128 FGAIBase::init(search_in_AI_path);
133 void FGAIBallistic::reinit() {
134 _impact_reported = false;
135 _collision_reported = false;
136 _expiry_reported = false;
148 _elapsed_time += (sg_random() * 100);
152 props->setStringValue("material/name", "");
153 props->setStringValue("name", _name.c_str());
154 props->setStringValue("submodels/path", _path.c_str());
157 props->setStringValue("force/path", _force_path.c_str());
158 props->setStringValue("contents/path", _contents_path.c_str());
161 //cout << "init: name " << _name.c_str() << " _life_timer " << _life_timer
168 //setParentNodes(_selected_ac);
170 //props->setStringValue("vector/path", _vector_path.c_str());
172 // start with high value so that animations don't trigger yet
184 setParentNodes(_selected_ac);
189 void FGAIBallistic::bind() {
192 _tiedProperties.setRoot(props);
193 tie("sim/time/elapsed-sec",
194 SGRawValueMethods<FGAIBallistic,double>(*this,
195 &FGAIBallistic::_getTime, &FGAIBallistic::setTime));
197 // SGRawValueMethods<FGAIBallistic,double>(*this,
198 // &FGAIBallistic::getMass));
200 tie("material/solid",
201 SGRawValuePointer<bool>(&_solid));
202 tie("altitude-agl-ft",
203 SGRawValuePointer<double>(&_ht_agl_ft));
204 tie("controls/slave-to-ac",
205 SGRawValueMethods<FGAIBallistic,bool>
206 (*this, &FGAIBallistic::getSlaved, &FGAIBallistic::setSlaved));
207 tie("controls/invisible",
208 SGRawValuePointer<bool>(&invisible));
210 if(_external_force || _slave_to_ac){
211 tie("controls/force_stabilized",
212 SGRawValuePointer<bool>(&_force_stabilised));
213 tie("position/global-x",
214 SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getCartPosX, 0));
215 tie("position/global-y",
216 SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getCartPosY, 0));
217 tie("position/global-z",
218 SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getCartPosZ, 0));
219 tie("velocities/vertical-speed-fps",
220 SGRawValuePointer<double>(&vs));
221 tie("velocities/true-airspeed-kt",
222 SGRawValuePointer<double>(&speed));
223 tie("velocities/horizontal-speed-fps",
224 SGRawValuePointer<double>(&hs));
225 tie("position/altitude-ft",
226 SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getElevationFt, &FGAIBase::_setAltitude));
227 tie("position/latitude-deg",
228 SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getLatitude, &FGAIBase::_setLatitude));
229 tie("position/longitude-deg",
230 SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getLongitude, &FGAIBase::_setLongitude));
231 tie("orientation/hdg-deg",
232 SGRawValuePointer<double>(&hdg));
233 tie("orientation/pitch-deg",
234 SGRawValuePointer<double>(&pitch));
235 tie("orientation/roll-deg",
236 SGRawValuePointer<double>(&roll));
237 tie("controls/slave-load-to-ac",
238 SGRawValueMethods<FGAIBallistic,bool>
239 (*this, &FGAIBallistic::getSlavedLoad, &FGAIBallistic::setSlavedLoad));
240 tie("position/load-offset",
241 SGRawValueMethods<FGAIBallistic,double>
242 (*this, &FGAIBallistic::getLoadOffset, &FGAIBallistic::setLoadOffset));
243 tie("load/distance-to-hitch-ft",
244 SGRawValueMethods<FGAIBallistic,double>
245 (*this, &FGAIBallistic::getDistanceToHitch));
246 tie("load/elevation-to-hitch-deg",
247 SGRawValueMethods<FGAIBallistic,double>
248 (*this, &FGAIBallistic::getElevToHitch));
249 tie("load/bearing-to-hitch-deg",
250 SGRawValueMethods<FGAIBallistic,double>
251 (*this, &FGAIBallistic::getBearingToHitch));
252 tie("material/load-resistance",
253 SGRawValuePointer<double>(&_load_resistance));
258 void FGAIBallistic::update(double dt) {
259 FGAIBase::update(dt);
265 } else if (!invisible){
272 void FGAIBallistic::setAzimuth(double az) {
275 hdg = _azimuth = (az - 5 ) + (10 * sg_random());
279 //cout << _name << " init hdg " << hdg << " random " << _random << endl;
282 void FGAIBallistic::setElevation(double el) {
283 pitch = _elevation = el;
286 void FGAIBallistic::setRoll(double rl) {
287 roll = _rotation = rl;
290 void FGAIBallistic::setStabilisation(bool val) {
291 _aero_stabilised = val;
294 void FGAIBallistic::setForceStabilisation(bool val) {
295 _force_stabilised = val;
298 void FGAIBallistic::setNoRoll(bool nr) {
302 void FGAIBallistic::setDragArea(double a) {
306 void FGAIBallistic::setLife(double seconds) {
309 life = seconds * _randomness + (seconds * (1 -_randomness) * sg_random());
310 //cout << " set life " << life << endl;
315 void FGAIBallistic::setBuoyancy(double fpss) {
319 void FGAIBallistic::setWind_from_east(double fps) {
320 _wind_from_east = fps;
323 void FGAIBallistic::setWind_from_north(double fps) {
324 _wind_from_north = fps;
327 void FGAIBallistic::setWind(bool val) {
331 void FGAIBallistic::setCd(double c) {
335 void FGAIBallistic::setMass(double m) {
339 void FGAIBallistic::setWeight(double w) {
343 void FGAIBallistic::setRandomness(double r) {
347 void FGAIBallistic::setRandom(bool r) {
351 void FGAIBallistic::setImpact(bool i) {
355 void FGAIBallistic::setCollision(bool c) {
356 _report_collision = c;
359 void FGAIBallistic::setExpiry(bool e) {
363 void FGAIBallistic::setExternalForce(bool f) {
367 void FGAIBallistic::setImpactReportNode(const string& path) {
370 _impact_report_node = fgGetNode(path.c_str(), true);
373 void FGAIBallistic::setSMPath(const string& s) {
375 //cout << "submodel path " << _path << endl;
378 void FGAIBallistic::setFuseRange(double f) {
382 void FGAIBallistic::setSubID(int i) {
386 void FGAIBallistic::setSubmodel(const string& s) {
390 void FGAIBallistic::setGroundOffset(double g) {
394 void FGAIBallistic::setLoadOffset(double l) {
398 double FGAIBallistic::getLoadOffset() const {
402 void FGAIBallistic::setSlaved(bool s) {
406 void FGAIBallistic::setContentsPath(const string& path) {
408 _contents_path = path;
411 _contents_node = fgGetNode(path.c_str(), true);
415 void FGAIBallistic::setContentsNode(SGPropertyNode_ptr node) {
418 _contents_node = node;
419 _contents_path = _contents_node->getDisplayName();
423 void FGAIBallistic::setParentNodes(SGPropertyNode_ptr node) {
427 _p_pos_node = _pnode->getChild("position", 0, true);
428 _p_lat_node = _p_pos_node->getChild("latitude-deg", 0, true);
429 _p_lon_node = _p_pos_node->getChild("longitude-deg", 0, true);
430 _p_alt_node = _p_pos_node->getChild("altitude-ft", 0, true);
431 _p_agl_node = _p_pos_node->getChild("altitude-agl-ft", 0, true);
434 _p_ori_node = _pnode->getChild("orientation", 0, true);
435 _p_pch_node = _p_ori_node->getChild("pitch-deg", 0, true);
436 _p_rll_node = _p_ori_node->getChild("roll-deg", 0, true);
437 _p_hdg_node = _p_ori_node->getChild("true-heading-deg",0, true);
439 _p_vel_node = _pnode->getChild("velocities", 0, true);
440 _p_spd_node = _p_vel_node->getChild("true-airspeed-kt", 0, true);
445 void FGAIBallistic::setParentPos() {
448 //cout << "set parent pos" << endl;
450 double lat = _p_lat_node->getDoubleValue();
451 double lon = _p_lon_node->getDoubleValue();
452 double alt = _p_alt_node->getDoubleValue();
454 _parentpos.setLongitudeDeg(lon);
455 _parentpos.setLatitudeDeg(lat);
456 _parentpos.setElevationFt(alt);
462 bool FGAIBallistic::getSlaved() const {
466 double FGAIBallistic::getMass() const {
470 double FGAIBallistic::getContents() {
472 _contents_lb = _contents_node->getChild("level-lbs",0,1)->getDoubleValue();
477 void FGAIBallistic::setContents(double c) {
479 _contents_lb = _contents_node->getChild("level-gal_us",0,1)->setDoubleValue(c);
482 void FGAIBallistic::setSlavedLoad(bool l) {
483 _slave_load_to_ac = l;
486 bool FGAIBallistic::getSlavedLoad() const {
487 return _slave_load_to_ac;
490 void FGAIBallistic::setForcePath(const string& p) {
492 if (!_force_path.empty()) {
493 SGPropertyNode *fnode = fgGetNode(_force_path.c_str(), 0, true );
494 _force_node = fnode->getChild("force-lb", 0, true);
495 _force_azimuth_node = fnode->getChild("force-azimuth-deg", 0, true);
496 _force_elevation_node = fnode->getChild("force-elevation-deg", 0, true);
500 bool FGAIBallistic::getHtAGL(double start){
501 const simgear::BVHMaterial* mat = 0;
502 if (getGroundElevationM(SGGeod::fromGeodM(pos, start),
503 _elevation_m, &mat)) {
504 const SGMaterial* material = dynamic_cast<const SGMaterial*>(mat);
505 _ht_agl_ft = pos.getElevationFt() - _elevation_m * SG_METER_TO_FEET;
508 const vector<string>& names = material->get_names();
509 _solid = material->get_solid();
510 _load_resistance = material->get_load_resistance();
511 _frictionFactor = material->get_friction_factor();
514 props->setStringValue("material/name", names[0].c_str());
516 props->setStringValue("material/name", "");
518 _mat_name = names[0];
520 //cout << "material " << _mat_name
521 //<< " solid " << _solid
522 //<< " load " << _load_resistance
523 //<< " frictionFactor " << _frictionFactor
535 double FGAIBallistic::getRecip(double az){
536 // calculate the reciprocal of the input azimuth
544 void FGAIBallistic::setPch(double e, double dt, double coeff){
545 double c = dt / (coeff + dt);
546 pitch = (e * c) + (pitch * (1 - c));
549 void FGAIBallistic::setBnk(double r, double dt, double coeff){
550 double c = dt / (coeff + dt);
551 roll = (r * c) + (roll * (1 - c));
554 void FGAIBallistic::setSpd(double s, double dt, double coeff){
555 double c = dt / (coeff + dt);
556 _speed = (s * c) + (_speed * (1 - c));
559 void FGAIBallistic::setHt(double h, double dt, double coeff){
560 double c = dt / (coeff + dt);
561 _height = (h * c) + (_height * (1 - c));
564 int FGAIBallistic::setHdg(double tgt_hdg, double dt, double coeff){
565 double recip = getRecip(hdg);
566 double c = dt / (coeff + dt);
567 //cout << "set heading " << tgt_hdg << endl;
568 //we need to ensure that we turn the short way to the new hdg
569 if (tgt_hdg < recip && tgt_hdg < hdg && hdg > 180) {
570 hdg = ((tgt_hdg + 360) * c) + (hdg * (1 - c));
571 // cout << "case 1: right turn" << endl;
572 } else if (tgt_hdg > recip && tgt_hdg > hdg && hdg <= 180){
573 hdg = ((tgt_hdg - 360) * c) + (hdg * (1 - c));
574 // cout << "case 2: left turn" << endl;
576 hdg = (tgt_hdg * c) + (hdg * (1 - c));
577 // cout << "case 4: left turn" << endl;
582 double FGAIBallistic::getTgtXOffset() const {
583 return _tgt_x_offset;
586 double FGAIBallistic::getTgtYOffset() const {
587 return _tgt_y_offset;
590 double FGAIBallistic::getTgtZOffset() const {
591 return _tgt_z_offset;
594 void FGAIBallistic::setTgtXOffset(double x){
598 void FGAIBallistic::setTgtYOffset(double y){
602 void FGAIBallistic::setTgtZOffset(double z){
606 void FGAIBallistic::slaveToAC(double dt){
611 double hdg, pch, rll;//, agl = 0;
615 hdg = _p_hdg_node->getDoubleValue();
616 pch = _p_pch_node->getDoubleValue();
617 rll = _p_rll_node->getDoubleValue();
618 // agl = _p_agl_node->getDoubleValue();
619 setOffsetPos(_parentpos, hdg, pch, rll);
620 setSpeed(_p_spd_node->getDoubleValue());
622 hdg = manager->get_user_heading();
623 pch = manager->get_user_pitch();
624 rll = manager->get_user_roll();
625 // agl = manager->get_user_agl();
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);
639 //update the mass (slugs)
640 _mass = (_weight_lb + getContents()) / slugs_to_lbs;
642 _impact_reported = false;
644 //cout << _name << " _mass "<<_mass <<" " << getContents()
645 //<< " " << getContents() / slugs_to_lbs << " weight " << _weight_lb << endl;
646 // cout << _name << " update hs " << hs << " vs " << vs << endl;
649 void FGAIBallistic::Run(double dt) {
653 //cout<<"AIBallistic run: name " << _name.c_str()
654 // << " dt " << dt << " _life_timer " << _life_timer << " pass " << _pass << endl;
656 // if life = -1 the object does not die
657 if (_life_timer > life && life != -1){
659 if (_report_expiry && !_expiry_reported && !_impact_reported && !_collision_reported){
660 //cout<<"AIBallistic run: name " << _name.c_str() << " expiry "
661 //<< " _life_timer " << _life_timer<< endl;
664 //cout<<"AIBallistic run: name " << _name.c_str()
665 // << " die " << " _life_timer " << _life_timer << endl;
672 //set the contents in the appropriate tank or other property in the parent to zero
675 //randomise Cd by +- 10%
677 _Cd = _Cd * 0.90 + (0.10 * sg_random());
679 // Adjust Cd by Mach number. The equations are based on curves
680 // for a conventional shell/bullet (no boat-tail).
684 Cdm = 0.0125 * Mach + _Cd;
685 else if (Mach < 1.2 )
686 Cdm = 0.3742 * pow(Mach, 2) - 0.252 * Mach + 0.0021 + _Cd;
688 Cdm = 0.2965 * pow(Mach, -1.1506) + _Cd;
690 //cout <<_name << " Mach " << Mach << " Cdm " << Cdm
691 // << " ballistic speed kts "<< speed << endl;
693 // drag = Cd * 0.5 * rho * speed * speed * drag_area;
694 // rho is adjusted for altitude in void FGAIBase::update,
695 // using Standard Atmosphere (sealevel temperature 15C)
696 // acceleration = drag/mass;
697 // adjust speed by drag
698 speed -= (Cdm * 0.5 * rho * speed * speed * _drag_area/_mass) * dt;
700 // don't let speed become negative
704 // double speed_fps = speed * SG_KT_TO_FPS;
706 // calculate vertical and horizontal speed components
709 //resolve horizontal speed into north and east components:
710 //and convert horizontal speed (fps) to degrees per second
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;
740 double force_azimuth_deg = 0;
741 double force_lbs = 0;
743 if (_external_force) {
744 //cout << _name << " external force " << hdg << " az " << _azimuth << endl;
746 SGPropertyNode *n = fgGetNode(_force_path.c_str(), true);
747 force_lbs = n->getChild("force-lb", 0, true)->getDoubleValue();
748 force_elevation_deg = n->getChild("force-elevation-deg", 0, true)->getDoubleValue();
749 force_azimuth_deg = n->getChild("force-azimuth-deg", 0, true)->getDoubleValue();
751 //resolve force into vertical and horizontal components:
752 double v_force_lbs = force_lbs * sin( force_elevation_deg * SG_DEGREES_TO_RADIANS );
753 h_force_lbs = force_lbs * cos( force_elevation_deg * SG_DEGREES_TO_RADIANS );
756 //we don't do this if impacts are calculated
759 if (getHtAGL(10000)){
760 double deadzone = 0.1;
762 if (_ht_agl_ft <= (0 + _ground_offset + deadzone) && _solid){
763 normal_force_lbs = (_mass * slugs_to_lbs) - v_force_lbs;
765 if ( normal_force_lbs < 0 )
766 normal_force_lbs = 0;
768 pos.setElevationFt(0 + _ground_offset);
772 // calculate friction
773 // we assume a static Coefficient of Friction (mu) of 0.62 (wood on concrete)
776 static_friction_force_lbs = mu * normal_force_lbs * _frictionFactor;
778 //adjust horizontal force. We assume that a speed of <= 5 fps is static
779 if (h_force_lbs <= static_friction_force_lbs && hs <= 5){
780 h_force_lbs = hs = 0;
781 _speed_north_fps = _speed_east_fps = 0;
783 dynamic_friction_force_lbs = (static_friction_force_lbs * 0.95);
785 //ignore wind when on the ground for now
787 _wind_from_north = 0;
796 //acceleration = (force(lbsf)/mass(slugs))
797 v_force_acc_fpss = v_force_lbs/_mass;
798 normal_force_fpss = normal_force_lbs/_mass;
799 double h_force_acc_fpss = h_force_lbs/_mass;
800 double dynamic_friction_acc_fpss = dynamic_friction_force_lbs/_mass;
802 // velocity = acceleration * dt
803 hs_force_fps = h_force_acc_fpss * dt;
804 double friction_force_fps = dynamic_friction_acc_fpss * dt;
806 //resolve horizontal speeds into north and east components:
807 force_speed_north_fps = cos(force_azimuth_deg * SG_DEGREES_TO_RADIANS) * hs_force_fps;
808 force_speed_east_fps = sin(force_azimuth_deg * SG_DEGREES_TO_RADIANS) * hs_force_fps;
810 friction_force_speed_north_fps = cos(getRecip(hdg) * SG_DEGREES_TO_RADIANS) * friction_force_fps;
811 friction_force_speed_east_fps = sin(getRecip(hdg) * SG_DEGREES_TO_RADIANS) * friction_force_fps;
813 // convert horizontal speed (fps) to degrees per second
814 force_speed_north_deg_sec = force_speed_north_fps / ft_per_deg_lat;
815 force_speed_east_deg_sec = force_speed_east_fps / ft_per_deg_lon;
817 friction_force_speed_north_deg_sec = friction_force_speed_north_fps / ft_per_deg_lat;
818 friction_force_speed_east_deg_sec = friction_force_speed_east_fps / ft_per_deg_lon;
821 // convert wind speed (fps) to degrees lat/lon per second
822 double wind_speed_from_north_deg_sec = _wind_from_north / ft_per_deg_lat;
823 double wind_speed_from_east_deg_sec = _wind_from_east / ft_per_deg_lon;
825 //recombine the horizontal velocity components
826 hs = sqrt(((_speed_north_fps + force_speed_north_fps + friction_force_speed_north_fps)
827 * (_speed_north_fps + force_speed_north_fps + friction_force_speed_north_fps))
828 + ((_speed_east_fps + force_speed_east_fps + friction_force_speed_east_fps)
829 * (_speed_east_fps + force_speed_east_fps + friction_force_speed_east_fps)));
834 // adjust vertical speed for acceleration of gravity, buoyancy, and vertical force
835 double gravity = SG_METER_TO_FEET * (Environment::Gravity::instance()->getGravity(pos));
836 vs -= (gravity - _buoyancy - v_force_acc_fpss - normal_force_fpss) * dt;
838 if (vs <= 0.00001 && vs >= -0.00001)
842 if(_slave_load_to_ac) {
844 manager->get_user_heading(),
845 manager->get_user_pitch(),
846 manager->get_user_roll()
848 pos.setLatitudeDeg(_offsetpos.getLatitudeDeg());
849 pos.setLongitudeDeg(_offsetpos.getLongitudeDeg());
850 pos.setElevationFt(_offsetpos.getElevationFt());
852 if (getHtAGL(10000)){
853 double deadzone = 0.1;
855 if (_ht_agl_ft <= (0 + _ground_offset + deadzone) && _solid){
856 pos.setElevationFt(0 + _ground_offset);
858 pos.setElevationFt(_offsetpos.getElevationFt() + _load_offset);
863 pos.setLatitudeDeg( pos.getLatitudeDeg()
864 + (speed_north_deg_sec - wind_speed_from_north_deg_sec
865 + force_speed_north_deg_sec + friction_force_speed_north_deg_sec) * dt );
866 pos.setLongitudeDeg( pos.getLongitudeDeg()
867 + (speed_east_deg_sec - wind_speed_from_east_deg_sec
868 + force_speed_east_deg_sec + friction_force_speed_east_deg_sec) * dt );
869 pos.setElevationFt(pos.getElevationFt() + vs * dt);
872 // cout << _name << " run hs " << hs << " vs " << vs << endl;
874 // recalculate total speed
875 if ( vs == 0 && hs == 0)
878 speed = sqrt( vs * vs + hs * hs) / SG_KT_TO_FPS;
880 // recalculate elevation and azimuth (velocity vectors)
881 _elevation = atan2( vs, hs ) * SG_RADIANS_TO_DEGREES;
882 _azimuth = atan2((_speed_east_fps + force_speed_east_fps + friction_force_speed_east_fps),
883 (_speed_north_fps + force_speed_north_fps + friction_force_speed_north_fps))
884 * SG_RADIANS_TO_DEGREES;
886 // rationalise azimuth
890 if (_aero_stabilised) { // we simulate rotational moment of inertia by using a filter
891 //cout<< "_aero_stabilised " << hdg << " az " << _azimuth << endl;
892 const double coeff = 0.9;
894 // we assume a symetrical MI about the pitch and yaw axis
895 setPch(_elevation, dt, coeff);
896 setHdg(_azimuth, dt, coeff);
897 } else if (_force_stabilised) { // we simulate rotational moment of inertia by using a filter
898 //cout<< "_force_stabilised "<< endl;
900 const double coeff = 0.9;
901 double ratio = h_force_lbs/(_mass * slugs_to_lbs);
903 if (ratio > 1) ratio = 1;
904 if (ratio < -1) ratio = -1;
906 double force_pitch = acos(ratio) * SG_RADIANS_TO_DEGREES;
908 if (force_pitch <= force_elevation_deg)
909 force_pitch = force_elevation_deg;
911 // we assume a symetrical MI about the pitch and yaw axis
912 setPch(force_pitch,dt, coeff);
913 setHdg(_azimuth, dt, coeff);
916 //do impacts and collisions
917 if (_report_impact && !_impact_reported)
920 if (_report_collision && !_collision_reported)
923 // set destruction flag if altitude less than sea level -1000
924 if (altitude_ft < -1000.0 && life != -1)
929 double FGAIBallistic::_getTime() const {
933 void FGAIBallistic::setTime(double s){
937 void FGAIBallistic::handle_impact() {
939 // try terrain intersection
940 double start = pos.getElevationM() + 100;
945 if (_ht_agl_ft <= 0) {
946 SG_LOG(SG_AI, SG_DEBUG, "AIBallistic: terrain impact material" << _mat_name);
947 report_impact(_elevation_m);
948 _impact_reported = true;
952 } else if (_subID == 0) // kill the AIObject if there is no subsubmodel
957 void FGAIBallistic::handle_expiry() {
959 //SG_LOG(SG_AI, SG_DEBUG, "AIBallistic: handle_expiry " << pos.getElevationM());
961 report_impact(pos.getElevationM());
962 _expiry_reported = true;
966 } else if (_subID == 0){ // kill the AIObject if there is no subsubmodel
972 void FGAIBallistic::handle_collision()
974 const FGAIBase *object = manager->calcCollision(pos.getElevationFt(),
975 pos.getLatitudeDeg(),pos.getLongitudeDeg(), _fuse_range);
978 report_impact(pos.getElevationM(), object);
979 _collision_reported = true;
983 void FGAIBallistic::report_impact(double elevation, const FGAIBase *object)
985 _impact_lat = pos.getLatitudeDeg();
986 _impact_lon = pos.getLongitudeDeg();
987 _impact_elev = elevation;
988 _impact_speed = speed * SG_KT_TO_MPS;
990 _impact_pitch = pitch;
993 SGPropertyNode *n = props->getNode("impact", true);
996 n->setStringValue("type", object->getTypeString());
998 n->setStringValue("type", "terrain");
1000 SG_LOG(SG_AI, SG_DEBUG, "AIBallistic: object impact " << _name
1001 << " lon " <<_impact_lon << " lat " <<_impact_lat << " sec " << _life_timer);
1003 n->setDoubleValue("longitude-deg", _impact_lon);
1004 n->setDoubleValue("latitude-deg", _impact_lat);
1005 n->setDoubleValue("elevation-m", _impact_elev);
1006 n->setDoubleValue("heading-deg", _impact_hdg);
1007 n->setDoubleValue("pitch-deg", _impact_pitch);
1008 n->setDoubleValue("roll-deg", _impact_roll);
1009 n->setDoubleValue("speed-mps", _impact_speed);
1011 _impact_report_node->setStringValue(props->getPath());
1014 SGVec3d FGAIBallistic::getCartUserPos() const {
1015 SGVec3d cartUserPos = SGVec3d::fromGeod(userpos);
1019 SGVec3d FGAIBallistic::getCartHitchPos() const{
1021 // convert geodetic positions to geocentered
1022 SGVec3d cartuserPos = SGVec3d::fromGeod(userpos);
1023 //SGVec3d cartPos = getCartPos();
1025 // Transform to the right coordinate frame, configuration is done in
1026 // the x-forward, y-right, z-up coordinates (feet), computation
1027 // in the simulation usual body x-forward, y-right, z-down coordinates
1029 SGVec3d _off(_x_offset * SG_FEET_TO_METER,
1030 _y_offset * SG_FEET_TO_METER,
1031 -_z_offset * SG_FEET_TO_METER);
1033 // Transform the user position to the horizontal local coordinate system.
1034 SGQuatd hlTrans = SGQuatd::fromLonLat(userpos);
1036 // and postrotate the orientation of the user model wrt the horizontal
1038 hlTrans *= SGQuatd::fromYawPitchRollDeg(
1039 manager->get_user_heading(),
1040 manager->get_user_pitch(),
1041 manager->get_user_roll());
1043 // The offset converted to the usual body fixed coordinate system
1044 // rotated to the earth-fixed coordinates axis
1045 SGVec3d off = hlTrans.backTransform(_off);
1047 // Add the position offset of the user model to get the geocentered position
1048 SGVec3d offsetPos = cartuserPos + off;
1053 void FGAIBallistic::setOffsetPos(SGGeod inpos, double heading, double pitch, double roll){
1054 // convert the hitch geocentered position to geodetic
1056 SGVec3d cartoffsetPos = getCartOffsetPos(inpos, heading, pitch, roll);
1058 //SGVec3d cartoffsetPos = getCartHitchPos();
1060 //SGGeodesy::SGCartToGeod(cartoffsetPos, hitchpos);
1061 SGGeodesy::SGCartToGeod(cartoffsetPos, _offsetpos);
1065 double FGAIBallistic::getDistanceToHitch() const {
1066 //calculate the distance load to hitch
1067 SGVec3d carthitchPos = getCartHitchPos();
1068 SGVec3d cartPos = getCartPos();
1070 SGVec3d diff = carthitchPos - cartPos;
1071 double distance = norm(diff);
1072 return distance * SG_METER_TO_FEET;
1075 double FGAIBallistic::getElevToHitch() const {
1076 // now the angle, positive angles are upwards
1077 double distance = getDistanceToHitch() * SG_FEET_TO_METER;
1079 double daltM = _offsetpos.getElevationM() - pos.getElevationM();
1081 if (fabs(distance) < SGLimits<float>::min()) {
1084 double sAngle = daltM/distance;
1085 sAngle = SGMiscd::min(1, SGMiscd::max(-1, sAngle));
1086 angle = SGMiscd::rad2deg(asin(sAngle));
1092 double FGAIBallistic::getBearingToHitch() const {
1093 //calculate the bearing and range of the second pos from the first
1094 double distance = getDistanceToHitch() * SG_FEET_TO_METER;
1097 geo_inverse_wgs_84(pos, _offsetpos, &az1, &az2, &distance);
1102 double FGAIBallistic::getRelBrgHitchToUser() const {
1103 //calculate the relative bearing
1104 double az1, az2, distance;
1106 geo_inverse_wgs_84(_offsetpos, userpos, &az1, &az2, &distance);
1108 double rel_brg = az1 - hdg;
1110 SG_NORMALIZE_RANGE(rel_brg, -180.0, 180.0);
1115 double FGAIBallistic::getElevHitchToUser() const {
1117 //calculate the distance from the user position
1118 SGVec3d carthitchPos = getCartHitchPos();
1119 SGVec3d cartuserPos = getCartUserPos();
1121 SGVec3d diff = cartuserPos - carthitchPos;
1123 double distance = norm(diff);
1126 double daltM = userpos.getElevationM() - _offsetpos.getElevationM();
1128 // now the angle, positive angles are upwards
1129 if (fabs(distance) < SGLimits<float>::min()) {
1132 double sAngle = daltM/distance;
1133 sAngle = SGMiscd::min(1, SGMiscd::max(-1, sAngle));
1134 angle = SGMiscd::rad2deg(asin(sAngle));
1140 void FGAIBallistic::setTgtOffsets(double dt, double coeff){
1141 double c = dt / (coeff + dt);
1143 _x_offset = (_tgt_x_offset * c) + (_x_offset * (1 - c));
1144 _y_offset = (_tgt_y_offset * c) + (_y_offset * (1 - c));
1145 _z_offset = (_tgt_z_offset * c) + (_z_offset * (1 - c));
1149 void FGAIBallistic::calcVSHS(){
1150 // calculate vertical and horizontal speed components
1151 double speed_fps = speed * SG_KT_TO_FPS;
1156 vs = sin( _elevation * SG_DEGREES_TO_RADIANS ) * speed_fps;
1157 hs = cos( _elevation * SG_DEGREES_TO_RADIANS ) * speed_fps;
1161 void FGAIBallistic::calcNE(){
1162 //resolve horizontal speed into north and east components:
1163 _speed_north_fps = cos(_azimuth / SG_RADIANS_TO_DEGREES) * hs;
1164 _speed_east_fps = sin(_azimuth / SG_RADIANS_TO_DEGREES) * hs;
1166 // convert horizontal speed (fps) to degrees per second
1167 speed_north_deg_sec = _speed_north_fps / ft_per_deg_lat;
1168 speed_east_deg_sec = _speed_east_fps / ft_per_deg_lon;
1172 SGVec3d FGAIBallistic::getCartOffsetPos(SGGeod inpos, double user_heading,
1173 double user_pitch, double user_roll
1176 // convert geodetic positions to geocentered
1177 SGVec3d cartuserPos = SGVec3d::fromGeod(inpos);
1178 //SGVec3d cartuserPos = getCartUserPos();
1179 //SGVec3d cartPos = getCartPos();
1181 // Transform to the right coordinate frame, configuration is done in
1182 // the x-forward, y-right, z-up coordinates (feet), computation
1183 // in the simulation usual body x-forward, y-right, z-down coordinates
1185 SGVec3d _off(_x_offset * SG_FEET_TO_METER,
1186 _y_offset * SG_FEET_TO_METER,
1187 -_z_offset * SG_FEET_TO_METER);
1189 // Transform the user position to the horizontal local coordinate system.
1190 SGQuatd hlTrans = SGQuatd::fromLonLat(inpos);
1192 // and postrotate the orientation of the user model wrt the horizontal
1194 hlTrans *= SGQuatd::fromYawPitchRollDeg(
1199 // The offset converted to the usual body fixed coordinate system
1200 // rotated to the earth-fixed coordinates axis
1201 SGVec3d off = hlTrans.backTransform(_off);
1203 // Add the position offset of the user model to get the geocentered position
1204 SGVec3d offsetPos = cartuserPos + off;
1209 void FGAIBallistic::setOffsetVelocity(double dt, SGGeod offsetpos) {
1210 //calculate the distance from the previous offset position
1211 SGVec3d cartoffsetPos = SGVec3d::fromGeod(offsetpos);
1212 SGVec3d diff = cartoffsetPos - _oldcartoffsetPos;
1214 double distance = norm(diff);
1215 //calculate speed knots
1216 speed = (distance/dt) * SG_MPS_TO_KT;
1218 //now calulate the angle between the old and current postion positions (degrees)
1220 double daltM = offsetpos.getElevationM() - _oldoffsetpos.getElevationM();
1222 if (fabs(distance) < SGLimits<float>::min()) {
1225 double sAngle = daltM/distance;
1226 sAngle = SGMiscd::min(1, SGMiscd::max(-1, sAngle));
1227 angle = SGMiscd::rad2deg(asin(sAngle));
1232 //calculate vertical and horizontal speed components
1235 //calculate the bearing of the new offset position from the old
1236 //don't do this if speed is low
1237 //cout << "speed " << speed << endl;
1239 double az1, az2, dist;
1240 geo_inverse_wgs_84(_oldoffsetpos, offsetpos, &az1, &az2, &dist);
1242 //cout << "offset az " << _azimuth << endl;
1245 //cout << " slow offset az " << _azimuth << endl;
1248 //resolve horizontal speed into north and east components:
1251 // and finally store the new values
1252 _oldcartoffsetPos = cartoffsetPos;
1253 _oldoffsetpos = offsetpos;