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