]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIBallistic.cxx
86703da9c6065e471369d5ae235865aca3de0a07
[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() {
31     _type_str = "ballistic";
32 }
33
34 FGAIBallistic::~FGAIBallistic() {
35 }
36
37
38 bool FGAIBallistic::init() {
39    FGAIBase::init();
40    vs = sin( elevation * 0.017453293 ) * speed;
41    hs = cos( elevation * 0.017453293 ) * speed;
42    aero_stabilized =  true;
43    hdg = azimuth;
44    pitch = elevation;
45    return true;
46 }
47
48 void FGAIBallistic::bind() {
49     FGAIBase::bind();
50 }
51
52 void FGAIBallistic::unbind() {
53     FGAIBase::unbind();
54 }
55
56 void FGAIBallistic::update(double dt) {
57
58    Run(dt);
59    Transform();
60    FGAIBase::update(dt);
61 }
62
63
64 void FGAIBallistic::setAzimuth(double az) {
65    azimuth = az;
66 }
67
68
69 void FGAIBallistic::setElevation(double el) {
70    elevation = el;
71 }
72
73
74 void FGAIBallistic::setStabilization(bool val) {
75    aero_stabilized = val;
76 }
77
78
79 void FGAIBallistic::Run(double dt) {
80
81    double speed_north_deg_sec;
82    double speed_east_deg_sec;
83    double ft_per_deg_lon;
84    double ft_per_deg_lat;
85
86    // get size of a degree at this latitude
87    ft_per_deg_lat = 366468.96 - 3717.12 * cos(pos.lat()/SG_RADIANS_TO_DEGREES);
88    ft_per_deg_lon = 365228.16 * cos(pos.lat() / SG_RADIANS_TO_DEGREES);
89
90    // the two drag calculations below assume sea-level density, 
91    // mass of 0.03 slugs,  drag coeff of 0.295, frontal area of 0.007 ft2 
92    // adjust horizontal speed due to drag 
93    hs -= 0.000082 * hs * hs * dt;
94    if ( hs < 0.0 ) hs = 0.0;
95
96    // adjust vertical speed due to drag
97    if (vs > 0.0) {
98      vs -= 0.000082 * vs * vs * dt;
99    } else {
100      vs += 0.000082 * vs * vs * dt;
101    }
102    
103    // convert horizontal speed (fps) to degrees per second
104    speed_north_deg_sec = cos(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lat;
105    speed_east_deg_sec  = sin(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lon;
106
107    // set new position
108    pos.setlat( pos.lat() + speed_north_deg_sec * dt);
109    pos.setlon( pos.lon() + speed_east_deg_sec * dt); 
110
111    // adjust vertical speed for acceleration of gravity
112    vs -= 32.17 * dt;
113
114    // adjust altitude (meters)
115    altitude += vs * dt * SG_FEET_TO_METER;
116    pos.setelev(altitude); 
117
118    // adjust pitch if aerostabilized
119    if (aero_stabilized) pitch = atan2( vs, hs ) * SG_RADIANS_TO_DEGREES;
120
121    // set destruction flag if altitude less than sea level -1000
122    if (altitude < -1000.0) setDie(true);
123
124 }
125