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