]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIBallistic.cxx
a4699d28283f7ff02faadfb0224192edab32d3ea
[flightgear.git] / src / AIModel / AIBallistic.cxx
1 // FGAIBallistic - FGAIBase-derived class creates a ballistic object
2 //
3 // Written by David Culp, started November 2003.
4 // - davidculp2@comcast.net
5 //
6 // With major additions by Mathias Froehlich & Vivian Meazza 2004-2007
7 //
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.
12 //
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.
17 //
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.
21
22 #ifdef HAVE_CONFIG_H
23 #  include <config.h>
24 #endif
25
26 #include <simgear/math/point3d.hxx>
27 #include <simgear/math/sg_random.h>
28 #include <simgear/scene/material/mat.hxx>
29 #include <math.h>
30 #include <vector>
31
32 #include <Scenery/scenery.hxx>
33
34 #include "AIBallistic.hxx"
35
36 SG_USING_STD(vector);
37
38 const double FGAIBallistic::slugs_to_kgs = 14.5939029372;
39
40 FGAIBallistic::FGAIBallistic() :
41     FGAIBase(otBallistic),
42     _aero_stabilised(false),
43     _drag_area(0.007),
44     _life_timer(0.0),
45     _gravity(32),
46     //  _buoyancy(64),
47     _ht_agl_ft(0),
48     _load_resistance(0),
49     _solid(false),
50     _impact_data(false),
51     _impact_report_node(0),
52     _impact_energy(0),
53     _impact_speed(0),
54     _impact_lat(0),
55     _impact_lon(0),
56     _impact_elev(0),
57     _mat_name("")
58 {
59     no_roll = false;
60 }
61
62 FGAIBallistic::~FGAIBallistic() {
63 }
64
65 void FGAIBallistic::readFromScenario(SGPropertyNode* scFileNode) {
66     if (!scFileNode)
67         return;
68
69     FGAIBase::readFromScenario(scFileNode);
70
71     setAzimuth(scFileNode->getDoubleValue("azimuth", 0.0));
72     setElevation(scFileNode->getDoubleValue("elevation", 0.0));
73     setDragArea(scFileNode->getDoubleValue("eda", 0.007));
74     setLife(scFileNode->getDoubleValue("life", 900.0));
75     setBuoyancy(scFileNode->getDoubleValue("buoyancy", 0));
76     setWind_from_east(scFileNode->getDoubleValue("wind_from_east", 0));
77     setWind_from_north(scFileNode->getDoubleValue("wind_from_north", 0));
78     setWind(scFileNode->getBoolValue("wind", false));
79     setRoll(scFileNode->getDoubleValue("roll", 0.0));
80     setCd(scFileNode->getDoubleValue("cd", 0.029));
81     setMass(scFileNode->getDoubleValue("mass", 0.007));
82     setStabilisation(scFileNode->getBoolValue("aero_stabilized", false));
83     setNoRoll(scFileNode->getBoolValue("no-roll", false));
84     setRandom(scFileNode->getBoolValue("random", false));
85     setImpact(scFileNode->getBoolValue("impact", false));
86     setImpactReportNode(scFileNode->getStringValue("impact-reports"));
87     setName(scFileNode->getStringValue("name", "Bomb"));
88 }
89
90 bool FGAIBallistic::init(bool search_in_AI_path) {
91     FGAIBase::init(search_in_AI_path);
92
93     props->setStringValue("material/name", _mat_name.c_str());
94     props->setStringValue("name", _name.c_str());
95
96     // start with high value so that animations don't trigger yet
97     _ht_agl_ft = 10000000;
98     hdg = _azimuth;
99     pitch = _elevation;
100     roll = _rotation;
101     Transform();
102     return true;
103 }
104
105 void FGAIBallistic::bind() {
106     //    FGAIBase::bind();
107     props->tie("sim/time/elapsed-sec",
108         SGRawValueMethods<FGAIBallistic,double>(*this,
109         &FGAIBallistic::_getTime));
110     props->tie("material/load-resistance",
111                 SGRawValuePointer<double>(&_load_resistance));
112     props->tie("material/solid",
113                 SGRawValuePointer<bool>(&_solid));
114     props->tie("altitude-agl-ft",
115                 SGRawValuePointer<double>(&_ht_agl_ft));
116     props->tie("impact/latitude-deg",
117                 SGRawValuePointer<double>(&_impact_lat));
118     props->tie("impact/longitude-deg",
119                 SGRawValuePointer<double>(&_impact_lon));
120     props->tie("impact/elevation-m",
121                 SGRawValuePointer<double>(&_impact_elev));
122     props->tie("impact/speed-mps",
123                 SGRawValuePointer<double>(&_impact_speed));
124     props->tie("impact/energy-kJ",
125                 SGRawValuePointer<double>(&_impact_energy));
126 }
127
128 void FGAIBallistic::unbind() {
129     //    FGAIBase::unbind();
130     props->untie("sim/time/elapsed-sec");
131     props->untie("material/load-resistance");
132     props->untie("material/solid");
133     props->untie("altitude-agl-ft");
134     props->untie("impact/latitude-deg");
135     props->untie("impact/longitude-deg");
136     props->untie("impact/elevation-m");
137     props->untie("impact/speed-mps");
138     props->untie("impact/energy-kJ");
139 }
140
141 void FGAIBallistic::update(double dt) {
142     FGAIBase::update(dt);
143     Run(dt);
144     Transform();
145 }
146
147 void FGAIBallistic::setAzimuth(double az) {
148     hdg = _azimuth = az;
149 }
150
151 void FGAIBallistic::setElevation(double el) {
152     pitch = _elevation = el;
153 }
154
155 void FGAIBallistic::setRoll(double rl) {
156     _rotation = rl;
157 }
158
159 void FGAIBallistic::setStabilisation(bool val) {
160     _aero_stabilised = val;
161 }
162
163 void FGAIBallistic::setNoRoll(bool nr) {
164     no_roll = nr;
165 }
166
167 void FGAIBallistic::setDragArea(double a) {
168     _drag_area = a;
169 }
170
171 void FGAIBallistic::setLife(double seconds) {
172     life = seconds;
173 }
174
175 void FGAIBallistic::setBuoyancy(double fpss) {
176     _buoyancy = fpss;
177 }
178
179 void FGAIBallistic::setWind_from_east(double fps) {
180     _wind_from_east = fps;
181 }
182
183 void FGAIBallistic::setWind_from_north(double fps) {
184     _wind_from_north = fps;
185 }
186
187 void FGAIBallistic::setWind(bool val) {
188     _wind = val;
189 }
190
191 void FGAIBallistic::setCd(double c) {
192     _Cd = c;
193 }
194
195 void FGAIBallistic::setMass(double m) {
196     _mass = m;
197 }
198
199 void FGAIBallistic::setRandom(bool r) {
200     _random = r;
201 }
202
203 void FGAIBallistic::setImpact(bool i) {
204     _impact = i;
205 }
206
207 void FGAIBallistic::setImpactReportNode(const string& path) {
208     if (path.empty())
209         _impact_report_node = 0;
210     else
211         _impact_report_node = fgGetNode(path.c_str(), true);
212 }
213
214 void FGAIBallistic::setName(const string& n) {
215     _name = n;
216 }
217
218 void FGAIBallistic::Run(double dt) {
219     _life_timer += dt;
220     //    cout << "life timer 1" << _life_timer <<  dt << endl;
221     if (_life_timer > life)
222         setDie(true);
223
224     double speed_north_deg_sec;
225     double speed_east_deg_sec;
226     double wind_speed_from_north_deg_sec;
227     double wind_speed_from_east_deg_sec;
228     double Cdm;      // Cd adjusted by Mach Number
229
230     //randomise Cd by +- 5%
231     if (_random)
232         _Cd = _Cd * 0.95 + (0.05 * sg_random());
233
234     // Adjust Cd by Mach number. The equations are based on curves
235     // for a conventional shell/bullet (no boat-tail).
236     if (Mach >= 1.2)
237         Cdm = 0.2965 * pow ( Mach, -1.1506 ) + _Cd;
238     else if (Mach >= 0.7)
239         Cdm = 0.3742 * pow ( Mach, 2) - 0.252 * Mach + 0.0021 + _Cd;
240     else
241         Cdm = 0.0125 * Mach + _Cd;
242
243     //cout << " Mach , " << Mach << " , Cdm , " << Cdm << " ballistic speed kts //"<< speed <<  endl;
244
245     // drag = Cd * 0.5 * rho * speed * speed * drag_area;
246     // rho is adjusted for altitude in void FGAIBase::update,
247     // using Standard Atmosphere (sealevel temperature 15C)
248     // acceleration = drag/mass;
249     // adjust speed by drag
250     speed -= (Cdm * 0.5 * rho * speed * speed * _drag_area/_mass) * dt;
251
252     // don't let speed become negative
253     if ( speed < 0.0 )
254         speed = 0.0;
255
256     double speed_fps = speed * SG_KT_TO_FPS;
257
258     // calculate vertical and horizontal speed components
259     vs = sin( pitch * SG_DEGREES_TO_RADIANS ) * speed_fps;
260     double hs = cos( pitch * SG_DEGREES_TO_RADIANS ) * speed_fps;
261
262     // convert horizontal speed (fps) to degrees per second
263     speed_north_deg_sec = cos(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lat;
264     speed_east_deg_sec  = sin(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lon;
265
266     // if wind not required, set to zero
267     if (!_wind) {
268         _wind_from_north = 0;
269         _wind_from_east = 0;
270     }
271
272     // convert wind speed (fps) to degrees per second
273     wind_speed_from_north_deg_sec = _wind_from_north / ft_per_deg_lat;
274     wind_speed_from_east_deg_sec  = _wind_from_east / ft_per_deg_lon;
275
276     // set new position
277     pos.setLatitudeDeg( pos.getLatitudeDeg()
278         + (speed_north_deg_sec - wind_speed_from_north_deg_sec) * dt );
279     pos.setLongitudeDeg( pos.getLongitudeDeg()
280         + (speed_east_deg_sec - wind_speed_from_east_deg_sec) * dt );
281
282     // adjust vertical speed for acceleration of gravity and buoyancy
283     vs -= (_gravity - _buoyancy) * dt;
284
285     // adjust altitude (feet)
286     altitude_ft += vs * dt;
287     pos.setElevationFt(altitude_ft);
288
289     // recalculate pitch (velocity vector) if aerostabilized
290     /*cout << _name << ": " << "aero_stabilised " << _aero_stabilised
291     << " pitch " << pitch <<" vs "  << vs <<endl ;*/
292
293     if (_aero_stabilised)
294         pitch = atan2( vs, hs ) * SG_RADIANS_TO_DEGREES;
295
296     // recalculate total speed
297     speed = sqrt( vs * vs + hs * hs) / SG_KT_TO_FPS;
298
299     if (_impact && !_impact_data && vs < 0)
300         handle_impact();
301
302     // set destruction flag if altitude less than sea level -1000
303     if (altitude_ft < -1000.0)
304         setDie(true);
305
306 }  // end Run
307
308 double FGAIBallistic::_getTime() const {
309     //    cout << "life timer 2" << _life_timer << endl;
310     return _life_timer;
311 }
312
313 void FGAIBallistic::handle_impact() {
314     double elevation_m;
315     const SGMaterial* material;
316
317     // try terrain intersection
318     if (!globals->get_scenery()->get_elevation_m(pos.getLatitudeDeg(), pos.getLongitudeDeg(),
319             10000.0, elevation_m, &material))
320         return;
321
322     if (material) {
323         const vector<string> names = material->get_names();
324
325         if (!names.empty())
326             _mat_name = names[0].c_str();
327
328         _solid = material->get_solid();
329         _load_resistance = material->get_load_resistance();
330         props->setStringValue("material/name", _mat_name.c_str());
331         //cout << "material " << _mat_name << " solid " << _solid << " load " << _load_resistance << endl;
332     }
333
334     _ht_agl_ft = pos.getElevationFt() - elevation_m * SG_METER_TO_FEET;
335
336     // report impact by setting tied variables
337     if (_ht_agl_ft <= 0) {
338         _impact_data = true;
339
340         _impact_lat = pos.getLatitudeDeg();
341         _impact_lon = pos.getLongitudeDeg();
342         _impact_elev = elevation_m;
343         _impact_speed = speed * SG_KT_TO_MPS;
344         _impact_energy = (_mass * slugs_to_kgs) * _impact_speed
345                 * _impact_speed / (2 * 1000);
346
347         if (_impact_report_node)
348             _impact_report_node->setStringValue(props->getPath());
349     }
350 }
351
352 // end AIBallistic
353