]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIBallistic.cxx
4cd14b43a88f9c87d5eb654b122cf710ad3024a1
[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() : FGAIBase(otBallistic) {
41     drag_area = 0.007;
42     life_timer = 0.0;
43     gravity = 32;
44     //  buoyancy = 64;
45     no_roll = false;
46     aero_stabilised = false;
47     ht_agl_ft = 0;
48     impact_data = false;
49     impact_energy = 0;
50     impact_speed = 0;
51     impact_lat = 0;
52     impact_lon = 0;
53     impact_elev = 0;
54     load_resistance = 0;
55     solid = false;
56     mat_name = "";
57 }
58
59 FGAIBallistic::~FGAIBallistic() {
60 }
61
62 void FGAIBallistic::readFromScenario(SGPropertyNode* scFileNode) {
63     if (!scFileNode)
64         return;
65
66     FGAIBase::readFromScenario(scFileNode);
67
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"));
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
92     hdg = azimuth;
93     pitch = elevation;
94     roll = rotation;
95     Transform();
96     return true;
97 }
98
99 void FGAIBallistic::bind() {
100     //    FGAIBase::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));
120 }
121
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");
133 }
134
135 void FGAIBallistic::update(double dt) {
136     FGAIBase::update(dt);
137     Run(dt);
138     Transform();
139 }
140
141 void FGAIBallistic::setAzimuth(double az) {
142     hdg = azimuth = az;
143 }
144
145 void FGAIBallistic::setElevation(double el) {
146     pitch = elevation = el;
147 }
148
149 void FGAIBallistic::setRoll(double rl) {
150     rotation = rl;
151 }
152
153 void FGAIBallistic::setStabilisation(bool val) {
154     aero_stabilised = val;
155 }
156
157 void FGAIBallistic::setNoRoll(bool nr) {
158     no_roll = nr;
159 }
160
161 void FGAIBallistic::setDragArea(double a) {
162     drag_area = a;
163 }
164
165 void FGAIBallistic::setLife(double seconds) {
166     life = seconds;
167 }
168
169 void FGAIBallistic::setBuoyancy(double fpss) {
170     buoyancy = fpss;
171 }
172
173 void FGAIBallistic::setWind_from_east(double fps) {
174     wind_from_east = fps;
175 }
176
177 void FGAIBallistic::setWind_from_north(double fps) {
178     wind_from_north = fps;
179 }
180
181 void FGAIBallistic::setWind(bool val) {
182     wind = val;
183 }
184
185 void FGAIBallistic::setCd(double c) {
186     Cd = c;
187 }
188
189 void FGAIBallistic::setMass(double m) {
190     mass = m;
191 }
192
193 void FGAIBallistic::setRandom(bool r) {
194     random = r;
195 }
196
197 void FGAIBallistic::setImpact(bool i) {
198     impact = i;
199 }
200
201 void FGAIBallistic::setName(const string& n) {
202     name = n;
203 }
204
205 void FGAIBallistic::Run(double dt) {
206     life_timer += dt;
207     //    cout << "life timer 1" << life_timer <<  dt << endl;
208     if (life_timer > life) setDie(true);
209
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
215
216     //randomise Cd by +- 5%
217     if (random)
218         Cd = Cd * 0.95 + (0.05 * sg_random());
219
220     // Adjust Cd by Mach number. The equations are based on curves
221     // for a conventional shell/bullet (no boat-tail).
222     if ( Mach < 0.7 )
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;
226     else
227         Cdm = 0.2965 * pow ( Mach, -1.1506 ) + Cd;
228
229     //cout << " Mach , " << Mach << " , Cdm , " << Cdm << " ballistic speed kts //"<< speed <<  endl;
230
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;
237
238     // don't let speed become negative
239     if ( speed < 0.0 )
240         speed = 0.0;
241
242     double speed_fps = speed * SG_KT_TO_FPS;
243
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;
247
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;
251
252     // if wind not required, set to zero
253     if (!wind) {
254         wind_from_north = 0;
255         wind_from_east = 0;
256     }
257
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;
261
262     // set new position
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 );
267
268     // adjust vertical speed for acceleration of gravity and buoyancy
269     vs -= (gravity - buoyancy) * dt;
270
271     // adjust altitude (feet)
272     altitude_ft += vs * dt;
273     pos.setElevationFt(altitude_ft);
274
275     // recalculate pitch (velocity vector) if aerostabilized
276     /*cout << name << ": " << "aero_stabilised " << aero_stabilised
277     << " pitch " << pitch <<" vs "  << vs <<endl ;*/
278
279     if (aero_stabilised)
280         pitch = atan2( vs, hs ) * SG_RADIANS_TO_DEGREES;
281
282     // recalculate total speed
283     speed = sqrt( vs * vs + hs * hs) / SG_KT_TO_FPS;
284
285     if (impact && !impact_data && vs < 0)
286         handle_impact();
287
288     // set destruction flag if altitude less than sea level -1000
289     if (altitude_ft < -1000.0)
290         setDie(true);
291
292 }  // end Run
293
294 double FGAIBallistic::_getTime() const {
295     //    cout << "life timer 2" << life_timer << endl;
296     return life_timer;
297 }
298
299 void FGAIBallistic::handle_impact() {
300     double elevation_m;
301     const SGMaterial* material;
302
303     // try terrain intersection
304     if (!globals->get_scenery()->get_elevation_m(pos.getLatitudeDeg(), pos.getLongitudeDeg(),
305             10000.0, elevation_m, &material))
306         return;
307
308     if (material) {
309         const vector<string> names = material->get_names();
310
311         if (!names.empty())
312             mat_name = names[0].c_str();
313
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;
318     }
319
320     ht_agl_ft = pos.getElevationFt() - elevation_m * SG_METER_TO_FEET;
321
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);
330
331         props->setBoolValue("impact/signal", true); // for listeners
332         impact_data = true;
333     }
334 }
335
336 // end AIBallistic
337