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>
32 #include <Scenery/scenery.hxx>
34 #include "AIBallistic.hxx"
38 const double FGAIBallistic::slugs_to_kgs = 14.5939029372;
40 FGAIBallistic::FGAIBallistic() :
41 FGAIBase(otBallistic),
42 _aero_stabilised(false),
61 FGAIBallistic::~FGAIBallistic() {
64 void FGAIBallistic::readFromScenario(SGPropertyNode* scFileNode) {
68 FGAIBase::readFromScenario(scFileNode);
70 setAzimuth(scFileNode->getDoubleValue("azimuth", 0.0));
71 setElevation(scFileNode->getDoubleValue("elevation", 0.0));
72 setDragArea(scFileNode->getDoubleValue("eda", 0.007));
73 setLife(scFileNode->getDoubleValue("life", 900.0));
74 setBuoyancy(scFileNode->getDoubleValue("buoyancy", 0));
75 setWind_from_east(scFileNode->getDoubleValue("wind_from_east", 0));
76 setWind_from_north(scFileNode->getDoubleValue("wind_from_north", 0));
77 setWind(scFileNode->getBoolValue("wind", false));
78 setRoll(scFileNode->getDoubleValue("roll", 0.0));
79 setCd(scFileNode->getDoubleValue("cd", 0.029));
80 setMass(scFileNode->getDoubleValue("mass", 0.007));
81 setStabilisation(scFileNode->getBoolValue("aero_stabilized", false));
82 setNoRoll(scFileNode->getBoolValue("no-roll", false));
83 setRandom(scFileNode->getBoolValue("random", false));
84 setImpact(scFileNode->getBoolValue("impact", false));
85 setName(scFileNode->getStringValue("name", "Bomb"));
88 bool FGAIBallistic::init(bool search_in_AI_path) {
89 FGAIBase::init(search_in_AI_path);
91 props->setStringValue("material/name", _mat_name.c_str());
92 props->setStringValue("name", _name.c_str());
94 // start with high value so that animations don't trigger yet
95 _ht_agl_ft = 10000000;
103 void FGAIBallistic::bind() {
105 props->tie("sim/time/elapsed-sec",
106 SGRawValueMethods<FGAIBallistic,double>(*this,
107 &FGAIBallistic::_getTime));
108 props->tie("material/load-resistance",
109 SGRawValuePointer<double>(&_load_resistance));
110 props->tie("material/solid",
111 SGRawValuePointer<bool>(&_solid));
112 props->tie("altitude-agl-ft",
113 SGRawValuePointer<double>(&_ht_agl_ft));
114 props->tie("impact/latitude-deg",
115 SGRawValuePointer<double>(&_impact_lat));
116 props->tie("impact/longitude-deg",
117 SGRawValuePointer<double>(&_impact_lon));
118 props->tie("impact/elevation-m",
119 SGRawValuePointer<double>(&_impact_elev));
120 props->tie("impact/speed-mps",
121 SGRawValuePointer<double>(&_impact_speed));
122 props->tie("impact/energy-kJ",
123 SGRawValuePointer<double>(&_impact_energy));
126 void FGAIBallistic::unbind() {
127 // FGAIBase::unbind();
128 props->untie("sim/time/elapsed-sec");
129 props->untie("material/load-resistance");
130 props->untie("material/solid");
131 props->untie("altitude-agl-ft");
132 props->untie("impact/latitude-deg");
133 props->untie("impact/longitude-deg");
134 props->untie("impact/elevation-m");
135 props->untie("impact/speed-mps");
136 props->untie("impact/energy-kJ");
139 void FGAIBallistic::update(double dt) {
140 FGAIBase::update(dt);
145 void FGAIBallistic::setAzimuth(double az) {
149 void FGAIBallistic::setElevation(double el) {
150 pitch = _elevation = el;
153 void FGAIBallistic::setRoll(double rl) {
157 void FGAIBallistic::setStabilisation(bool val) {
158 _aero_stabilised = val;
161 void FGAIBallistic::setNoRoll(bool nr) {
165 void FGAIBallistic::setDragArea(double a) {
169 void FGAIBallistic::setLife(double seconds) {
173 void FGAIBallistic::setBuoyancy(double fpss) {
177 void FGAIBallistic::setWind_from_east(double fps) {
178 _wind_from_east = fps;
181 void FGAIBallistic::setWind_from_north(double fps) {
182 _wind_from_north = fps;
185 void FGAIBallistic::setWind(bool val) {
189 void FGAIBallistic::setCd(double c) {
193 void FGAIBallistic::setMass(double m) {
197 void FGAIBallistic::setRandom(bool r) {
201 void FGAIBallistic::setImpact(bool i) {
205 void FGAIBallistic::setName(const string& n) {
209 void FGAIBallistic::Run(double dt) {
211 // cout << "life timer 1" << _life_timer << dt << endl;
212 if (_life_timer > life)
215 double speed_north_deg_sec;
216 double speed_east_deg_sec;
217 double wind_speed_from_north_deg_sec;
218 double wind_speed_from_east_deg_sec;
219 double Cdm; // Cd adjusted by Mach Number
221 //randomise Cd by +- 5%
223 _Cd = _Cd * 0.95 + (0.05 * sg_random());
225 // Adjust Cd by Mach number. The equations are based on curves
226 // for a conventional shell/bullet (no boat-tail).
228 Cdm = 0.2965 * pow ( Mach, -1.1506 ) + _Cd;
229 else if (Mach >= 0.7)
230 Cdm = 0.3742 * pow ( Mach, 2) - 0.252 * Mach + 0.0021 + _Cd;
232 Cdm = 0.0125 * Mach + _Cd;
234 //cout << " Mach , " << Mach << " , Cdm , " << Cdm << " ballistic speed kts //"<< speed << endl;
236 // drag = Cd * 0.5 * rho * speed * speed * drag_area;
237 // rho is adjusted for altitude in void FGAIBase::update,
238 // using Standard Atmosphere (sealevel temperature 15C)
239 // acceleration = drag/mass;
240 // adjust speed by drag
241 speed -= (Cdm * 0.5 * rho * speed * speed * _drag_area/_mass) * dt;
243 // don't let speed become negative
247 double speed_fps = speed * SG_KT_TO_FPS;
249 // calculate vertical and horizontal speed components
250 vs = sin( pitch * SG_DEGREES_TO_RADIANS ) * speed_fps;
251 double hs = cos( pitch * SG_DEGREES_TO_RADIANS ) * speed_fps;
253 // convert horizontal speed (fps) to degrees per second
254 speed_north_deg_sec = cos(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lat;
255 speed_east_deg_sec = sin(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lon;
257 // if wind not required, set to zero
259 _wind_from_north = 0;
263 // convert wind speed (fps) to degrees per second
264 wind_speed_from_north_deg_sec = _wind_from_north / ft_per_deg_lat;
265 wind_speed_from_east_deg_sec = _wind_from_east / ft_per_deg_lon;
268 pos.setLatitudeDeg( pos.getLatitudeDeg()
269 + (speed_north_deg_sec - wind_speed_from_north_deg_sec) * dt );
270 pos.setLongitudeDeg( pos.getLongitudeDeg()
271 + (speed_east_deg_sec - wind_speed_from_east_deg_sec) * dt );
273 // adjust vertical speed for acceleration of gravity and buoyancy
274 vs -= (_gravity - _buoyancy) * dt;
276 // adjust altitude (feet)
277 altitude_ft += vs * dt;
278 pos.setElevationFt(altitude_ft);
280 // recalculate pitch (velocity vector) if aerostabilized
281 /*cout << _name << ": " << "aero_stabilised " << _aero_stabilised
282 << " pitch " << pitch <<" vs " << vs <<endl ;*/
284 if (_aero_stabilised)
285 pitch = atan2( vs, hs ) * SG_RADIANS_TO_DEGREES;
287 // recalculate total speed
288 speed = sqrt( vs * vs + hs * hs) / SG_KT_TO_FPS;
290 if (_impact && !_impact_data && vs < 0)
293 // set destruction flag if altitude less than sea level -1000
294 if (altitude_ft < -1000.0)
299 double FGAIBallistic::_getTime() const {
300 // cout << "life timer 2" << _life_timer << endl;
304 void FGAIBallistic::handle_impact() {
306 const SGMaterial* material;
308 // try terrain intersection
309 if (!globals->get_scenery()->get_elevation_m(pos.getLatitudeDeg(), pos.getLongitudeDeg(),
310 10000.0, elevation_m, &material))
314 const vector<string> names = material->get_names();
317 _mat_name = names[0].c_str();
319 _solid = material->get_solid();
320 _load_resistance = material->get_load_resistance();
321 props->setStringValue("material/name", _mat_name.c_str());
322 //cout << "material " << _mat_name << " solid " << _solid << " load " << _load_resistance << endl;
325 _ht_agl_ft = pos.getElevationFt() - elevation_m * SG_METER_TO_FEET;
327 // report impact by setting tied variables
328 if (_ht_agl_ft <= 0) {
329 _impact_lat = pos.getLatitudeDeg();
330 _impact_lon = pos.getLongitudeDeg();
331 _impact_elev = elevation_m;
332 _impact_speed = speed * SG_KT_TO_MPS;
333 _impact_energy = (_mass * slugs_to_kgs) * _impact_speed
334 * _impact_speed / (2 * 1000);
336 props->setBoolValue("impact/signal", true); // for listeners