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