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