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-2007
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/point3d.hxx>
27 #include <simgear/math/sg_random.h>
28 #include <simgear/scene/material/mat.hxx>
29 #include <simgear/math/sg_geodesy.hxx>
31 #include <Scenery/scenery.hxx>
33 #include "AIBallistic.hxx"
35 const double FGAIBallistic::slugs_to_kgs = 14.5939029372;
37 FGAIBallistic::FGAIBallistic() :
38 FGAIBase(otBallistic),
40 _aero_stabilised(false),
49 _report_collision(false),
50 _report_impact(false),
51 _impact_report_node(fgGetNode("/ai/models/model-impact", true)),
52 _external_force(false)
58 FGAIBallistic::~FGAIBallistic() {
61 void FGAIBallistic::readFromScenario(SGPropertyNode* scFileNode) {
65 FGAIBase::readFromScenario(scFileNode);
67 setAzimuth(scFileNode->getDoubleValue("azimuth", 0.0));
68 setElevation(scFileNode->getDoubleValue("elevation", 0.0));
69 setDragArea(scFileNode->getDoubleValue("eda", 0.007));
70 setLife(scFileNode->getDoubleValue("life", 900.0));
71 setBuoyancy(scFileNode->getDoubleValue("buoyancy", 0));
72 setWind_from_east(scFileNode->getDoubleValue("wind_from_east", 0));
73 setWind_from_north(scFileNode->getDoubleValue("wind_from_north", 0));
74 setWind(scFileNode->getBoolValue("wind", false));
75 setRoll(scFileNode->getDoubleValue("roll", 0.0));
76 setCd(scFileNode->getDoubleValue("cd", 0.029));
77 setMass(scFileNode->getDoubleValue("mass", 0.007));
78 setStabilisation(scFileNode->getBoolValue("aero_stabilized", false));
79 setNoRoll(scFileNode->getBoolValue("no-roll", false));
80 setRandom(scFileNode->getBoolValue("random", false));
81 setImpact(scFileNode->getBoolValue("impact", false));
82 setImpactReportNode(scFileNode->getStringValue("impact-reports"));
83 setName(scFileNode->getStringValue("name", "Bomb"));
84 setFuseRange(scFileNode->getDoubleValue("fuse-range", 0.0));
85 setSMPath(scFileNode->getStringValue("submodel-path", ""));
86 setSubID(scFileNode->getIntValue("SubID", 0));
89 bool FGAIBallistic::init(bool search_in_AI_path) {
90 FGAIBase::init(search_in_AI_path);
92 props->setStringValue("material/name", "");
93 props->setStringValue("name", _name.c_str());
94 props->setStringValue("submodels/path", _submodel.c_str());
96 // start with high value so that animations don't trigger yet
106 void FGAIBallistic::bind() {
108 props->tie("sim/time/elapsed-sec",
109 SGRawValueMethods<FGAIBallistic,double>(*this,
110 &FGAIBallistic::_getTime));
111 props->tie("material/load-resistance",
112 SGRawValuePointer<double>(&_load_resistance));
113 props->tie("material/solid",
114 SGRawValuePointer<bool>(&_solid));
115 props->tie("altitude-agl-ft",
116 SGRawValuePointer<double>(&_ht_agl_ft));
119 void FGAIBallistic::unbind() {
120 // FGAIBase::unbind();
121 props->untie("sim/time/elapsed-sec");
122 props->untie("material/load-resistance");
123 props->untie("material/solid");
124 props->untie("altitude-agl-ft");
127 void FGAIBallistic::update(double dt) {
128 FGAIBase::update(dt);
133 void FGAIBallistic::setAzimuth(double az) {
137 void FGAIBallistic::setElevation(double el) {
138 pitch = _elevation = el;
141 void FGAIBallistic::setRoll(double rl) {
145 void FGAIBallistic::setStabilisation(bool val) {
146 _aero_stabilised = val;
149 void FGAIBallistic::setNoRoll(bool nr) {
153 void FGAIBallistic::setDragArea(double a) {
157 void FGAIBallistic::setLife(double seconds) {
161 void FGAIBallistic::setBuoyancy(double fpss) {
165 void FGAIBallistic::setWind_from_east(double fps) {
166 _wind_from_east = fps;
169 void FGAIBallistic::setWind_from_north(double fps) {
170 _wind_from_north = fps;
173 void FGAIBallistic::setWind(bool val) {
177 void FGAIBallistic::setCd(double c) {
181 void FGAIBallistic::setMass(double m) {
185 void FGAIBallistic::setRandom(bool r) {
189 void FGAIBallistic::setImpact(bool i) {
193 void FGAIBallistic::setCollision(bool c) {
194 _report_collision = c;
197 void FGAIBallistic::setExternalForce(bool f) {
201 void FGAIBallistic::setImpactReportNode(const string& path) {
204 _impact_report_node = fgGetNode(path.c_str(), true);
207 void FGAIBallistic::setName(const string& n) {
211 void FGAIBallistic::setSMPath(const string& s) {
215 void FGAIBallistic::setFuseRange(double f) {
219 void FGAIBallistic::setSubID(int i) {
221 //cout << "sub id " << _subID << " name " << _name << endl;
224 void FGAIBallistic::setSubmodel(const string& s) {
228 void FGAIBallistic::setForcePath(const string& p) {
230 if (!_force_path.empty()) {
231 SGPropertyNode *fnode = fgGetNode(_force_path.c_str(), 0, true );
232 _force_node = fnode->getChild("force-lb", 0, true);
233 _force_azimuth_node = fnode->getChild("force-azimuth-deg", 0, true);
234 _force_elevation_node = fnode->getChild("force-elevation-deg", 0, true);
238 void FGAIBallistic::Run(double dt) {
240 //cout << "life timer" <<_name <<" " << _life_timer << dt << endl;
241 if (_life_timer > life)
244 //randomise Cd by +- 5%
246 _Cd = _Cd * 0.95 + (0.05 * sg_random());
248 // Adjust Cd by Mach number. The equations are based on curves
249 // for a conventional shell/bullet (no boat-tail).
253 Cdm = 0.0125 * Mach + _Cd;
254 else if (Mach < 1.2 )
255 Cdm = 0.3742 * pow(Mach, 2) - 0.252 * Mach + 0.0021 + _Cd;
257 Cdm = 0.2965 * pow(Mach, -1.1506) + _Cd;
259 //cout << " Mach , " << Mach << " , Cdm , " << Cdm << " ballistic speed kts //"<< speed << endl;
261 // drag = Cd * 0.5 * rho * speed * speed * drag_area;
262 // rho is adjusted for altitude in void FGAIBase::update,
263 // using Standard Atmosphere (sealevel temperature 15C)
264 // acceleration = drag/mass;
265 // adjust speed by drag
266 speed -= (Cdm * 0.5 * rho * speed * speed * _drag_area/_mass) * dt;
268 // don't let speed become negative
272 double speed_fps = speed * SG_KT_TO_FPS;
275 // calculate vertical and horizontal speed components
279 vs = sin( _elevation * SG_DEGREES_TO_RADIANS ) * speed_fps;
280 hs = cos( _elevation * SG_DEGREES_TO_RADIANS ) * speed_fps;
283 //resolve horizontal speed into north and east components:
284 double speed_north_fps = cos(_azimuth / SG_RADIANS_TO_DEGREES) * hs;
285 double speed_east_fps = sin(_azimuth / SG_RADIANS_TO_DEGREES) * hs;
287 // convert horizontal speed (fps) to degrees per second
288 double speed_north_deg_sec = speed_north_fps / ft_per_deg_lat;
289 double speed_east_deg_sec = speed_east_fps / ft_per_deg_lon;
291 // if wind not required, set to zero
293 _wind_from_north = 0;
297 // convert wind speed (fps) to degrees lat/lon per second
298 double wind_speed_from_north_deg_sec = _wind_from_north / ft_per_deg_lat;
299 double wind_speed_from_east_deg_sec = _wind_from_east / ft_per_deg_lon;
301 //calculate velocity due to external force
302 double force_speed_north_deg_sec = 0;
303 double force_speed_east_deg_sec = 0;
304 double vs_force_fps = 0;
305 double hs_force_fps = 0;
306 double v_force_acc_fpss = 0;
307 double force_speed_north_fps = 0;
308 double force_speed_east_fps = 0;
310 if (_external_force) {
311 SGPropertyNode *n = fgGetNode(_force_path.c_str(), true);
312 double force_lbs = n->getChild("force-lb", 0, true)->getDoubleValue();
313 double force_elevation_deg = n->getChild("force-elevation-deg", 0, true)->getDoubleValue();
314 double force_azimuth_deg = n->getChild("force-azimuth-deg", 0, true)->getDoubleValue();
316 //resolve force into vertical and horizontal components:
317 double v_force_lbs = force_lbs * sin( force_elevation_deg * SG_DEGREES_TO_RADIANS );
318 double h_force_lbs = force_lbs * cos( force_elevation_deg * SG_DEGREES_TO_RADIANS );
320 //acceleration = (force(lbsf)/mass(slugs))
321 v_force_acc_fpss = (v_force_lbs/_mass);
322 double h_force_acc_fpss = (h_force_lbs/_mass);
324 // velocity = acceleration * dt
325 hs_force_fps = h_force_acc_fpss * dt;
327 //resolve horizontal speed into north and east components:
328 force_speed_north_fps = cos(force_azimuth_deg * SG_DEGREES_TO_RADIANS) * hs_force_fps;
329 force_speed_east_fps = sin(force_azimuth_deg * SG_DEGREES_TO_RADIANS) * hs_force_fps;
331 // convert horizontal speed (fps) to degrees per second
332 double force_speed_north_deg_sec = force_speed_north_fps / ft_per_deg_lat;
333 double force_speed_east_deg_sec = force_speed_east_fps / ft_per_deg_lon;
335 //recombine the horizontal velocity components
336 hs = sqrt(((speed_north_fps + force_speed_north_fps) * (speed_north_fps + force_speed_north_fps))
337 + ((speed_east_fps + force_speed_east_fps) * (speed_east_fps + force_speed_east_fps)));
339 /*cout << "mass " << _mass
340 << " force " << force_lbs
341 << " elevation " << force_elevation_deg
342 << " azimuth " << force_azimuth_deg
345 //cout << " _hs_force_fps " << hs_force_fps
346 //<< " force_speed_north_fps " << force_speed_north_fps
347 //<< " force_speed_east_fps " << force_speed_east_fps
348 //<< " speed_north_fps " << speed_north_fps
349 //<< " speed_east_fps " << speed_east_fps
354 pos.setLatitudeDeg( pos.getLatitudeDeg()
355 + (speed_north_deg_sec - wind_speed_from_north_deg_sec + force_speed_north_deg_sec) * dt );
356 pos.setLongitudeDeg( pos.getLongitudeDeg()
357 + (speed_east_deg_sec - wind_speed_from_east_deg_sec + force_speed_east_deg_sec) * dt );
359 // adjust vertical speed for acceleration of gravity, buoyancy, and vertical force
360 //v_force_acc_fpss = 0;
361 vs -= (_gravity - _buoyancy - v_force_acc_fpss) * dt;
363 // adjust altitude (feet) and set new elevation
364 altitude_ft += vs * dt;
365 pos.setElevationFt(altitude_ft);
367 // recalculate elevation and azimuth (velocity vectors)
368 _elevation = atan2( vs, hs ) * SG_RADIANS_TO_DEGREES;
369 _azimuth = atan2((speed_east_fps + force_speed_east_fps),
370 (speed_north_fps + force_speed_north_fps)) * SG_RADIANS_TO_DEGREES;
372 // rationalise azimuth
373 if (_azimuth < 0) _azimuth += 360;
375 if (_aero_stabilised) { // we simulate rotational moment of inertia by using a filter
376 const double coeff = 0.9;
377 double c = dt / (coeff + dt);
379 pitch = (_elevation * c) + (pitch * (1 - c));
381 if (_azimuth - hdg > 180) {
382 hdg = (_azimuth * c) - (hdg * (1 - c));
384 hdg = (_azimuth * c) + (hdg * (1 - c));
389 // recalculate total speed
390 speed = sqrt( vs * vs + hs * hs) / SG_KT_TO_FPS;
392 /*cout << "_elevation " << _elevation
393 << " pitch " << pitch
396 << " speed " << speed
399 //do impacts and collisions
400 if (_report_impact && !_impact_reported)
403 if (_report_collision && !_collision_reported)
406 // set destruction flag if altitude less than sea level -1000
407 if (altitude_ft < -1000.0)
412 double FGAIBallistic::_getTime() const {
413 // cout << "life timer 2" << _life_timer << endl;
417 void FGAIBallistic::handle_impact() {
419 const SGMaterial* material;
421 // try terrain intersection
422 if (!globals->get_scenery()->get_elevation_m(pos.getLatitudeDeg(), pos.getLongitudeDeg(),
423 10000.0, elevation_m, &material))
427 const vector<string> names = material->get_names();
431 mat_name = names[0].c_str();
433 _solid = material->get_solid();
434 _load_resistance = material->get_load_resistance();
435 props->setStringValue("material/name", mat_name.c_str());
436 //cout << "material " << mat_name << " solid " << _solid << " load " << _load_resistance << endl;
439 _ht_agl_ft = pos.getElevationFt() - elevation_m * SG_METER_TO_FEET;
441 if (_ht_agl_ft <= 0) {
442 SG_LOG(SG_GENERAL, SG_DEBUG, "AIBallistic: terrain impact");
443 report_impact(elevation_m);
444 _impact_reported = true;
446 // kill the AIObject if there is no subsubmodel
452 void FGAIBallistic::handle_collision()
454 const FGAIBase *object = manager->calcCollision(pos.getElevationFt(),
455 pos.getLatitudeDeg(),pos.getLongitudeDeg(), _fuse_range);
458 SG_LOG(SG_GENERAL, SG_DEBUG, "AIBallistic: object hit");
459 report_impact(pos.getElevationM(), object);
460 _collision_reported = true;
464 void FGAIBallistic::report_impact(double elevation, const FGAIBase *object)
466 _impact_lat = pos.getLatitudeDeg();
467 _impact_lon = pos.getLongitudeDeg();
468 _impact_elev = elevation;
469 _impact_speed = speed * SG_KT_TO_MPS;
471 _impact_pitch = pitch;
474 SGPropertyNode *n = props->getNode("impact", true);
476 n->setStringValue("type", object->getTypeString());
478 n->setStringValue("type", "terrain");
480 n->setDoubleValue("longitude-deg", _impact_lon);
481 n->setDoubleValue("latitude-deg", _impact_lat);
482 n->setDoubleValue("elevation-m", _impact_elev);
483 n->setDoubleValue("heading-deg", _impact_hdg);
484 n->setDoubleValue("pitch-deg", _impact_pitch);
485 n->setDoubleValue("roll-deg", _impact_roll);
486 n->setDoubleValue("speed-mps", _impact_speed);
488 _impact_report_node->setStringValue(props->getPath());