]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIBallistic.cxx
c42d0d775c3d936890efb7e3d71b7d2591c91b75
[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 <simgear/math/sg_geodesy.hxx>
30
31 #include <Scenery/scenery.hxx>
32
33 #include "AIBallistic.hxx"
34
35 const double FGAIBallistic::slugs_to_kgs = 14.5939029372;
36
37 FGAIBallistic::FGAIBallistic() :
38     FGAIBase(otBallistic),
39     _elevation(0),
40     _aero_stabilised(false),
41     _drag_area(0.007),
42     _life_timer(0.0),
43     _gravity(32),
44     _buoyancy(0),
45     _random(false),
46     _ht_agl_ft(0),
47     _load_resistance(0),
48     _solid(false),
49     _report_collision(false),
50     _report_impact(false),
51     _impact_report_node(fgGetNode("/ai/models/model-impact", true))
52 {
53     no_roll = false;
54 }
55
56 FGAIBallistic::~FGAIBallistic() {
57 }
58
59 void FGAIBallistic::readFromScenario(SGPropertyNode* scFileNode) {
60     if (!scFileNode)
61         return;
62
63     FGAIBase::readFromScenario(scFileNode);
64
65     setAzimuth(scFileNode->getDoubleValue("azimuth", 0.0));
66     setElevation(scFileNode->getDoubleValue("elevation", 0.0));
67     setDragArea(scFileNode->getDoubleValue("eda", 0.007));
68     setLife(scFileNode->getDoubleValue("life", 900.0));
69     setBuoyancy(scFileNode->getDoubleValue("buoyancy", 0));
70     setWind_from_east(scFileNode->getDoubleValue("wind_from_east", 0));
71     setWind_from_north(scFileNode->getDoubleValue("wind_from_north", 0));
72     setWind(scFileNode->getBoolValue("wind", false));
73     setRoll(scFileNode->getDoubleValue("roll", 0.0));
74     setCd(scFileNode->getDoubleValue("cd", 0.029));
75     setMass(scFileNode->getDoubleValue("mass", 0.007));
76     setStabilisation(scFileNode->getBoolValue("aero_stabilized", false));
77     setNoRoll(scFileNode->getBoolValue("no-roll", false));
78     setRandom(scFileNode->getBoolValue("random", false));
79     setImpact(scFileNode->getBoolValue("impact", false));
80     setImpactReportNode(scFileNode->getStringValue("impact-reports"));
81     setName(scFileNode->getStringValue("name", "Bomb"));
82     setFuseRange(scFileNode->getDoubleValue("fuse-range", 0.0));
83     setSMPath(scFileNode->getStringValue("submodel-path", ""));
84     setSubID(scFileNode->getIntValue("SubID", 0));
85 }
86
87 bool FGAIBallistic::init(bool search_in_AI_path) {
88     FGAIBase::init(search_in_AI_path);
89
90     props->setStringValue("material/name", "");
91     props->setStringValue("name", _name.c_str());
92     props->setStringValue("submodels/path", _submodel.c_str());
93
94     // start with high value so that animations don't trigger yet
95     _ht_agl_ft = 1e10;
96     hdg = _azimuth;
97     pitch = _elevation;
98     roll = _rotation;
99     Transform();
100
101     return true;
102 }
103
104 void FGAIBallistic::bind() {
105     //    FGAIBase::bind();
106     props->tie("sim/time/elapsed-sec",
107         SGRawValueMethods<FGAIBallistic,double>(*this,
108         &FGAIBallistic::_getTime));
109     props->tie("material/load-resistance",
110                 SGRawValuePointer<double>(&_load_resistance));
111     props->tie("material/solid",
112                 SGRawValuePointer<bool>(&_solid));
113     props->tie("altitude-agl-ft",
114                 SGRawValuePointer<double>(&_ht_agl_ft));
115 }
116
117 void FGAIBallistic::unbind() {
118     //    FGAIBase::unbind();
119     props->untie("sim/time/elapsed-sec");
120     props->untie("material/load-resistance");
121     props->untie("material/solid");
122     props->untie("altitude-agl-ft");
123 }
124
125 void FGAIBallistic::update(double dt) {
126     FGAIBase::update(dt);
127     Run(dt);
128     Transform();
129 }
130
131 void FGAIBallistic::setAzimuth(double az) {
132     hdg = _azimuth = az;
133 }
134
135 void FGAIBallistic::setElevation(double el) {
136     pitch = _elevation = el;
137 }
138
139 void FGAIBallistic::setRoll(double rl) {
140     _rotation = rl;
141 }
142
143 void FGAIBallistic::setStabilisation(bool val) {
144     _aero_stabilised = val;
145 }
146
147 void FGAIBallistic::setNoRoll(bool nr) {
148     no_roll = nr;
149 }
150
151 void FGAIBallistic::setDragArea(double a) {
152     _drag_area = a;
153 }
154
155 void FGAIBallistic::setLife(double seconds) {
156     life = seconds;
157 }
158
159 void FGAIBallistic::setBuoyancy(double fpss) {
160     _buoyancy = fpss;
161 }
162
163 void FGAIBallistic::setWind_from_east(double fps) {
164     _wind_from_east = fps;
165 }
166
167 void FGAIBallistic::setWind_from_north(double fps) {
168     _wind_from_north = fps;
169 }
170
171 void FGAIBallistic::setWind(bool val) {
172     _wind = val;
173 }
174
175 void FGAIBallistic::setCd(double c) {
176     _Cd = c;
177 }
178
179 void FGAIBallistic::setMass(double m) {
180     _mass = m;
181 }
182
183 void FGAIBallistic::setRandom(bool r) {
184     _random = r;
185 }
186
187 void FGAIBallistic::setImpact(bool i) {
188     _report_impact = i;
189 }
190
191 void FGAIBallistic::setCollision(bool c) {
192     _report_collision = c;
193 }
194
195 void FGAIBallistic::setImpactReportNode(const string& path) {
196     if (!path.empty())
197         _impact_report_node = fgGetNode(path.c_str(), true);
198 }
199
200 void FGAIBallistic::setName(const string& n) {
201     _name = n;
202 }
203
204 void FGAIBallistic::setSMPath(const string& s) {
205     _submodel = s;
206 }
207
208 void FGAIBallistic::setFuseRange(double f) {
209     _fuse_range = f;
210 }
211
212 void FGAIBallistic::setSubID(int i) {
213     _subID = i;
214     //cout << "sub id " << _subID << " name " << _name << endl;
215 }
216
217 void FGAIBallistic::setSubmodel(const string& s) {
218     _submodel = s;
219 }
220
221 void FGAIBallistic::Run(double dt) {
222     _life_timer += dt;
223      //cout << "life timer" <<_name <<" " << _life_timer <<  dt << endl;
224     if (_life_timer > life)
225         setDie(true);
226
227     //randomise Cd by +- 5%
228     if (_random)
229         _Cd = _Cd * 0.95 + (0.05 * sg_random());
230
231     // Adjust Cd by Mach number. The equations are based on curves
232     // for a conventional shell/bullet (no boat-tail).
233     double Cdm;
234
235     if (Mach < 0.7)
236         Cdm = 0.0125 * Mach + _Cd;
237     else if (Mach < 1.2 )
238         Cdm = 0.3742 * pow(Mach, 2) - 0.252 * Mach + 0.0021 + _Cd;
239     else
240         Cdm = 0.2965 * pow(Mach, -1.1506) + _Cd;
241
242     //cout << " Mach , " << Mach << " , Cdm , " << Cdm << " ballistic speed kts //"<< speed <<  endl;
243
244     // drag = Cd * 0.5 * rho * speed * speed * drag_area;
245     // rho is adjusted for altitude in void FGAIBase::update,
246     // using Standard Atmosphere (sealevel temperature 15C)
247     // acceleration = drag/mass;
248     // adjust speed by drag
249     speed -= (Cdm * 0.5 * rho * speed * speed * _drag_area/_mass) * dt;
250
251     // don't let speed become negative
252     if ( speed < 0.0 )
253         speed = 0.0;
254
255     double speed_fps = speed * SG_KT_TO_FPS;
256     double hs;
257
258     // calculate vertical and horizontal speed components
259     if (speed == 0.0) {
260         hs = vs = 0.0;
261     } else {
262         vs = sin( _elevation * SG_DEGREES_TO_RADIANS ) * speed_fps;
263         hs = cos( _elevation * SG_DEGREES_TO_RADIANS ) * speed_fps;
264     }
265
266     // convert horizontal speed (fps) to degrees per second
267     double speed_north_deg_sec = cos(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lat;
268     double speed_east_deg_sec  = sin(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lon;
269
270     // if wind not required, set to zero
271     if (!_wind) {
272         _wind_from_north = 0;
273         _wind_from_east = 0;
274     }
275
276     // convert wind speed (fps) to degrees per second
277     double wind_speed_from_north_deg_sec = _wind_from_north / ft_per_deg_lat;
278     double wind_speed_from_east_deg_sec  = _wind_from_east / ft_per_deg_lon;
279
280     // set new position
281     pos.setLatitudeDeg( pos.getLatitudeDeg()
282         + (speed_north_deg_sec - wind_speed_from_north_deg_sec) * dt );
283     pos.setLongitudeDeg( pos.getLongitudeDeg()
284         + (speed_east_deg_sec - wind_speed_from_east_deg_sec) * dt );
285
286     // adjust vertical speed for acceleration of gravity and buoyancy
287     vs -= (_gravity - _buoyancy) * dt;
288
289     // adjust altitude (feet)
290     altitude_ft += vs * dt;
291     pos.setElevationFt(altitude_ft);
292
293     // recalculate elevation (velocity vector) if aerostabilized
294     /*cout << _name << ": " << "aero_stabilised " << _aero_stabilised
295     << " pitch " << pitch <<" vs "  << vs <<endl ;*/
296
297     if (_aero_stabilised) { // we simulate rotational moment of inertia by using a filter
298         const double coeff = 0.9;
299         double c = dt / (coeff + dt);
300         //cout << "c " << c << endl;
301         _elevation = atan2( vs, hs ) * SG_RADIANS_TO_DEGREES;
302         pitch = (_elevation * c) + (pitch * (1 - c));
303     }
304
305     // recalculate total speed
306     speed = sqrt( vs * vs + hs * hs) / SG_KT_TO_FPS;
307
308     //do impacts and collisions
309     if (_report_impact && !_impact_reported)
310         handle_impact();
311
312     if (_report_collision && !_collision_reported)
313         handle_collision();
314
315     // set destruction flag if altitude less than sea level -1000
316     if (altitude_ft < -1000.0)
317         setDie(true);
318
319 }  // end Run
320
321 double FGAIBallistic::_getTime() const {
322     //    cout << "life timer 2" << _life_timer << endl;
323     return _life_timer;
324 }
325
326 void FGAIBallistic::handle_impact() {
327     double elevation_m;
328     const SGMaterial* material;
329
330     // try terrain intersection
331     if (!globals->get_scenery()->get_elevation_m(pos.getLatitudeDeg(), pos.getLongitudeDeg(),
332             10000.0, elevation_m, &material))
333         return;
334
335     if (material) {
336         const vector<string> names = material->get_names();
337         string mat_name;
338
339         if (!names.empty())
340             mat_name = names[0].c_str();
341
342         _solid = material->get_solid();
343         _load_resistance = material->get_load_resistance();
344         props->setStringValue("material/name", mat_name.c_str());
345         //cout << "material " << mat_name << " solid " << _solid << " load " << _load_resistance << endl;
346     }
347
348     _ht_agl_ft = pos.getElevationFt() - elevation_m * SG_METER_TO_FEET;
349
350     if (_ht_agl_ft <= 0) {
351         SG_LOG(SG_GENERAL, SG_DEBUG, "AIBallistic: terrain impact");
352         report_impact(elevation_m);
353         _impact_reported = true;
354
355         // kill the AIObject if there is no subsubmodel
356         if (_subID == 0)
357             setDie(true);
358     }
359 }
360
361 void FGAIBallistic::handle_collision()
362 {
363     const FGAIBase *object = manager->calcCollision(pos.getElevationFt(),
364             pos.getLatitudeDeg(),pos.getLongitudeDeg(), _fuse_range);
365
366     if (object) {
367         SG_LOG(SG_GENERAL, SG_DEBUG, "AIBallistic: object hit");
368         report_impact(pos.getElevationM(), object);
369         _collision_reported = true;
370     }
371 }
372
373 void FGAIBallistic::report_impact(double elevation, const FGAIBase *object)
374 {
375     _impact_lat    = pos.getLatitudeDeg();
376     _impact_lon    = pos.getLongitudeDeg();
377     _impact_elev   = elevation;
378     _impact_speed  = speed * SG_KT_TO_MPS;
379     _impact_hdg    = hdg;
380     _impact_pitch  = pitch;
381     _impact_roll   = roll;
382
383     SGPropertyNode *n = props->getNode("impact", true);
384     if (object)
385         n->setStringValue("type", object->getTypeString());
386     else
387         n->setStringValue("type", "terrain");
388
389     n->setDoubleValue("longitude-deg", _impact_lon);
390     n->setDoubleValue("latitude-deg", _impact_lat);
391     n->setDoubleValue("elevation-m", _impact_elev);
392     n->setDoubleValue("heading-deg", _impact_hdg);
393     n->setDoubleValue("pitch-deg", _impact_pitch);
394     n->setDoubleValue("roll-deg", _impact_roll);
395     n->setDoubleValue("speed-mps", _impact_speed);
396
397     _impact_report_node->setStringValue(props->getPath());
398 }
399
400 // end AIBallistic
401