]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIBallistic.cxx
Mathias Froehlich:
[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 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License as
8 // published by the Free Software Foundation; either version 2 of the
9 // License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23
24 #include <simgear/math/point3d.hxx>
25 #include <math.h>
26
27 #include "AIBallistic.hxx"
28
29
30 FGAIBallistic::FGAIBallistic() : FGAIBase(otBallistic) {
31     drag_area = 0.007;
32     life_timer = 0.0;
33     gravity = 32;
34 //  buoyancy = 64;
35     no_roll = false;
36     aero_stabilised = false;
37 }
38
39 FGAIBallistic::~FGAIBallistic() {
40 }
41
42 void FGAIBallistic::readFromScenario(SGPropertyNode* scFileNode) {
43   if (!scFileNode)
44     return;
45
46   FGAIBase::readFromScenario(scFileNode);
47
48   setAzimuth(scFileNode->getDoubleValue("azimuth", 0.0));
49   setElevation(scFileNode->getDoubleValue("elevation", 0.0));
50   setDragArea(scFileNode->getDoubleValue("eda", 0.007));
51   setLife(scFileNode->getDoubleValue("life", 900.0));
52   setBuoyancy(scFileNode->getDoubleValue("buoyancy", 0));
53   setWind_from_east(scFileNode->getDoubleValue("wind_from_east", 0));
54   setWind_from_north(scFileNode->getDoubleValue("wind_from_north", 0));
55   setWind(scFileNode->getBoolValue("wind", false));
56   setRoll(scFileNode->getDoubleValue("roll", 0.0));
57   setCd(scFileNode->getDoubleValue("cd", 0.029));
58   setMass(scFileNode->getDoubleValue("mass", 0.007));
59   setStabilisation(scFileNode->getBoolValue("aero_stabilized", false));
60 }
61
62 bool FGAIBallistic::init() {
63    FGAIBase::init();
64    hdg = azimuth;
65    pitch = elevation;
66    roll = rotation;
67    Transform();
68    return true;
69 }
70
71 void FGAIBallistic::bind() {
72 //    FGAIBase::bind();
73    props->tie("sim/time/elapsed-sec",
74                SGRawValueMethods<FGAIBallistic,double>(*this,
75                                           &FGAIBallistic::_getTime));
76 }
77
78 void FGAIBallistic::unbind() {
79 //    FGAIBase::unbind();
80    props->untie("sim/time/elapsed-sec");
81 }
82
83 void FGAIBallistic::update(double dt) {
84    FGAIBase::update(dt);
85    Run(dt);
86    Transform();
87 }
88
89
90 void FGAIBallistic::setAzimuth(double az) {
91    hdg = azimuth = az;
92 }
93
94
95 void FGAIBallistic::setElevation(double el) {
96    pitch = elevation = el;
97 }
98
99 void FGAIBallistic::setRoll(double rl) {
100    rotation = rl;
101 }
102
103 void FGAIBallistic::setStabilisation(bool val) {
104    aero_stabilised = val;
105 }
106
107 void FGAIBallistic::setDragArea(double a) {
108    drag_area = a;
109 }
110
111 void FGAIBallistic::setLife(double seconds) {
112    life = seconds;
113 }
114
115 void FGAIBallistic::setBuoyancy(double fpss) {
116    buoyancy = fpss;
117 }
118
119 void FGAIBallistic::setWind_from_east(double fps) {
120    wind_from_east = fps;
121 }
122
123 void FGAIBallistic::setWind_from_north(double fps) {
124    wind_from_north = fps;
125 }
126
127 void FGAIBallistic::setWind(bool val) {
128    wind = val;
129 }
130
131 void FGAIBallistic::setCd(double c) {
132    Cd = c;
133 }
134
135 void FGAIBallistic::setMass(double m) {
136    mass = m;
137 }
138
139 void FGAIBallistic::Run(double dt) {
140
141    life_timer += dt;
142 //    cout << "life timer 1" << life_timer <<  dt << endl;
143    if (life_timer > life) setDie(true); 
144
145    double speed_north_deg_sec;
146    double speed_east_deg_sec;
147    double wind_speed_from_north_deg_sec;
148    double wind_speed_from_east_deg_sec;
149    double Cdm;      // Cd adjusted by Mach Number
150    
151    // Adjust Cd by Mach number. The equations are based on curves
152    // for a conventional shell/bullet (no boat-tail). 
153    if ( Mach < 0.7 ) { Cdm = 0.0125 * Mach + Cd; }
154      else if ( 0.7 < Mach && Mach < 1.2 ) { 
155        Cdm = 0.3742 * pow ( Mach, 2) - 0.252 * Mach + 0.0021 + Cd; }
156      else { Cdm = 0.2965 * pow ( Mach, -1.1506 ) + Cd; }
157
158 //   cout << " Mach , " << Mach << " , Cdm , " << Cdm << endl;
159    
160    // drag = Cd * 0.5 * rho * speed * speed * drag_area;
161    // rho is adjusted for altitude in void FGAIBase::update,
162    // using Standard Atmosphere (sealevel temperature 15C)
163    // acceleration = drag/mass;
164    // adjust speed by drag
165    speed -= (Cdm * 0.5 * rho * speed * speed * drag_area/mass) * dt; 
166
167    // don't let speed become negative
168    if ( speed < 0.0 ) speed = 0.0;
169    
170    // calculate vertical and horizontal speed components
171    vs = sin( pitch * SG_DEGREES_TO_RADIANS ) * speed;
172    hs = cos( pitch * SG_DEGREES_TO_RADIANS ) * speed;
173
174    // convert horizontal speed (fps) to degrees per second
175    speed_north_deg_sec = cos(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lat;
176    speed_east_deg_sec  = sin(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lon;
177    
178    // if wind not required, set to zero
179    if (!wind) {
180       wind_from_north = 0;
181       wind_from_east = 0;
182    }
183   
184    // convert wind speed (fps) to degrees per second
185    wind_speed_from_north_deg_sec = wind_from_north / ft_per_deg_lat;
186    wind_speed_from_east_deg_sec  = wind_from_east / ft_per_deg_lon;
187    
188    // set new position
189    pos.setlat( pos.lat() + (speed_north_deg_sec - wind_speed_from_north_deg_sec) * dt );
190    pos.setlon( pos.lon() + (speed_east_deg_sec - wind_speed_from_east_deg_sec) * dt ); 
191
192    // adjust vertical speed for acceleration of gravity and buoyancy
193    vs -= (gravity - buoyancy) * dt;
194    
195    // adjust altitude (feet)
196    altitude += vs * dt;
197    pos.setelev(altitude * SG_FEET_TO_METER); 
198
199    // recalculate pitch (velocity vector) if aerostabilized
200    //   cout << "aero_stabilised " << aero_stabilised  << endl ;
201    if (aero_stabilised) pitch = atan2( vs, hs ) * SG_RADIANS_TO_DEGREES;
202
203    // recalculate total speed
204    speed = sqrt( vs * vs + hs * hs);
205
206    // set destruction flag if altitude less than sea level -1000
207    if (altitude < -1000.0) setDie(true);
208
209 }  // end Run
210
211 double FGAIBallistic::_getTime() const {
212 //    cout << "life timer 2" << life_timer << endl;
213   return life_timer;
214 }
215
216 // end AIBallistic