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"
31 FGAIBallistic::FGAIBallistic(FGAIManager* mgr) {
33 _type_str = "ballistic";
42 FGAIBallistic::~FGAIBallistic() {
46 bool FGAIBallistic::init() {
48 aero_stabilized = true;
56 void FGAIBallistic::bind() {
58 props->tie("sim/time/elapsed-sec",
59 SGRawValueMethods<FGAIBallistic,double>(*this,
60 &FGAIBallistic::_getTime));
63 void FGAIBallistic::unbind() {
64 // FGAIBase::unbind();
65 props->untie("sim/time/elapsed-sec");
68 void FGAIBallistic::update(double dt) {
75 void FGAIBallistic::setAzimuth(double az) {
80 void FGAIBallistic::setElevation(double el) {
81 pitch = elevation = el;
84 void FGAIBallistic::setRoll(double rl) {
88 void FGAIBallistic::setStabilization(bool val) {
89 aero_stabilized = val;
92 void FGAIBallistic::setDragArea(double a) {
96 void FGAIBallistic::setLife(double seconds) {
100 void FGAIBallistic::setBuoyancy(double fpss) {
104 void FGAIBallistic::setWind_from_east(double fps) {
105 wind_from_east = fps;
108 void FGAIBallistic::setWind_from_north(double fps) {
109 wind_from_north = fps;
112 void FGAIBallistic::setWind(bool val) {
116 void FGAIBallistic::setCd(double c) {
120 void FGAIBallistic::setWeight(double w) {
124 void FGAIBallistic::Run(double dt) {
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;
134 // the drag calculations below assume sea-level density,
135 // rho = 0.023780 slugs/ft3
137 mass = weight * lbs_to_slugs;
139 // drag = Cd * 0.5 * rho * speed * speed * drag_area;
140 // acceleration = drag/mass;
141 // adjust speed by drag
142 speed -= (Cd * 0.5 * rho * speed * speed * drag_area/mass) * dt;
144 // don't let speed become negative
145 if ( speed < 0.0 ) speed = 0.0;
147 // calculate vertical and horizontal speed components
148 vs = sin( pitch * SG_DEGREES_TO_RADIANS ) * speed;
149 hs = cos( pitch * SG_DEGREES_TO_RADIANS ) * speed;
151 // convert horizontal speed (fps) to degrees per second
152 speed_north_deg_sec = cos(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lat;
153 speed_east_deg_sec = sin(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lon;
155 // if wind not required, set to zero
161 // convert wind speed (fps) to degrees per second
162 wind_speed_from_north_deg_sec = wind_from_north / ft_per_deg_lat;
163 wind_speed_from_east_deg_sec = wind_from_east / ft_per_deg_lon;
166 pos.setlat( pos.lat() + (speed_north_deg_sec - wind_speed_from_north_deg_sec) * dt );
167 pos.setlon( pos.lon() + (speed_east_deg_sec - wind_speed_from_east_deg_sec) * dt );
169 // adjust vertical speed for acceleration of gravity and buoyancy
170 vs -= (gravity - buoyancy) * dt;
172 // adjust altitude (feet)
174 pos.setelev(altitude * SG_FEET_TO_METER);
176 // recalculate pitch (velocity vector) if aerostabilized
177 if (aero_stabilized) pitch = atan2( vs, hs ) * SG_RADIANS_TO_DEGREES;
179 // recalculate total speed
180 speed = sqrt( vs * vs + hs * hs);
182 // set destruction flag if altitude less than sea level -1000
183 if (altitude < -1000.0) setDie(true);
187 double FGAIBallistic::_getTime() const {