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() : FGAIBase(otBallistic) {
46 aero_stabilised = false;
59 FGAIBallistic::~FGAIBallistic() {
62 void FGAIBallistic::readFromScenario(SGPropertyNode* scFileNode) {
66 FGAIBase::readFromScenario(scFileNode);
68 setAzimuth(scFileNode->getDoubleValue("azimuth", 0.0));
69 setElevation(scFileNode->getDoubleValue("elevation", 0.0));
70 setDragArea(scFileNode->getDoubleValue("eda", 0.007));
71 setLife(scFileNode->getDoubleValue("life", 900.0));
72 setBuoyancy(scFileNode->getDoubleValue("buoyancy", 0));
73 setWind_from_east(scFileNode->getDoubleValue("wind_from_east", 0));
74 setWind_from_north(scFileNode->getDoubleValue("wind_from_north", 0));
75 setWind(scFileNode->getBoolValue("wind", false));
76 setRoll(scFileNode->getDoubleValue("roll", 0.0));
77 setCd(scFileNode->getDoubleValue("cd", 0.029));
78 setMass(scFileNode->getDoubleValue("mass", 0.007));
79 setStabilisation(scFileNode->getBoolValue("aero_stabilized", false));
80 setNoRoll(scFileNode->getBoolValue("no-roll", false));
81 setRandom(scFileNode->getBoolValue("random", false));
82 setImpact(scFileNode->getBoolValue("impact", false));
83 setName(scFileNode->getStringValue("name", "Bomb"));
86 bool FGAIBallistic::init(bool search_in_AI_path) {
87 FGAIBase::init(search_in_AI_path);
89 props->setStringValue("material/name", mat_name.c_str());
90 props->setStringValue("name", name.c_str());
99 void FGAIBallistic::bind() {
101 props->tie("sim/time/elapsed-sec",
102 SGRawValueMethods<FGAIBallistic,double>(*this,
103 &FGAIBallistic::_getTime));
104 props->tie("material/load-resistance",
105 SGRawValuePointer<double>(&load_resistance));
106 props->tie("material/solid",
107 SGRawValuePointer<bool>(&solid));
108 props->tie("altitude-agl-ft",
109 SGRawValuePointer<double>(&ht_agl_ft));
110 props->tie("impact/latitude-deg",
111 SGRawValuePointer<double>(&impact_lat));
112 props->tie("impact/longitude-deg",
113 SGRawValuePointer<double>(&impact_lon));
114 props->tie("impact/elevation-m",
115 SGRawValuePointer<double>(&impact_elev));
116 props->tie("impact/speed-mps",
117 SGRawValuePointer<double>(&impact_speed));
118 props->tie("impact/energy-kJ",
119 SGRawValuePointer<double>(&impact_energy));
122 void FGAIBallistic::unbind() {
123 // FGAIBase::unbind();
124 props->untie("sim/time/elapsed-sec");
125 props->untie("material/load-resistance");
126 props->untie("material/solid");
127 props->untie("altitude-agl-ft");
128 props->untie("impact/latitude-deg");
129 props->untie("impact/longitude-deg");
130 props->untie("impact/elevation-m");
131 props->untie("impact/speed-mps");
132 props->untie("impact/energy-kJ");
135 void FGAIBallistic::update(double dt) {
136 FGAIBase::update(dt);
141 void FGAIBallistic::setAzimuth(double az) {
145 void FGAIBallistic::setElevation(double el) {
146 pitch = elevation = el;
149 void FGAIBallistic::setRoll(double rl) {
153 void FGAIBallistic::setStabilisation(bool val) {
154 aero_stabilised = val;
157 void FGAIBallistic::setNoRoll(bool nr) {
161 void FGAIBallistic::setDragArea(double a) {
165 void FGAIBallistic::setLife(double seconds) {
169 void FGAIBallistic::setBuoyancy(double fpss) {
173 void FGAIBallistic::setWind_from_east(double fps) {
174 wind_from_east = fps;
177 void FGAIBallistic::setWind_from_north(double fps) {
178 wind_from_north = fps;
181 void FGAIBallistic::setWind(bool val) {
185 void FGAIBallistic::setCd(double c) {
189 void FGAIBallistic::setMass(double m) {
193 void FGAIBallistic::setRandom(bool r) {
197 void FGAIBallistic::setImpact(bool i) {
201 void FGAIBallistic::setName(const string& n) {
205 void FGAIBallistic::Run(double dt) {
207 // cout << "life timer 1" << life_timer << dt << endl;
208 if (life_timer > life) setDie(true);
210 double speed_north_deg_sec;
211 double speed_east_deg_sec;
212 double wind_speed_from_north_deg_sec;
213 double wind_speed_from_east_deg_sec;
214 double Cdm; // Cd adjusted by Mach Number
216 //randomise Cd by +- 5%
218 Cd = Cd * 0.95 + (0.05 * sg_random());
220 // Adjust Cd by Mach number. The equations are based on curves
221 // for a conventional shell/bullet (no boat-tail).
223 Cdm = 0.0125 * Mach + Cd;
224 else if ( 0.7 < Mach && Mach < 1.2 )
225 Cdm = 0.3742 * pow ( Mach, 2) - 0.252 * Mach + 0.0021 + Cd;
227 Cdm = 0.2965 * pow ( Mach, -1.1506 ) + Cd;
229 //cout << " Mach , " << Mach << " , Cdm , " << Cdm << " ballistic speed kts //"<< speed << endl;
231 // drag = Cd * 0.5 * rho * speed * speed * drag_area;
232 // rho is adjusted for altitude in void FGAIBase::update,
233 // using Standard Atmosphere (sealevel temperature 15C)
234 // acceleration = drag/mass;
235 // adjust speed by drag
236 speed -= (Cdm * 0.5 * rho * speed * speed * drag_area/mass) * dt;
238 // don't let speed become negative
242 double speed_fps = speed * SG_KT_TO_FPS;
244 // calculate vertical and horizontal speed components
245 vs = sin( pitch * SG_DEGREES_TO_RADIANS ) * speed_fps;
246 double hs = cos( pitch * SG_DEGREES_TO_RADIANS ) * speed_fps;
248 // convert horizontal speed (fps) to degrees per second
249 speed_north_deg_sec = cos(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lat;
250 speed_east_deg_sec = sin(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lon;
252 // if wind not required, set to zero
258 // convert wind speed (fps) to degrees per second
259 wind_speed_from_north_deg_sec = wind_from_north / ft_per_deg_lat;
260 wind_speed_from_east_deg_sec = wind_from_east / ft_per_deg_lon;
263 pos.setLatitudeDeg( pos.getLatitudeDeg()
264 + (speed_north_deg_sec - wind_speed_from_north_deg_sec) * dt );
265 pos.setLongitudeDeg( pos.getLongitudeDeg()
266 + (speed_east_deg_sec - wind_speed_from_east_deg_sec) * dt );
268 // adjust vertical speed for acceleration of gravity and buoyancy
269 vs -= (gravity - buoyancy) * dt;
271 // adjust altitude (feet)
272 altitude_ft += vs * dt;
273 pos.setElevationFt(altitude_ft);
275 // recalculate pitch (velocity vector) if aerostabilized
276 /*cout << name << ": " << "aero_stabilised " << aero_stabilised
277 << " pitch " << pitch <<" vs " << vs <<endl ;*/
280 pitch = atan2( vs, hs ) * SG_RADIANS_TO_DEGREES;
282 // recalculate total speed
283 speed = sqrt( vs * vs + hs * hs) / SG_KT_TO_FPS;
285 if (impact && !impact_data && vs < 0)
288 // set destruction flag if altitude less than sea level -1000
289 if (altitude_ft < -1000.0)
294 double FGAIBallistic::_getTime() const {
295 // cout << "life timer 2" << life_timer << endl;
299 void FGAIBallistic::handle_impact() {
301 const SGMaterial* material;
303 // try terrain intersection
304 if (!globals->get_scenery()->get_elevation_m(pos.getLatitudeDeg(), pos.getLongitudeDeg(),
305 10000.0, elevation_m, &material))
309 const vector<string> names = material->get_names();
312 mat_name = names[0].c_str();
314 solid = material->get_solid();
315 load_resistance = material->get_load_resistance();
316 props->setStringValue("material/name", mat_name.c_str());
317 //cout << "material " << mat_name << " solid " << solid << " load " << load_resistance << endl;
320 ht_agl_ft = pos.getElevationFt() - elevation_m * SG_METER_TO_FEET;
322 // report impact by setting tied variables
323 if (ht_agl_ft <= 0) {
324 impact_lat = pos.getLatitudeDeg();
325 impact_lon = pos.getLongitudeDeg();
326 impact_elev = elevation_m;
327 impact_speed = speed * SG_KT_TO_MPS;
328 impact_energy = (mass * slugs_to_kgs) * impact_speed
329 * impact_speed / (2 * 1000);
331 props->setBoolValue("impact/signal", true); // for listeners