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