#include <simgear/math/point3d.hxx>
#include <simgear/math/sg_random.h>
#include <simgear/scene/material/mat.hxx>
-#include <math.h>
-#include <vector>
+#include <simgear/math/sg_geodesy.hxx>
#include <Scenery/scenery.hxx>
#include "AIBallistic.hxx"
-SG_USING_STD(vector);
-
const double FGAIBallistic::slugs_to_kgs = 14.5939029372;
FGAIBallistic::FGAIBallistic() :
FGAIBase(otBallistic),
+ _elevation(0),
_aero_stabilised(false),
_drag_area(0.007),
_life_timer(0.0),
_gravity(32),
- // _buoyancy(64),
+ _buoyancy(0),
+ _random(false),
_ht_agl_ft(0),
_load_resistance(0),
_solid(false),
- _impact_data(false),
- _impact_energy(0),
- _impact_speed(0),
- _impact_lat(0),
- _impact_lon(0),
- _impact_elev(0),
- _mat_name("")
+ _report_collision(false),
+ _report_impact(false),
+ _impact_report_node(fgGetNode("/ai/models/model-impact", true))
{
no_roll = false;
}
setNoRoll(scFileNode->getBoolValue("no-roll", false));
setRandom(scFileNode->getBoolValue("random", false));
setImpact(scFileNode->getBoolValue("impact", false));
+ setImpactReportNode(scFileNode->getStringValue("impact-reports"));
setName(scFileNode->getStringValue("name", "Bomb"));
+ setFuseRange(scFileNode->getDoubleValue("fuse-range", 0.0));
+ setSMPath(scFileNode->getStringValue("submodel-path", ""));
+ setSubID(scFileNode->getIntValue("SubID", 0));
}
bool FGAIBallistic::init(bool search_in_AI_path) {
FGAIBase::init(search_in_AI_path);
- props->setStringValue("material/name", _mat_name.c_str());
+ props->setStringValue("material/name", "");
props->setStringValue("name", _name.c_str());
+ props->setStringValue("submodels/path", _submodel.c_str());
// start with high value so that animations don't trigger yet
- _ht_agl_ft = 10000000;
+ _ht_agl_ft = 1e10;
hdg = _azimuth;
pitch = _elevation;
roll = _rotation;
Transform();
+
return true;
}
SGRawValuePointer<bool>(&_solid));
props->tie("altitude-agl-ft",
SGRawValuePointer<double>(&_ht_agl_ft));
- props->tie("impact/latitude-deg",
- SGRawValuePointer<double>(&_impact_lat));
- props->tie("impact/longitude-deg",
- SGRawValuePointer<double>(&_impact_lon));
- props->tie("impact/elevation-m",
- SGRawValuePointer<double>(&_impact_elev));
- props->tie("impact/speed-mps",
- SGRawValuePointer<double>(&_impact_speed));
- props->tie("impact/energy-kJ",
- SGRawValuePointer<double>(&_impact_energy));
}
void FGAIBallistic::unbind() {
props->untie("material/load-resistance");
props->untie("material/solid");
props->untie("altitude-agl-ft");
- props->untie("impact/latitude-deg");
- props->untie("impact/longitude-deg");
- props->untie("impact/elevation-m");
- props->untie("impact/speed-mps");
- props->untie("impact/energy-kJ");
}
void FGAIBallistic::update(double dt) {
}
void FGAIBallistic::setImpact(bool i) {
- _impact = i;
+ _report_impact = i;
+}
+
+void FGAIBallistic::setCollision(bool c) {
+ _report_collision = c;
+}
+
+void FGAIBallistic::setImpactReportNode(const string& path) {
+ if (!path.empty())
+ _impact_report_node = fgGetNode(path.c_str(), true);
}
void FGAIBallistic::setName(const string& n) {
_name = n;
}
+void FGAIBallistic::setSMPath(const string& s) {
+ _submodel = s;
+}
+
+void FGAIBallistic::setFuseRange(double f) {
+ _fuse_range = f;
+}
+
+void FGAIBallistic::setSubID(int i) {
+ _subID = i;
+ //cout << "sub id " << _subID << " name " << _name << endl;
+}
+
+void FGAIBallistic::setSubmodel(const string& s) {
+ _submodel = s;
+}
+
void FGAIBallistic::Run(double dt) {
_life_timer += dt;
- // cout << "life timer 1" << _life_timer << dt << endl;
+ //cout << "life timer" <<_name <<" " << _life_timer << dt << endl;
if (_life_timer > life)
setDie(true);
- double speed_north_deg_sec;
- double speed_east_deg_sec;
- double wind_speed_from_north_deg_sec;
- double wind_speed_from_east_deg_sec;
- double Cdm; // Cd adjusted by Mach Number
-
//randomise Cd by +- 5%
if (_random)
_Cd = _Cd * 0.95 + (0.05 * sg_random());
// Adjust Cd by Mach number. The equations are based on curves
// for a conventional shell/bullet (no boat-tail).
- if (Mach >= 1.2)
- Cdm = 0.2965 * pow ( Mach, -1.1506 ) + _Cd;
- else if (Mach >= 0.7)
- Cdm = 0.3742 * pow ( Mach, 2) - 0.252 * Mach + 0.0021 + _Cd;
- else
+ double Cdm;
+
+ if (Mach < 0.7)
Cdm = 0.0125 * Mach + _Cd;
+ else if (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;
speed = 0.0;
double speed_fps = speed * SG_KT_TO_FPS;
+ double hs;
// calculate vertical and horizontal speed components
- vs = sin( pitch * SG_DEGREES_TO_RADIANS ) * speed_fps;
- double hs = cos( pitch * SG_DEGREES_TO_RADIANS ) * speed_fps;
+ if (speed == 0.0) {
+ hs = vs = 0.0;
+ } else {
+ vs = sin( _elevation * SG_DEGREES_TO_RADIANS ) * speed_fps;
+ hs = cos( _elevation * SG_DEGREES_TO_RADIANS ) * speed_fps;
+ }
// convert horizontal speed (fps) to degrees per second
- 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;
+ double speed_north_deg_sec = cos(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lat;
+ double speed_east_deg_sec = sin(hdg / SG_RADIANS_TO_DEGREES) * hs / ft_per_deg_lon;
// if wind not required, set to zero
if (!_wind) {
}
// convert wind speed (fps) to degrees per second
- 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;
+ double wind_speed_from_north_deg_sec = _wind_from_north / ft_per_deg_lat;
+ double wind_speed_from_east_deg_sec = _wind_from_east / ft_per_deg_lon;
// set new position
pos.setLatitudeDeg( pos.getLatitudeDeg()
altitude_ft += vs * dt;
pos.setElevationFt(altitude_ft);
- // recalculate pitch (velocity vector) if aerostabilized
+ // recalculate elevation (velocity vector) if aerostabilized
/*cout << _name << ": " << "aero_stabilised " << _aero_stabilised
<< " pitch " << pitch <<" vs " << vs <<endl ;*/
- if (_aero_stabilised)
- pitch = atan2( vs, hs ) * SG_RADIANS_TO_DEGREES;
+ if (_aero_stabilised) { // we simulate rotational moment of inertia by using a filter
+ const double coeff = 0.9;
+ double c = dt / (coeff + dt);
+ //cout << "c " << c << endl;
+ _elevation = atan2( vs, hs ) * SG_RADIANS_TO_DEGREES;
+ pitch = (_elevation * c) + (pitch * (1 - c));
+ }
// recalculate total speed
speed = sqrt( vs * vs + hs * hs) / SG_KT_TO_FPS;
- if (_impact && !_impact_data && vs < 0)
+ //do impacts and collisions
+ if (_report_impact && !_impact_reported)
handle_impact();
+ if (_report_collision && !_collision_reported)
+ handle_collision();
+
// set destruction flag if altitude less than sea level -1000
if (altitude_ft < -1000.0)
setDie(true);
if (material) {
const vector<string> names = material->get_names();
+ string mat_name;
if (!names.empty())
- _mat_name = names[0].c_str();
+ mat_name = names[0].c_str();
_solid = material->get_solid();
_load_resistance = material->get_load_resistance();
- props->setStringValue("material/name", _mat_name.c_str());
- //cout << "material " << _mat_name << " solid " << _solid << " load " << _load_resistance << endl;
+ props->setStringValue("material/name", mat_name.c_str());
+ //cout << "material " << mat_name << " solid " << _solid << " load " << _load_resistance << endl;
}
_ht_agl_ft = pos.getElevationFt() - elevation_m * SG_METER_TO_FEET;
- // report impact by setting tied variables
if (_ht_agl_ft <= 0) {
- _impact_lat = pos.getLatitudeDeg();
- _impact_lon = pos.getLongitudeDeg();
- _impact_elev = elevation_m;
- _impact_speed = speed * SG_KT_TO_MPS;
- _impact_energy = (_mass * slugs_to_kgs) * _impact_speed
- * _impact_speed / (2 * 1000);
-
- props->setBoolValue("impact/signal", true); // for listeners
- _impact_data = true;
+ SG_LOG(SG_GENERAL, SG_DEBUG, "AIBallistic: terrain impact");
+ report_impact(elevation_m);
+ _impact_reported = true;
+
+ // kill the AIObject if there is no subsubmodel
+ if (_subID == 0)
+ setDie(true);
+ }
+}
+
+void FGAIBallistic::handle_collision()
+{
+ const FGAIBase *object = manager->calcCollision(pos.getElevationFt(),
+ pos.getLatitudeDeg(),pos.getLongitudeDeg(), _fuse_range);
+
+ if (object) {
+ SG_LOG(SG_GENERAL, SG_DEBUG, "AIBallistic: object hit");
+ report_impact(pos.getElevationM(), object);
+ _collision_reported = true;
}
}
+void FGAIBallistic::report_impact(double elevation, const FGAIBase *object)
+{
+ _impact_lat = pos.getLatitudeDeg();
+ _impact_lon = pos.getLongitudeDeg();
+ _impact_elev = elevation;
+ _impact_speed = speed * SG_KT_TO_MPS;
+ _impact_hdg = hdg;
+ _impact_pitch = pitch;
+ _impact_roll = roll;
+
+ SGPropertyNode *n = props->getNode("impact", true);
+ if (object)
+ n->setStringValue("type", object->getTypeString());
+ else
+ n->setStringValue("type", "terrain");
+
+ n->setDoubleValue("longitude-deg", _impact_lon);
+ n->setDoubleValue("latitude-deg", _impact_lat);
+ n->setDoubleValue("elevation-m", _impact_elev);
+ n->setDoubleValue("heading-deg", _impact_hdg);
+ n->setDoubleValue("pitch-deg", _impact_pitch);
+ n->setDoubleValue("roll-deg", _impact_roll);
+ n->setDoubleValue("speed-mps", _impact_speed);
+
+ _impact_report_node->setStringValue(props->getPath());
+}
+
// end AIBallistic