]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIBallistic.cxx
5bfadf48b403cd9a11166d968b74740795c1060f
[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(FGAIManager* mgr) {
31     manager = mgr;
32     _type_str = "ballistic";
33     _otype = otBallistic;
34     drag_area = 0.007;
35     life_timer = 0.0;
36     gravity = 32;
37 //  buoyancy = 64;
38     no_roll = false;
39     Mach = 0.4;
40 }
41
42 FGAIBallistic::~FGAIBallistic() {
43 }
44
45
46 bool FGAIBallistic::init() {
47    FGAIBase::init();
48    aero_stabilized = true;
49    hdg = azimuth;
50    pitch = elevation;
51    roll = rotation;
52    Transform();
53    return true;
54 }
55
56 void FGAIBallistic::bind() {
57 //    FGAIBase::bind();
58    props->tie("sim/time/elapsed-sec",
59                SGRawValueMethods<FGAIBallistic,double>(*this,
60                                           &FGAIBallistic::_getTime));
61 }
62
63 void FGAIBallistic::unbind() {
64 //    FGAIBase::unbind();
65    props->untie("sim/time/elapsed-sec");
66 }
67
68 void FGAIBallistic::update(double dt) {
69    FGAIBase::update(dt);
70    Run(dt);
71    Transform();
72 }
73
74
75 void FGAIBallistic::setAzimuth(double az) {
76    hdg = azimuth = az;
77 }
78
79
80 void FGAIBallistic::setElevation(double el) {
81    pitch = elevation = el;
82 }
83
84 void FGAIBallistic::setRoll(double rl) {
85    rotation = rl;
86 }
87
88 void FGAIBallistic::setStabilization(bool val) {
89    aero_stabilized = val;
90 }
91
92 void FGAIBallistic::setDragArea(double a) {
93    drag_area = a;
94 }
95
96 void FGAIBallistic::setLife(double seconds) {
97    life = seconds;
98 }
99
100 void FGAIBallistic::setBuoyancy(double fpss) {
101    buoyancy = fpss;
102 }
103
104 void FGAIBallistic::setWind_from_east(double fps) {
105    wind_from_east = fps;
106 }
107
108 void FGAIBallistic::setWind_from_north(double fps) {
109    wind_from_north = fps;
110 }
111
112 void FGAIBallistic::setWind(bool val) {
113    wind = val;
114 }
115
116 void FGAIBallistic::setCd(double c) {
117    Cd = c;
118 }
119
120 void FGAIBallistic::setMass(double m) {
121    mass = m;
122 }
123
124 void FGAIBallistic::Run(double dt) {
125
126    life_timer += dt;
127 //    cout << "life timer 1" << life_timer <<  dt << endl;
128    if (life_timer > life) setDie(true); 
129
130    double speed_north_deg_sec;
131    double speed_east_deg_sec;
132    double wind_speed_from_north_deg_sec;
133    double wind_speed_from_east_deg_sec;
134    double Cdm;      // Cd adjusted by Mach Number
135    
136    // Adjust Cd by Mach number. The equations are based on curves
137    // for a conventional shell/bullet (no boat-tail). 
138    if ( Mach < 0.7 ) { Cdm = 0.0125 * Mach + Cd; }
139      else if ( 0.7 < Mach && Mach < 1.2 ) { 
140        Cdm = 0.3742 * pow ( Mach, 2) - 0.252 * Mach + 0.0021 + Cd; }
141      else { Cdm = 0.2965 * pow ( Mach, -1.1506 ) + Cd; }
142
143 //   cout << " Mach , " << Mach << " , Cdm , " << Cdm << endl;
144    
145    // drag = Cd * 0.5 * rho * speed * speed * drag_area;
146    // rho is adjusted for altitude in void FGAIBase::update,
147    // using Standard Atmosphere (sealevel temperature 15C)
148    // acceleration = drag/mass;
149    // adjust speed by drag
150    speed -= (Cdm * 0.5 * rho * speed * speed * drag_area/mass) * dt; 
151
152    // don't let speed become negative
153    if ( speed < 0.0 ) speed = 0.0;
154    
155    // calculate vertical and horizontal speed components
156    vs = sin( pitch * SG_DEGREES_TO_RADIANS ) * speed;
157    hs = cos( pitch * SG_DEGREES_TO_RADIANS ) * speed;
158
159    // convert horizontal speed (fps) to degrees per second
160    speed_north_deg_sec = cos(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lat;
161    speed_east_deg_sec  = sin(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lon;
162    
163    // if wind not required, set to zero
164    if (!wind) {
165       wind_from_north = 0;
166       wind_from_east = 0;
167    }
168   
169    // convert wind speed (fps) to degrees per second
170    wind_speed_from_north_deg_sec = wind_from_north / ft_per_deg_lat;
171    wind_speed_from_east_deg_sec  = wind_from_east / ft_per_deg_lon;
172    
173    // set new position
174    pos.setlat( pos.lat() + (speed_north_deg_sec - wind_speed_from_north_deg_sec) * dt );
175    pos.setlon( pos.lon() + (speed_east_deg_sec - wind_speed_from_east_deg_sec) * dt ); 
176
177    // adjust vertical speed for acceleration of gravity and buoyancy
178    vs -= (gravity - buoyancy) * dt;
179    
180    // adjust altitude (feet)
181    altitude += vs * dt;
182    pos.setelev(altitude * SG_FEET_TO_METER); 
183
184    // recalculate pitch (velocity vector) if aerostabilized
185    if (aero_stabilized) pitch = atan2( vs, hs ) * SG_RADIANS_TO_DEGREES;
186
187    // recalculate total speed
188    speed = sqrt( vs * vs + hs * hs);
189
190    // set destruction flag if altitude less than sea level -1000
191    if (altitude < -1000.0) setDie(true);
192
193 }  // end Run
194
195 double FGAIBallistic::_getTime() const {
196 //    cout << "life timer 2" << life_timer << endl;
197   return life_timer;
198 }
199
200 // end AIBallistic