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 "AIModelData.hxx"
33 #include "AIBallistic.hxx"
35 #include <Main/util.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) :
45 _aero_stabilised(false),
48 _gravity(32.1740485564),
54 _report_collision(false),
55 _report_impact(false),
57 _impact_report_node(fgGetNode("/ai/models/model-impact", true)),
58 _force_stabilised(false),
59 _external_force(false),
61 _slave_load_to_ac(false),
62 _formate_to_ac(false),
72 FGAIBallistic::~FGAIBallistic() {
75 void FGAIBallistic::readFromScenario(SGPropertyNode* scFileNode) {
80 FGAIBase::readFromScenario(scFileNode);
82 //setPath(scFileNode->getStringValue("model", "Models/Geometry/rocket.ac"));
83 setAzimuth(scFileNode->getDoubleValue("azimuth", 0.0));
84 setElevation(scFileNode->getDoubleValue("elevation", 0));
85 setDragArea(scFileNode->getDoubleValue("eda", 0.007));
86 setLife(scFileNode->getDoubleValue("life", 900.0));
87 setBuoyancy(scFileNode->getDoubleValue("buoyancy", 0));
88 setWind_from_east(scFileNode->getDoubleValue("wind_from_east", 0));
89 setWind_from_north(scFileNode->getDoubleValue("wind_from_north", 0));
90 setWind(scFileNode->getBoolValue("wind", false));
91 setRoll(scFileNode->getDoubleValue("roll", 0.0));
92 setCd(scFileNode->getDoubleValue("cd", 0.029));
93 //setMass(scFileNode->getDoubleValue("mass", 0.007));
94 setWeight(scFileNode->getDoubleValue("weight", 0.25));
95 setStabilisation(scFileNode->getBoolValue("aero-stabilized", false));
96 setNoRoll(scFileNode->getBoolValue("no-roll", false));
97 setRandom(scFileNode->getBoolValue("random", false));
98 setImpact(scFileNode->getBoolValue("impact", false));
99 setImpactReportNode(scFileNode->getStringValue("impact-reports"));
100 setName(scFileNode->getStringValue("name", "Rocket"));
101 setFuseRange(scFileNode->getDoubleValue("fuse-range", 0.0));
102 setSMPath(scFileNode->getStringValue("submodel-path", ""));
103 setSubID(scFileNode->getIntValue("SubID", 0));
104 setExternalForce(scFileNode->getBoolValue("external-force", false));
105 setForcePath(scFileNode->getStringValue("force-path", ""));
106 setForceStabilisation(scFileNode->getBoolValue("force-stabilized", false));
107 setXoffset(scFileNode->getDoubleValue("x-offset", 0.0));
108 setYoffset(scFileNode->getDoubleValue("y-offset", 0.0));
109 setZoffset(scFileNode->getDoubleValue("z-offset", 0.0));
110 setPitchoffset(scFileNode->getDoubleValue("pitch-offset", 0.0));
111 setRolloffset(scFileNode->getDoubleValue("roll-offset", 0.0));
112 setYawoffset(scFileNode->getDoubleValue("yaw-offset", 0.0));
113 setGroundOffset(scFileNode->getDoubleValue("ground-offset", 0.0));
114 setLoadOffset(scFileNode->getDoubleValue("load-offset", 0.0));
115 setSlaved(scFileNode->getBoolValue("slaved", false));
116 setSlavedLoad(scFileNode->getBoolValue("slaved-load", false));
117 setContentsNode(scFileNode->getStringValue("contents"));
118 setRandom(scFileNode->getBoolValue("random", false));
121 osg::Node* FGAIBallistic::load3DModel(const string &path, SGPropertyNode *prop_root)
123 model = SGModelLib::loadModel(path, prop_root, new FGAIModelData(this, prop_root));
127 bool FGAIBallistic::init(bool search_in_AI_path) {
128 FGAIBase::init(search_in_AI_path);
130 _impact_reported = false;
131 _collision_reported = false;
134 _elapsed_time += (sg_random() * 100);
136 props->setStringValue("material/name", "");
137 props->setStringValue("name", _name.c_str());
138 props->setStringValue("submodels/path", _submodel.c_str());
140 // start with high value so that animations don't trigger yet
151 void FGAIBallistic::bind() {
154 props->tie("sim/time/elapsed-sec",
155 SGRawValueMethods<FGAIBallistic,double>(*this,
156 &FGAIBallistic::_getTime));
157 props->tie("mass-slug",
158 SGRawValueMethods<FGAIBallistic,double>(*this,
159 &FGAIBallistic::getMass));
160 props->tie("material/load-resistance",
161 SGRawValuePointer<double>(&_load_resistance));
162 props->tie("material/solid",
163 SGRawValuePointer<bool>(&_solid));
164 props->tie("altitude-agl-ft",
165 SGRawValuePointer<double>(&_ht_agl_ft));
166 props->tie("controls/slave-to-ac",
167 SGRawValueMethods<FGAIBallistic,bool>
168 (*this, &FGAIBallistic::getSlaved, &FGAIBallistic::setSlaved));
169 props->tie("controls/invisible",
170 SGRawValuePointer<bool>(&invisible));
173 props->tie("controls/force_stabilized",
174 SGRawValuePointer<bool>(&_force_stabilised));
175 props->tie("position/global-x",
176 SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getCartPosX, 0));
177 props->tie("position/global-y",
178 SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getCartPosY, 0));
179 props->tie("position/global-z",
180 SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getCartPosZ, 0));
181 props->tie("velocities/vertical-speed-fps",
182 SGRawValuePointer<double>(&vs));
183 props->tie("velocities/true-airspeed-kt",
184 SGRawValuePointer<double>(&speed));
185 props->tie("velocities/horizontal-speed-fps",
186 SGRawValuePointer<double>(&hs));
187 props->tie("position/altitude-ft",
188 SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getAltitude, &FGAIBase::_setAltitude));
189 props->tie("position/latitude-deg",
190 SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getLatitude, &FGAIBase::_setLatitude));
191 props->tie("position/longitude-deg",
192 SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getLongitude, &FGAIBase::_setLongitude));
193 props->tie("orientation/hdg-deg",
194 SGRawValuePointer<double>(&hdg));
195 props->tie("orientation/pitch-deg",
196 SGRawValuePointer<double>(&pitch));
197 props->tie("orientation/roll-deg",
198 SGRawValuePointer<double>(&roll));
199 props->tie("controls/slave-load-to-ac",
200 SGRawValueMethods<FGAIBallistic,bool>
201 (*this, &FGAIBallistic::getSlavedLoad, &FGAIBallistic::setSlavedLoad));
202 props->tie("position/load-offset",
203 SGRawValueMethods<FGAIBallistic,double>
204 (*this, &FGAIBallistic::getLoadOffset, &FGAIBallistic::setLoadOffset));
205 props->tie("load/distance-to-hitch-ft",
206 SGRawValueMethods<FGAIBallistic,double>
207 (*this, &FGAIBallistic::getDistanceLoadToHitch));
208 props->tie("load/elevation-to-hitch-deg",
209 SGRawValueMethods<FGAIBallistic,double>
210 (*this, &FGAIBallistic::getElevLoadToHitch));
211 props->tie("load/bearing-to-hitch-deg",
212 SGRawValueMethods<FGAIBallistic,double>
213 (*this, &FGAIBallistic::getBearingLoadToHitch));
218 void FGAIBallistic::unbind() {
219 // FGAIBase::unbind();
221 props->untie("sim/time/elapsed-sec");
222 props->untie("mass-slug");
223 props->untie("material/load-resistance");
224 props->untie("material/solid");
225 props->untie("altitude-agl-ft");
226 props->untie("controls/slave-to-ac");
227 props->untie("controls/invisible");
230 props->untie("position/global-y");
231 props->untie("position/global-x");
232 props->untie("position/global-z");
233 props->untie("velocities/vertical-speed-fps");
234 props->untie("velocities/true-airspeed-kt");
235 props->untie("velocities/horizontal-speed-fps");
236 props->untie("position/altitude-ft");
237 props->untie("position/latitude-deg");
238 props->untie("position/longitude-deg");
239 props->untie("position/ht-agl-ft");
240 props->untie("orientation/hdg-deg");
241 props->untie("orientation/pitch-deg");
242 props->untie("orientation/roll-deg");
243 props->untie("controls/force_stabilized");
244 props->untie("position/load-offset");
245 props->untie("load/distance-to-hitch-ft");
246 props->untie("load/elevation-to-hitch-deg");
247 props->untie("load/bearing-to-hitch-deg");
251 void FGAIBallistic::update(double dt) {
252 FGAIBase::update(dt);
258 setHitchVelocity(dt);
259 } else if (_formate_to_ac){
262 setHitchVelocity(dt);
263 } else if (!invisible){
270 void FGAIBallistic::setAzimuth(double az) {
274 void FGAIBallistic::setElevation(double el) {
275 pitch = _elevation = el;
278 void FGAIBallistic::setRoll(double rl) {
279 roll = _rotation = rl;
282 void FGAIBallistic::setStabilisation(bool val) {
283 _aero_stabilised = val;
286 void FGAIBallistic::setForceStabilisation(bool val) {
287 _force_stabilised = val;
290 void FGAIBallistic::setNoRoll(bool nr) {
294 void FGAIBallistic::setDragArea(double a) {
298 void FGAIBallistic::setLife(double seconds) {
302 void FGAIBallistic::setBuoyancy(double fpss) {
306 void FGAIBallistic::setWind_from_east(double fps) {
307 _wind_from_east = fps;
310 void FGAIBallistic::setWind_from_north(double fps) {
311 _wind_from_north = fps;
314 void FGAIBallistic::setWind(bool val) {
318 void FGAIBallistic::setCd(double c) {
322 void FGAIBallistic::setMass(double m) {
326 void FGAIBallistic::setWeight(double w) {
329 void FGAIBallistic::setRandom(bool r) {
333 void FGAIBallistic::setImpact(bool i) {
337 void FGAIBallistic::setCollision(bool c) {
338 _report_collision = c;
341 void FGAIBallistic::setExternalForce(bool f) {
345 void FGAIBallistic::setImpactReportNode(const string& path) {
348 _impact_report_node = fgGetNode(path.c_str(), true);
351 void FGAIBallistic::setName(const string& n) {
355 void FGAIBallistic::setSMPath(const string& s) {
359 void FGAIBallistic::setFuseRange(double f) {
363 void FGAIBallistic::setSubID(int i) {
367 void FGAIBallistic::setSubmodel(const string& s) {
371 void FGAIBallistic::setGroundOffset(double g) {
375 void FGAIBallistic::setLoadOffset(double l) {
379 double FGAIBallistic::getLoadOffset() const {
383 void FGAIBallistic::setSlaved(bool s) {
387 void FGAIBallistic::setFormate(bool f) {
391 void FGAIBallistic::setContentsNode(const string& path) {
393 _contents_node = fgGetNode(path.c_str(), true);
397 bool FGAIBallistic::getSlaved() const {
401 double FGAIBallistic::getMass() const {
405 double FGAIBallistic::getContents() {
407 _contents_lb = _contents_node->getChild("level-lbs",0,1)->getDoubleValue();
411 void FGAIBallistic::setContents(double c) {
413 _contents_lb = _contents_node->getChild("level-gal_us",0,1)->setDoubleValue(c);
416 void FGAIBallistic::setSlavedLoad(bool l) {
417 _slave_load_to_ac = l;
420 bool FGAIBallistic::getSlavedLoad() const {
421 return _slave_load_to_ac;
424 void FGAIBallistic::setForcePath(const string& p) {
426 if (!_force_path.empty()) {
427 SGPropertyNode *fnode = fgGetNode(_force_path.c_str(), 0, true );
428 _force_node = fnode->getChild("force-lb", 0, true);
429 _force_azimuth_node = fnode->getChild("force-azimuth-deg", 0, true);
430 _force_elevation_node = fnode->getChild("force-elevation-deg", 0, true);
434 bool FGAIBallistic::getHtAGL(){
436 if (globals->get_scenery()->get_elevation_m(pos.getLatitudeDeg(), pos.getLongitudeDeg(),
437 10000.0, _elevation_m, &_material)){
438 _ht_agl_ft = pos.getElevationFt() - _elevation_m * SG_METER_TO_FEET;
440 const vector<string>& names = _material->get_names();
442 _solid = _material->get_solid();
443 _load_resistance = _material->get_load_resistance();
444 _frictionFactor =_material->get_friction_factor();
446 props->setStringValue("material/name", names[0].c_str());
448 props->setStringValue("material/name", "");
449 /*cout << "material " << mat_name
450 << " solid " << _solid
451 << " load " << _load_resistance
452 << " frictionFactor " << frictionFactor
462 double FGAIBallistic::getRecip(double az){
463 // calculate the reciprocal of the input azimuth
471 void FGAIBallistic::setPch(double e, double dt, double coeff){
472 double c = dt / (coeff + dt);
473 pitch = (e * c) + (pitch * (1 - c));
476 void FGAIBallistic::setBnk(double r, double dt, double coeff){
477 double c = dt / (coeff + dt);
478 roll = (r * c) + (roll * (1 - c));
481 void FGAIBallistic::setHt(double h, double dt, double coeff){
482 double c = dt / (coeff + dt);
483 _height = (h * c) + (_height * (1 - c));
486 void FGAIBallistic::setHdg(double az, double dt, double coeff){
487 double recip = getRecip(hdg);
488 double c = dt / (coeff + dt);
489 //we need to ensure that we turn the short way to the new hdg
490 if (az < recip && az < hdg && hdg > 180) {
491 hdg = ((az + 360) * c) + (hdg * (1 - c));
492 } else if (az > recip && az > hdg && hdg <= 180){
493 hdg = ((az - 360) * c) + (hdg * (1 - c));
495 hdg = (az * c) + (hdg * (1 - c));
499 double FGAIBallistic::getTgtXOffset() const {
500 return _tgt_x_offset;
503 double FGAIBallistic::getTgtYOffset() const {
504 return _tgt_y_offset;
507 double FGAIBallistic::getTgtZOffset() const {
508 return _tgt_z_offset;
511 void FGAIBallistic::setTgtXOffset(double x){
515 void FGAIBallistic::setTgtYOffset(double y){
519 void FGAIBallistic::setTgtZOffset(double z){
523 void FGAIBallistic::slaveToAC(double dt){
526 pos.setLatitudeDeg(hitchpos.getLatitudeDeg());
527 pos.setLongitudeDeg(hitchpos.getLongitudeDeg());
528 pos.setElevationFt(hitchpos.getElevationFt());
529 setHeading(manager->get_user_heading());
530 setPitch(manager->get_user_pitch() + _pitch_offset);
531 setBank(manager->get_user_roll() + _roll_offset);
532 setSpeed(manager->get_user_speed());
533 //update the mass (slugs)
534 _mass = (_weight_lb + getContents()) / slugs_to_lbs;
536 /*cout <<"_mass "<<_mass <<" " << getContents()
537 <<" " << getContents() / slugs_to_lbs << endl;*/
540 void FGAIBallistic::Run(double dt) {
543 // if life = -1 the object does not die
544 if (_life_timer > life && life != -1)
547 //set the contents in the appropriate tank or other property in the parent to zero
550 //randomise Cd by +- 5%
552 _Cd = _Cd * 0.95 + (0.05 * sg_random());
554 // Adjust Cd by Mach number. The equations are based on curves
555 // for a conventional shell/bullet (no boat-tail).
559 Cdm = 0.0125 * Mach + _Cd;
560 else if (Mach < 1.2 )
561 Cdm = 0.3742 * pow(Mach, 2) - 0.252 * Mach + 0.0021 + _Cd;
563 Cdm = 0.2965 * pow(Mach, -1.1506) + _Cd;
565 //cout << "Mach " << Mach << " Cdm " << Cdm << "// ballistic speed kts "<< speed << endl;
567 // drag = Cd * 0.5 * rho * speed * speed * drag_area;
568 // rho is adjusted for altitude in void FGAIBase::update,
569 // using Standard Atmosphere (sealevel temperature 15C)
570 // acceleration = drag/mass;
571 // adjust speed by drag
572 speed -= (Cdm * 0.5 * rho * speed * speed * _drag_area/_mass) * dt;
574 // don't let speed become negative
578 double speed_fps = speed * SG_KT_TO_FPS;
581 // calculate vertical and horizontal speed components
585 vs = sin( _elevation * SG_DEGREES_TO_RADIANS ) * speed_fps;
586 hs = cos( _elevation * SG_DEGREES_TO_RADIANS ) * speed_fps;
589 //resolve horizontal speed into north and east components:
590 double speed_north_fps = cos(_azimuth / SG_RADIANS_TO_DEGREES) * hs;
591 double speed_east_fps = sin(_azimuth / SG_RADIANS_TO_DEGREES) * hs;
593 // convert horizontal speed (fps) to degrees per second
594 double speed_north_deg_sec = speed_north_fps / ft_per_deg_lat;
595 double speed_east_deg_sec = speed_east_fps / ft_per_deg_lon;
597 // if wind not required, set to zero
599 _wind_from_north = 0;
602 _wind_from_north = manager->get_wind_from_north();
603 _wind_from_east = manager->get_wind_from_east();
606 //calculate velocity due to external force
607 double force_speed_north_deg_sec = 0;
608 double force_speed_east_deg_sec = 0;
609 double vs_force_fps = 0;
610 double hs_force_fps = 0;
611 double v_force_acc_fpss = 0;
612 double force_speed_north_fps = 0;
613 double force_speed_east_fps = 0;
614 double h_force_lbs = 0;
615 double normal_force_lbs = 0;
616 double normal_force_fpss = 0;
617 double static_friction_force_lbs = 0;
618 double dynamic_friction_force_lbs = 0;
619 double friction_force_speed_north_fps = 0;
620 double friction_force_speed_east_fps = 0;
621 double friction_force_speed_north_deg_sec = 0;
622 double friction_force_speed_east_deg_sec = 0;
623 double force_elevation_deg = 0;
625 if (_external_force) {
626 SGPropertyNode *n = fgGetNode(_force_path.c_str(), true);
627 double force_lbs = n->getChild("force-lb", 0, true)->getDoubleValue();
628 force_elevation_deg = n->getChild("force-elevation-deg", 0, true)->getDoubleValue();
629 double force_azimuth_deg = n->getChild("force-azimuth-deg", 0, true)->getDoubleValue();
631 //resolve force into vertical and horizontal components:
632 double v_force_lbs = force_lbs * sin( force_elevation_deg * SG_DEGREES_TO_RADIANS );
633 h_force_lbs = force_lbs * cos( force_elevation_deg * SG_DEGREES_TO_RADIANS );
638 double deadzone = 0.1;
640 if (_ht_agl_ft <= (0 + _ground_offset + deadzone) && _solid){
641 normal_force_lbs = (_mass * slugs_to_lbs) - v_force_lbs;
643 if ( normal_force_lbs < 0 )
644 normal_force_lbs = 0;
646 pos.setElevationFt(0 + _ground_offset);
650 // calculate friction
651 // we assume a static Coefficient of Friction (mu) of 0.62 (wood on concrete)
654 static_friction_force_lbs = mu * normal_force_lbs * _frictionFactor;
656 //adjust horizontal force. We assume that a speed of <= 5 fps is static
657 if (h_force_lbs <= static_friction_force_lbs && hs <= 5){
658 h_force_lbs = hs = 0;
659 speed_north_fps = speed_east_fps = 0;
661 dynamic_friction_force_lbs = (static_friction_force_lbs * 0.95);
663 //ignore wind when on the ground for now
665 _wind_from_north = 0;
672 //acceleration = (force(lbsf)/mass(slugs))
673 v_force_acc_fpss = v_force_lbs/_mass;
674 normal_force_fpss = normal_force_lbs/_mass;
675 double h_force_acc_fpss = h_force_lbs/_mass;
676 double dynamic_friction_acc_fpss = dynamic_friction_force_lbs/_mass;
678 // velocity = acceleration * dt
679 hs_force_fps = h_force_acc_fpss * dt;
680 double friction_force_fps = dynamic_friction_acc_fpss * dt;
682 //resolve horizontal speeds into north and east components:
683 force_speed_north_fps = cos(force_azimuth_deg * SG_DEGREES_TO_RADIANS) * hs_force_fps;
684 force_speed_east_fps = sin(force_azimuth_deg * SG_DEGREES_TO_RADIANS) * hs_force_fps;
686 friction_force_speed_north_fps = cos(getRecip(hdg) * SG_DEGREES_TO_RADIANS) * friction_force_fps;
687 friction_force_speed_east_fps = sin(getRecip(hdg) * SG_DEGREES_TO_RADIANS) * friction_force_fps;
689 // convert horizontal speed (fps) to degrees per second
690 force_speed_north_deg_sec = force_speed_north_fps / ft_per_deg_lat;
691 force_speed_east_deg_sec = force_speed_east_fps / ft_per_deg_lon;
693 friction_force_speed_north_deg_sec = friction_force_speed_north_fps / ft_per_deg_lat;
694 friction_force_speed_east_deg_sec = friction_force_speed_east_fps / ft_per_deg_lon;
697 // convert wind speed (fps) to degrees lat/lon per second
698 double wind_speed_from_north_deg_sec = _wind_from_north / ft_per_deg_lat;
699 double wind_speed_from_east_deg_sec = _wind_from_east / ft_per_deg_lon;
701 //recombine the horizontal velocity components
702 hs = sqrt(((speed_north_fps + force_speed_north_fps + friction_force_speed_north_fps)
703 * (speed_north_fps + force_speed_north_fps + friction_force_speed_north_fps))
704 + ((speed_east_fps + force_speed_east_fps + friction_force_speed_east_fps)
705 * (speed_east_fps + force_speed_east_fps + friction_force_speed_east_fps)));
710 // adjust vertical speed for acceleration of gravity, buoyancy, and vertical force
711 vs -= (_gravity - _buoyancy - v_force_acc_fpss - normal_force_fpss) * dt;
713 if (vs <= 0.00001 && vs >= -0.00001)
717 if(_slave_load_to_ac) {
719 pos.setLatitudeDeg(hitchpos.getLatitudeDeg());
720 pos.setLongitudeDeg(hitchpos.getLongitudeDeg());
721 pos.setElevationFt(hitchpos.getElevationFt());
724 double deadzone = 0.1;
726 if (_ht_agl_ft <= (0 + _ground_offset + deadzone) && _solid){
727 pos.setElevationFt(0 + _ground_offset);
729 pos.setElevationFt(hitchpos.getElevationFt() + _load_offset);
734 pos.setLatitudeDeg( pos.getLatitudeDeg()
735 + (speed_north_deg_sec - wind_speed_from_north_deg_sec
736 + force_speed_north_deg_sec + friction_force_speed_north_deg_sec) * dt );
737 pos.setLongitudeDeg( pos.getLongitudeDeg()
738 + (speed_east_deg_sec - wind_speed_from_east_deg_sec
739 + force_speed_east_deg_sec + friction_force_speed_east_deg_sec) * dt );
740 pos.setElevationFt(pos.getElevationFt() + vs * dt);
743 // recalculate total speed
744 if ( vs == 0 && hs == 0)
747 speed = sqrt( vs * vs + hs * hs) / SG_KT_TO_FPS;
749 // recalculate elevation and azimuth (velocity vectors)
750 _elevation = atan2( vs, hs ) * SG_RADIANS_TO_DEGREES;
751 _azimuth = atan2((speed_east_fps + force_speed_east_fps + friction_force_speed_east_fps),
752 (speed_north_fps + force_speed_north_fps + friction_force_speed_north_fps))
753 * SG_RADIANS_TO_DEGREES;
755 // rationalise azimuth
759 if (_aero_stabilised) { // we simulate rotational moment of inertia by using a filter
760 const double coeff = 0.9;
762 // we assume a symetrical MI about the pitch and yaw axis
763 setPch(_elevation, dt, coeff);
764 setHdg(_azimuth, dt, coeff);
765 } else if (_force_stabilised) { // we simulate rotational moment of inertia by using a filter
766 const double coeff = 0.9;
767 double ratio = h_force_lbs/(_mass * slugs_to_lbs);
769 if (ratio > 1) ratio = 1;
770 if (ratio < -1) ratio = -1;
772 double force_pitch = acos(ratio) * SG_RADIANS_TO_DEGREES;
774 if (force_pitch <= force_elevation_deg)
775 force_pitch = force_elevation_deg;
777 // we assume a symetrical MI about the pitch and yaw axis
778 setPch(force_pitch,dt, coeff);
779 setHdg(_azimuth, dt, coeff);
782 //do impacts and collisions
783 if (_report_impact && !_impact_reported)
786 if (_report_collision && !_collision_reported)
789 // set destruction flag if altitude less than sea level -1000
790 if (altitude_ft < -1000.0 && life != -1)
795 double FGAIBallistic::_getTime() const {
799 void FGAIBallistic::handle_impact() {
801 // try terrain intersection
805 if (_ht_agl_ft <= 0) {
806 SG_LOG(SG_GENERAL, SG_DEBUG, "AIBallistic: terrain impact");
807 report_impact(_elevation_m);
808 _impact_reported = true;
812 } else if (_subID == 0) // kill the AIObject if there is no subsubmodel
817 void FGAIBallistic::handle_collision()
819 const FGAIBase *object = manager->calcCollision(pos.getElevationFt(),
820 pos.getLatitudeDeg(),pos.getLongitudeDeg(), _fuse_range);
823 SG_LOG(SG_GENERAL, SG_DEBUG, "AIBallistic: object hit");
824 report_impact(pos.getElevationM(), object);
825 _collision_reported = true;
829 void FGAIBallistic::report_impact(double elevation, const FGAIBase *object)
831 _impact_lat = pos.getLatitudeDeg();
832 _impact_lon = pos.getLongitudeDeg();
833 _impact_elev = elevation;
834 _impact_speed = speed * SG_KT_TO_MPS;
836 _impact_pitch = pitch;
839 SGPropertyNode *n = props->getNode("impact", true);
841 n->setStringValue("type", object->getTypeString());
843 n->setStringValue("type", "terrain");
845 n->setDoubleValue("longitude-deg", _impact_lon);
846 n->setDoubleValue("latitude-deg", _impact_lat);
847 n->setDoubleValue("elevation-m", _impact_elev);
848 n->setDoubleValue("heading-deg", _impact_hdg);
849 n->setDoubleValue("pitch-deg", _impact_pitch);
850 n->setDoubleValue("roll-deg", _impact_roll);
851 n->setDoubleValue("speed-mps", _impact_speed);
853 _impact_report_node->setStringValue(props->getPath());
856 SGVec3d FGAIBallistic::getCartUserPos() const {
857 SGVec3d cartUserPos = SGVec3d::fromGeod(userpos);
861 SGVec3d FGAIBallistic::getCartHitchPos() const{
863 // convert geodetic positions to geocentered
864 SGVec3d cartuserPos = getCartUserPos();
865 SGVec3d cartPos = getCartPos();
867 // Transform to the right coordinate frame, configuration is done in
868 // the x-forward, y-right, z-up coordinates (feet), computation
869 // in the simulation usual body x-forward, y-right, z-down coordinates
871 SGVec3d _off(_x_offset * SG_FEET_TO_METER,
872 _y_offset * SG_FEET_TO_METER,
873 -_z_offset * SG_FEET_TO_METER);
875 // Transform the user position to the horizontal local coordinate system.
876 SGQuatd hlTrans = SGQuatd::fromLonLat(userpos);
878 // and postrotate the orientation of the user model wrt the horizontal
880 hlTrans *= SGQuatd::fromYawPitchRollDeg(
881 manager->get_user_heading(),
882 manager->get_user_pitch(),
883 manager->get_user_roll());
885 // The offset converted to the usual body fixed coordinate system
886 // rotated to the earth-fixed coordinates axis
887 SGVec3d off = hlTrans.backTransform(_off);
889 // Add the position offset of the user model to get the geocentered position
890 SGVec3d offsetPos = cartuserPos + off;
895 void FGAIBallistic::setHitchPos(){
896 // convert the hitch geocentered position to geodetic
897 SGVec3d carthitchPos = getCartHitchPos();
899 SGGeodesy::SGCartToGeod(carthitchPos, hitchpos);
902 double FGAIBallistic::getDistanceLoadToHitch() const {
903 //calculate the distance load to hitch
904 SGVec3d carthitchPos = getCartHitchPos();
905 SGVec3d cartPos = getCartPos();
907 SGVec3d diff = carthitchPos - cartPos;
908 double distance = norm(diff);
909 return distance * SG_METER_TO_FEET;
912 void FGAIBallistic::setHitchVelocity(double dt) {
913 //calculate the distance from the previous hitch position
914 SGVec3d carthitchPos = getCartHitchPos();
915 SGVec3d diff = carthitchPos - _oldcarthitchPos;
917 double distance = norm(diff);
919 //calculate speed knots
920 speed = (distance/dt) * SG_MPS_TO_KT;
922 //now calulate the angle between the old and current hitch positions (degrees)
924 double daltM = hitchpos.getElevationM() - oldhitchpos.getElevationM();
926 if (fabs(distance) < SGLimits<float>::min()) {
929 double sAngle = daltM/distance;
930 sAngle = SGMiscd::min(1, SGMiscd::max(-1, sAngle));
931 angle = SGMiscd::rad2deg(asin(sAngle));
936 //calculate the bearing of the new hitch position from the old
937 double az1, az2, dist;
939 geo_inverse_wgs_84(oldhitchpos, hitchpos, &az1, &az2, &dist);
943 // and finally store the new values
944 _oldcarthitchPos = carthitchPos;
945 oldhitchpos = hitchpos;
948 double FGAIBallistic::getElevLoadToHitch() const {
949 // now the angle, positive angles are upwards
950 double distance = getDistanceLoadToHitch() * SG_FEET_TO_METER;
952 double daltM = hitchpos.getElevationM() - pos.getElevationM();
954 if (fabs(distance) < SGLimits<float>::min()) {
957 double sAngle = daltM/distance;
958 sAngle = SGMiscd::min(1, SGMiscd::max(-1, sAngle));
959 angle = SGMiscd::rad2deg(asin(sAngle));
965 double FGAIBallistic::getBearingLoadToHitch() const {
966 //calculate the bearing and range of the second pos from the first
967 double az1, az2, distance;
969 geo_inverse_wgs_84(pos, hitchpos, &az1, &az2, &distance);
974 double FGAIBallistic::getRelBrgHitchToUser() const {
975 //calculate the relative bearing
976 double az1, az2, distance;
978 geo_inverse_wgs_84(hitchpos, userpos, &az1, &az2, &distance);
980 double rel_brg = az1 - hdg;
988 double FGAIBallistic::getElevHitchToUser() const {
990 //calculate the distance from the user position
991 SGVec3d carthitchPos = getCartHitchPos();
992 SGVec3d cartuserPos = getCartUserPos();
994 SGVec3d diff = cartuserPos - carthitchPos;
996 double distance = norm(diff);
999 double daltM = userpos.getElevationM() - hitchpos.getElevationM();
1001 // now the angle, positive angles are upwards
1002 if (fabs(distance) < SGLimits<float>::min()) {
1005 double sAngle = daltM/distance;
1006 sAngle = SGMiscd::min(1, SGMiscd::max(-1, sAngle));
1007 angle = SGMiscd::rad2deg(asin(sAngle));
1013 void FGAIBallistic::setTgtOffsets(double dt, double coeff){
1014 double c = dt / (coeff + dt);
1016 _x_offset = (_tgt_x_offset * c) + (_x_offset * (1 - c));
1017 _y_offset = (_tgt_y_offset * c) + (_y_offset * (1 - c));
1018 _z_offset = (_tgt_z_offset * c) + (_z_offset * (1 - c));
1021 void FGAIBallistic::formateToAC(double dt){
1023 setTgtOffsets(dt, 25);
1025 setHitchVelocity(dt);
1027 // elapsed time has a random initialisation so that each
1028 // wingman moves differently
1029 _elapsed_time += dt;
1031 // we derive a sine based factor to give us smoothly
1032 // varying error between -1 and 1
1033 double factor = sin(SGMiscd::deg2rad(_elapsed_time * 10));
1034 double r_angle = 5 * factor;
1035 double p_angle = 2.5 * factor;
1036 double h_angle = 5 * factor;
1037 double h_feet = 3 * factor;
1039 pos.setLatitudeDeg(hitchpos.getLatitudeDeg());
1040 pos.setLongitudeDeg(hitchpos.getLongitudeDeg());
1044 if(_ht_agl_ft <= 10) {
1045 _height = userpos.getElevationFt();
1046 } else if (_ht_agl_ft > 10 && _ht_agl_ft <= 150 ) {
1047 setHt(userpos.getElevationFt(), dt, 1.0);
1048 } else if (_ht_agl_ft > 150 && _ht_agl_ft <= 250) {
1049 setHt(hitchpos.getElevationFt()+ h_feet, dt, 0.75);
1051 setHt(hitchpos.getElevationFt()+ h_feet, dt, 0.5);
1053 pos.setElevationFt(_height);
1056 // these calculations are unreliable at slow speeds
1058 setHdg(_azimuth + h_angle, dt, 0.9);
1059 setPch(_elevation + p_angle + _pitch_offset, dt, 0.9);
1061 if (roll <= 115 && roll >= -115)
1062 setBnk(manager->get_user_roll() + r_angle + _roll_offset, dt, 0.5);
1064 roll = manager->get_user_roll() + r_angle + _roll_offset;
1067 setHdg(manager->get_user_heading(), dt, 0.9);
1068 setPch(manager->get_user_pitch() + _pitch_offset, dt, 0.9);
1069 setBnk(manager->get_user_roll() + _roll_offset, dt, 0.9);