2003-11-28 15:48:05 +00:00
|
|
|
// FGAIBallistic - FGAIBase-derived class creates a ballistic object
|
|
|
|
//
|
|
|
|
// Written by David Culp, started November 2003.
|
|
|
|
// - davidculp2@comcast.net
|
|
|
|
//
|
2007-03-30 22:51:52 +00:00
|
|
|
// With major additions by Mathias Froehlich & Vivian Meazza 2004-2007
|
|
|
|
//
|
2003-11-28 15:48:05 +00:00
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
2006-02-21 01:16:04 +00:00
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2003-11-28 15:48:05 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <simgear/math/point3d.hxx>
|
2007-03-30 22:51:52 +00:00
|
|
|
#include <simgear/math/sg_random.h>
|
2003-11-28 15:48:05 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "AIBallistic.hxx"
|
|
|
|
|
|
|
|
|
2006-02-11 13:16:56 +00:00
|
|
|
FGAIBallistic::FGAIBallistic() : FGAIBase(otBallistic) {
|
2004-08-26 16:25:54 +00:00
|
|
|
drag_area = 0.007;
|
2004-08-30 09:11:59 +00:00
|
|
|
life_timer = 0.0;
|
2004-09-22 08:47:05 +00:00
|
|
|
gravity = 32;
|
|
|
|
// buoyancy = 64;
|
2004-09-17 16:32:58 +00:00
|
|
|
no_roll = false;
|
2006-02-11 13:16:56 +00:00
|
|
|
aero_stabilised = false;
|
2004-09-22 08:47:05 +00:00
|
|
|
}
|
2003-11-28 15:48:05 +00:00
|
|
|
|
|
|
|
FGAIBallistic::~FGAIBallistic() {
|
|
|
|
}
|
|
|
|
|
2006-02-11 13:16:56 +00:00
|
|
|
void FGAIBallistic::readFromScenario(SGPropertyNode* scFileNode) {
|
|
|
|
if (!scFileNode)
|
|
|
|
return;
|
|
|
|
|
|
|
|
FGAIBase::readFromScenario(scFileNode);
|
|
|
|
|
|
|
|
setAzimuth(scFileNode->getDoubleValue("azimuth", 0.0));
|
|
|
|
setElevation(scFileNode->getDoubleValue("elevation", 0.0));
|
|
|
|
setDragArea(scFileNode->getDoubleValue("eda", 0.007));
|
|
|
|
setLife(scFileNode->getDoubleValue("life", 900.0));
|
|
|
|
setBuoyancy(scFileNode->getDoubleValue("buoyancy", 0));
|
|
|
|
setWind_from_east(scFileNode->getDoubleValue("wind_from_east", 0));
|
|
|
|
setWind_from_north(scFileNode->getDoubleValue("wind_from_north", 0));
|
|
|
|
setWind(scFileNode->getBoolValue("wind", false));
|
|
|
|
setRoll(scFileNode->getDoubleValue("roll", 0.0));
|
|
|
|
setCd(scFileNode->getDoubleValue("cd", 0.029));
|
|
|
|
setMass(scFileNode->getDoubleValue("mass", 0.007));
|
|
|
|
setStabilisation(scFileNode->getBoolValue("aero_stabilized", false));
|
2007-03-30 22:51:52 +00:00
|
|
|
setNoRoll(scFileNode->getBoolValue("no-roll", false));
|
|
|
|
setRandom(scFileNode->getBoolValue("random", false));
|
2006-02-11 13:16:56 +00:00
|
|
|
}
|
2003-11-28 15:48:05 +00:00
|
|
|
|
2007-01-13 16:04:28 +00:00
|
|
|
bool FGAIBallistic::init(bool search_in_AI_path) {
|
|
|
|
FGAIBase::init(search_in_AI_path);
|
2003-11-28 15:48:05 +00:00
|
|
|
hdg = azimuth;
|
|
|
|
pitch = elevation;
|
2004-09-17 16:32:58 +00:00
|
|
|
roll = rotation;
|
2004-09-14 08:27:55 +00:00
|
|
|
Transform();
|
2003-11-28 15:48:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2003-12-21 20:12:55 +00:00
|
|
|
void FGAIBallistic::bind() {
|
2004-08-21 12:24:54 +00:00
|
|
|
// FGAIBase::bind();
|
2004-09-08 14:02:25 +00:00
|
|
|
props->tie("sim/time/elapsed-sec",
|
|
|
|
SGRawValueMethods<FGAIBallistic,double>(*this,
|
|
|
|
&FGAIBallistic::_getTime));
|
2003-12-21 20:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FGAIBallistic::unbind() {
|
2004-08-21 12:24:54 +00:00
|
|
|
// FGAIBase::unbind();
|
2004-08-30 11:13:29 +00:00
|
|
|
props->untie("sim/time/elapsed-sec");
|
2003-12-21 20:12:55 +00:00
|
|
|
}
|
2003-11-28 15:48:05 +00:00
|
|
|
|
|
|
|
void FGAIBallistic::update(double dt) {
|
2004-06-11 13:49:07 +00:00
|
|
|
FGAIBase::update(dt);
|
2003-11-28 15:48:05 +00:00
|
|
|
Run(dt);
|
|
|
|
Transform();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGAIBallistic::setAzimuth(double az) {
|
2004-08-21 12:24:54 +00:00
|
|
|
hdg = azimuth = az;
|
2003-11-28 15:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FGAIBallistic::setElevation(double el) {
|
2004-08-21 12:24:54 +00:00
|
|
|
pitch = elevation = el;
|
2003-11-28 15:48:05 +00:00
|
|
|
}
|
|
|
|
|
2004-09-17 16:32:58 +00:00
|
|
|
void FGAIBallistic::setRoll(double rl) {
|
|
|
|
rotation = rl;
|
|
|
|
}
|
2003-11-28 15:48:05 +00:00
|
|
|
|
2004-10-28 08:29:15 +00:00
|
|
|
void FGAIBallistic::setStabilisation(bool val) {
|
|
|
|
aero_stabilised = val;
|
2003-11-28 15:48:05 +00:00
|
|
|
}
|
|
|
|
|
2007-03-30 22:51:52 +00:00
|
|
|
void FGAIBallistic::setNoRoll(bool nr) {
|
|
|
|
no_roll = nr;
|
|
|
|
}
|
|
|
|
|
2004-08-26 16:25:54 +00:00
|
|
|
void FGAIBallistic::setDragArea(double a) {
|
|
|
|
drag_area = a;
|
|
|
|
}
|
2003-11-28 15:48:05 +00:00
|
|
|
|
2004-08-30 09:11:59 +00:00
|
|
|
void FGAIBallistic::setLife(double seconds) {
|
|
|
|
life = seconds;
|
|
|
|
}
|
|
|
|
|
2004-09-01 08:32:54 +00:00
|
|
|
void FGAIBallistic::setBuoyancy(double fpss) {
|
|
|
|
buoyancy = fpss;
|
|
|
|
}
|
|
|
|
|
2004-09-05 09:45:34 +00:00
|
|
|
void FGAIBallistic::setWind_from_east(double fps) {
|
|
|
|
wind_from_east = fps;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGAIBallistic::setWind_from_north(double fps) {
|
|
|
|
wind_from_north = fps;
|
|
|
|
}
|
2004-09-01 08:32:54 +00:00
|
|
|
|
2004-09-05 09:45:34 +00:00
|
|
|
void FGAIBallistic::setWind(bool val) {
|
|
|
|
wind = val;
|
|
|
|
}
|
2003-11-28 15:48:05 +00:00
|
|
|
|
2004-09-22 08:47:05 +00:00
|
|
|
void FGAIBallistic::setCd(double c) {
|
|
|
|
Cd = c;
|
|
|
|
}
|
|
|
|
|
2004-09-27 14:24:20 +00:00
|
|
|
void FGAIBallistic::setMass(double m) {
|
|
|
|
mass = m;
|
2004-09-22 08:47:05 +00:00
|
|
|
}
|
|
|
|
|
2007-03-30 22:51:52 +00:00
|
|
|
void FGAIBallistic::setRandom(bool r) {
|
|
|
|
random = r;
|
|
|
|
}
|
2004-09-22 19:11:36 +00:00
|
|
|
|
2007-03-30 22:51:52 +00:00
|
|
|
void FGAIBallistic::setName(const string& n) {
|
|
|
|
name = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGAIBallistic::Run(double dt) {
|
2004-08-30 09:11:59 +00:00
|
|
|
life_timer += dt;
|
2004-10-02 12:02:04 +00:00
|
|
|
// cout << "life timer 1" << life_timer << dt << endl;
|
2004-08-30 09:11:59 +00:00
|
|
|
if (life_timer > life) setDie(true);
|
|
|
|
|
2003-11-28 15:48:05 +00:00
|
|
|
double speed_north_deg_sec;
|
|
|
|
double speed_east_deg_sec;
|
2004-09-05 09:45:34 +00:00
|
|
|
double wind_speed_from_north_deg_sec;
|
|
|
|
double wind_speed_from_east_deg_sec;
|
2004-10-02 12:02:04 +00:00
|
|
|
double Cdm; // Cd adjusted by Mach Number
|
|
|
|
|
2007-03-30 22:51:52 +00:00
|
|
|
//randomise Cd by +- 5%
|
|
|
|
if (random)
|
|
|
|
Cd = Cd * 0.95 + (0.05 * sg_random());
|
|
|
|
|
2004-10-02 12:02:04 +00:00
|
|
|
// Adjust Cd by Mach number. The equations are based on curves
|
|
|
|
// for a conventional shell/bullet (no boat-tail).
|
2007-03-30 22:51:52 +00:00
|
|
|
if ( Mach < 0.7 )
|
|
|
|
Cdm = 0.0125 * Mach + Cd;
|
|
|
|
else if ( 0.7 < Mach && Mach < 1.2 )
|
|
|
|
Cdm = 0.3742 * pow ( Mach, 2) - 0.252 * Mach + 0.0021 + Cd;
|
|
|
|
else
|
|
|
|
Cdm = 0.2965 * pow ( Mach, -1.1506 ) + Cd;
|
|
|
|
|
|
|
|
//cout << " Mach , " << Mach << " , Cdm , " << Cdm << " ballistic speed kts //"<< speed << endl;
|
2004-09-22 08:47:05 +00:00
|
|
|
|
|
|
|
// drag = Cd * 0.5 * rho * speed * speed * drag_area;
|
2004-09-27 14:24:20 +00:00
|
|
|
// rho is adjusted for altitude in void FGAIBase::update,
|
|
|
|
// using Standard Atmosphere (sealevel temperature 15C)
|
2004-09-22 08:47:05 +00:00
|
|
|
// acceleration = drag/mass;
|
|
|
|
// adjust speed by drag
|
2004-10-02 12:02:04 +00:00
|
|
|
speed -= (Cdm * 0.5 * rho * speed * speed * drag_area/mass) * dt;
|
2004-09-22 08:47:05 +00:00
|
|
|
|
|
|
|
// don't let speed become negative
|
2007-03-30 22:51:52 +00:00
|
|
|
if ( speed < 0.0 )
|
|
|
|
speed = 0.0;
|
|
|
|
|
|
|
|
double speed_fps = speed * SG_KT_TO_FPS;
|
2004-09-22 08:47:05 +00:00
|
|
|
|
|
|
|
// calculate vertical and horizontal speed components
|
2007-03-30 22:51:52 +00:00
|
|
|
vs = sin( pitch * SG_DEGREES_TO_RADIANS ) * speed_fps;
|
|
|
|
double hs = cos( pitch * SG_DEGREES_TO_RADIANS ) * speed_fps;
|
2004-08-21 12:24:54 +00:00
|
|
|
|
2003-11-28 15:48:05 +00:00
|
|
|
// convert horizontal speed (fps) to degrees per second
|
2003-12-22 10:24:15 +00:00
|
|
|
speed_north_deg_sec = cos(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lat;
|
|
|
|
speed_east_deg_sec = sin(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lon;
|
2004-09-05 09:45:34 +00:00
|
|
|
|
2004-09-22 08:47:05 +00:00
|
|
|
// if wind not required, set to zero
|
2004-09-22 19:11:36 +00:00
|
|
|
if (!wind) {
|
2004-09-05 09:45:34 +00:00
|
|
|
wind_from_north = 0;
|
|
|
|
wind_from_east = 0;
|
2004-09-22 08:47:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// convert wind speed (fps) to degrees per second
|
2004-09-05 09:45:34 +00:00
|
|
|
wind_speed_from_north_deg_sec = wind_from_north / ft_per_deg_lat;
|
|
|
|
wind_speed_from_east_deg_sec = wind_from_east / ft_per_deg_lon;
|
|
|
|
|
|
|
|
// set new position
|
2007-03-30 22:51:52 +00:00
|
|
|
pos.setLatitudeDeg( pos.getLatitudeDeg()
|
|
|
|
+ (speed_north_deg_sec - wind_speed_from_north_deg_sec) * dt );
|
|
|
|
pos.setLongitudeDeg( pos.getLongitudeDeg()
|
|
|
|
+ (speed_east_deg_sec - wind_speed_from_east_deg_sec) * dt );
|
2003-11-28 15:48:05 +00:00
|
|
|
|
2004-09-22 08:47:05 +00:00
|
|
|
// adjust vertical speed for acceleration of gravity and buoyancy
|
2004-09-01 08:32:54 +00:00
|
|
|
vs -= (gravity - buoyancy) * dt;
|
|
|
|
|
2004-08-21 12:24:54 +00:00
|
|
|
// adjust altitude (feet)
|
2006-06-24 00:00:27 +00:00
|
|
|
altitude_ft += vs * dt;
|
|
|
|
pos.setElevationFt(altitude_ft);
|
2004-08-21 12:24:54 +00:00
|
|
|
|
|
|
|
// recalculate pitch (velocity vector) if aerostabilized
|
2007-03-30 22:51:52 +00:00
|
|
|
/*cout << name << ": " << "aero_stabilised " << aero_stabilised
|
|
|
|
<< " pitch " << pitch <<" vs " << vs <<endl ;*/
|
|
|
|
|
|
|
|
if (aero_stabilised)
|
|
|
|
pitch = atan2( vs, hs ) * SG_RADIANS_TO_DEGREES;
|
2003-11-28 15:48:05 +00:00
|
|
|
|
2004-08-21 12:24:54 +00:00
|
|
|
// recalculate total speed
|
2007-03-30 22:51:52 +00:00
|
|
|
speed = sqrt( vs * vs + hs * hs) / SG_KT_TO_FPS;
|
2004-08-21 12:24:54 +00:00
|
|
|
|
2003-11-28 15:48:05 +00:00
|
|
|
// set destruction flag if altitude less than sea level -1000
|
2006-06-24 00:00:27 +00:00
|
|
|
if (altitude_ft < -1000.0) setDie(true);
|
2003-11-28 15:48:05 +00:00
|
|
|
|
2004-09-22 08:47:05 +00:00
|
|
|
} // end Run
|
2004-09-08 14:02:25 +00:00
|
|
|
|
|
|
|
double FGAIBallistic::_getTime() const {
|
2004-10-02 12:02:04 +00:00
|
|
|
// cout << "life timer 2" << life_timer << endl;
|
|
|
|
return life_timer;
|
2004-09-08 14:02:25 +00:00
|
|
|
}
|
|
|
|
|
2004-09-22 08:47:05 +00:00
|
|
|
// end AIBallistic
|