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";
36 FGAIBallistic::~FGAIBallistic() {
40 bool FGAIBallistic::init() {
42 aero_stabilized = true;
48 void FGAIBallistic::bind() {
52 void FGAIBallistic::unbind() {
53 // FGAIBase::unbind();
56 void FGAIBallistic::update(double dt) {
63 void FGAIBallistic::setAzimuth(double az) {
68 void FGAIBallistic::setElevation(double el) {
69 pitch = elevation = el;
73 void FGAIBallistic::setStabilization(bool val) {
74 aero_stabilized = val;
78 void FGAIBallistic::Run(double dt) {
80 double speed_north_deg_sec;
81 double speed_east_deg_sec;
83 // the two drag calculations below assume sea-level density,
84 // mass of 0.03 slugs, drag coeff of 0.295, frontal area of 0.007 ft2
85 // adjust speed due to drag
86 speed -= 0.000082 * speed * speed * dt;
87 if ( speed < 0.0 ) speed = 0.0;
88 vs = sin( pitch * SG_DEGREES_TO_RADIANS ) * speed;
89 hs = cos( pitch * SG_DEGREES_TO_RADIANS ) * speed;
91 // convert horizontal speed (fps) to degrees per second
92 speed_north_deg_sec = cos(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lat;
93 speed_east_deg_sec = sin(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lon;
96 pos.setlat( pos.lat() + speed_north_deg_sec * dt);
97 pos.setlon( pos.lon() + speed_east_deg_sec * dt);
99 // adjust altitude (feet)
101 pos.setelev(altitude * SG_FEET_TO_METER);
103 // adjust vertical speed for acceleration of gravity
106 // recalculate pitch (velocity vector) if aerostabilized
107 if (aero_stabilized) pitch = atan2( vs, hs ) * SG_RADIANS_TO_DEGREES;
109 // recalculate total speed
110 speed = sqrt( vs * vs + hs * hs);
112 // set destruction flag if altitude less than sea level -1000
113 if (altitude < -1000.0) setDie(true);