]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIBallistic.cxx
4b2b51c7ac4f9966a1c748316be9d8d98d4b93ed
[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         }
39
40 FGAIBallistic::~FGAIBallistic() {
41 }
42
43
44 bool FGAIBallistic::init() {
45    FGAIBase::init();
46    aero_stabilized = true;
47    hdg = azimuth;
48    pitch = elevation;
49    Transform();
50    return true;
51 }
52
53 void FGAIBallistic::bind() {
54 //    FGAIBase::bind();
55    props->tie("sim/time/elapsed-sec",
56                SGRawValueMethods<FGAIBallistic,double>(*this,
57                                           &FGAIBallistic::_getTime));
58 }
59
60 void FGAIBallistic::unbind() {
61 //    FGAIBase::unbind();
62    props->untie("sim/time/elapsed-sec");
63 }
64
65 void FGAIBallistic::update(double dt) {
66    FGAIBase::update(dt);
67    Run(dt);
68    Transform();
69 }
70
71
72 void FGAIBallistic::setAzimuth(double az) {
73    hdg = azimuth = az;
74 }
75
76
77 void FGAIBallistic::setElevation(double el) {
78    pitch = elevation = el;
79 }
80
81
82 void FGAIBallistic::setStabilization(bool val) {
83    aero_stabilized = val;
84 }
85
86 void FGAIBallistic::setDragArea(double a) {
87    drag_area = a;
88 }
89
90 void FGAIBallistic::setLife(double seconds) {
91    life = seconds;
92 }
93
94 void FGAIBallistic::setBuoyancy(double fpss) {
95    buoyancy = fpss;
96 }
97
98 void FGAIBallistic::setWind_from_east(double fps) {
99    wind_from_east = fps;
100 }
101
102 void FGAIBallistic::setWind_from_north(double fps) {
103    wind_from_north = fps;
104 }
105
106 void FGAIBallistic::setWind(bool val) {
107    wind = val;
108 }
109 void FGAIBallistic::Run(double dt) {
110
111    life_timer += dt;
112    if (life_timer > life) setDie(true); 
113
114    double speed_north_deg_sec;
115    double speed_east_deg_sec;
116    double wind_speed_from_north_deg_sec;
117    double wind_speed_from_east_deg_sec;
118       
119    // the two drag calculations below assume sea-level density, 
120    // mass of 0.03 slugs,  drag coeff of 0.295
121    // adjust speed due to drag 
122    speed -= 0.0116918 * drag_area * speed * speed * dt;
123    if ( speed < 0.0 ) speed = 0.0;
124    vs = sin( pitch * SG_DEGREES_TO_RADIANS ) * speed;
125    hs = cos( pitch * SG_DEGREES_TO_RADIANS ) * speed;
126
127    // convert horizontal speed (fps) to degrees per second
128    speed_north_deg_sec = cos(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lat;
129    speed_east_deg_sec  = sin(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lon;
130    
131    // convert wind speed (fps) to degrees per second
132    
133    if (!wind){
134       wind_from_north = 0;
135       wind_from_east = 0;
136           }
137
138    wind_speed_from_north_deg_sec = wind_from_north / ft_per_deg_lat;
139    wind_speed_from_east_deg_sec  = wind_from_east / ft_per_deg_lon;
140    
141  
142    // set new position
143 //   pos.setlat( pos.lat() + (speed_north_deg_sec * dt)  );
144 //   pos.setlon( pos.lon() + (speed_east_deg_sec * dt)  ); 
145  
146  
147    // set new position
148    
149    pos.setlat( pos.lat() + (speed_north_deg_sec - wind_speed_from_north_deg_sec) * dt );
150    pos.setlon( pos.lon() + (speed_east_deg_sec - wind_speed_from_east_deg_sec) * dt ); 
151
152    // adjust vertical speed for acceleration of gravity
153    vs -= (gravity - buoyancy) * dt;
154    
155    // adjust altitude (feet)
156    altitude += vs * dt;
157    pos.setelev(altitude * SG_FEET_TO_METER); 
158
159    // recalculate pitch (velocity vector) if aerostabilized
160    if (aero_stabilized) pitch = atan2( vs, hs ) * SG_RADIANS_TO_DEGREES;
161
162    // recalculate total speed
163    speed = sqrt( vs * vs + hs * hs);
164
165    // set destruction flag if altitude less than sea level -1000
166    if (altitude < -1000.0) setDie(true);
167
168 }
169
170 double FGAIBallistic::_getTime() const {
171    return life_timer;
172 }
173