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