1 // FGAIBallistic - FGAIBase-derived class creates a ballistic object
3 // Written by David Culp, started November 2003.
4 // - davidculp2@comcast.net
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.
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.
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.
24 #include <simgear/math/point3d.hxx>
27 #include "AIBallistic.hxx"
30 FGAIBallistic::FGAIBallistic(FGAIManager* mgr) {
32 _type_str = "ballistic";
41 FGAIBallistic::~FGAIBallistic() {
45 bool FGAIBallistic::init() {
54 void FGAIBallistic::bind() {
56 props->tie("sim/time/elapsed-sec",
57 SGRawValueMethods<FGAIBallistic,double>(*this,
58 &FGAIBallistic::_getTime));
61 void FGAIBallistic::unbind() {
62 // FGAIBase::unbind();
63 props->untie("sim/time/elapsed-sec");
66 void FGAIBallistic::update(double dt) {
73 void FGAIBallistic::setAzimuth(double az) {
78 void FGAIBallistic::setElevation(double el) {
79 pitch = elevation = el;
82 void FGAIBallistic::setRoll(double rl) {
86 void FGAIBallistic::setStabilisation(bool val) {
87 aero_stabilised = val;
90 void FGAIBallistic::setDragArea(double a) {
94 void FGAIBallistic::setLife(double seconds) {
98 void FGAIBallistic::setBuoyancy(double fpss) {
102 void FGAIBallistic::setWind_from_east(double fps) {
103 wind_from_east = fps;
106 void FGAIBallistic::setWind_from_north(double fps) {
107 wind_from_north = fps;
110 void FGAIBallistic::setWind(bool val) {
114 void FGAIBallistic::setCd(double c) {
118 void FGAIBallistic::setMass(double m) {
122 void FGAIBallistic::Run(double dt) {
125 // cout << "life timer 1" << life_timer << dt << endl;
126 if (life_timer > life) setDie(true);
128 double speed_north_deg_sec;
129 double speed_east_deg_sec;
130 double wind_speed_from_north_deg_sec;
131 double wind_speed_from_east_deg_sec;
132 double Cdm; // Cd adjusted by Mach Number
134 // Adjust Cd by Mach number. The equations are based on curves
135 // for a conventional shell/bullet (no boat-tail).
136 if ( Mach < 0.7 ) { Cdm = 0.0125 * Mach + Cd; }
137 else if ( 0.7 < Mach && Mach < 1.2 ) {
138 Cdm = 0.3742 * pow ( Mach, 2) - 0.252 * Mach + 0.0021 + Cd; }
139 else { Cdm = 0.2965 * pow ( Mach, -1.1506 ) + Cd; }
141 // cout << " Mach , " << Mach << " , Cdm , " << Cdm << endl;
143 // drag = Cd * 0.5 * rho * speed * speed * drag_area;
144 // rho is adjusted for altitude in void FGAIBase::update,
145 // using Standard Atmosphere (sealevel temperature 15C)
146 // acceleration = drag/mass;
147 // adjust speed by drag
148 speed -= (Cdm * 0.5 * rho * speed * speed * drag_area/mass) * dt;
150 // don't let speed become negative
151 if ( speed < 0.0 ) speed = 0.0;
153 // calculate vertical and horizontal speed components
154 vs = sin( pitch * SG_DEGREES_TO_RADIANS ) * speed;
155 hs = cos( pitch * SG_DEGREES_TO_RADIANS ) * speed;
157 // convert horizontal speed (fps) to degrees per second
158 speed_north_deg_sec = cos(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lat;
159 speed_east_deg_sec = sin(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lon;
161 // if wind not required, set to zero
167 // convert wind speed (fps) to degrees per second
168 wind_speed_from_north_deg_sec = wind_from_north / ft_per_deg_lat;
169 wind_speed_from_east_deg_sec = wind_from_east / ft_per_deg_lon;
172 pos.setlat( pos.lat() + (speed_north_deg_sec - wind_speed_from_north_deg_sec) * dt );
173 pos.setlon( pos.lon() + (speed_east_deg_sec - wind_speed_from_east_deg_sec) * dt );
175 // adjust vertical speed for acceleration of gravity and buoyancy
176 vs -= (gravity - buoyancy) * dt;
178 // adjust altitude (feet)
180 pos.setelev(altitude * SG_FEET_TO_METER);
182 // recalculate pitch (velocity vector) if aerostabilized
183 // cout << "aero_stabilised " << aero_stabilised << endl ;
184 if (aero_stabilised) pitch = atan2( vs, hs ) * SG_RADIANS_TO_DEGREES;
186 // recalculate total speed
187 speed = sqrt( vs * vs + hs * hs);
189 // set destruction flag if altitude less than sea level -1000
190 if (altitude < -1000.0) setDie(true);
194 double FGAIBallistic::_getTime() const {
195 // cout << "life timer 2" << life_timer << endl;