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