1
0
Fork 0

Vivian MEAZZA: add terrain collision for submodels on request

(set <impact>1</impact> in the submodel config)

mf: minor modifications & cleanup; more to come
This commit is contained in:
mfranz 2007-05-15 16:19:11 +00:00
parent 1bdea279cd
commit 7d5952b185
4 changed files with 210 additions and 95 deletions

View file

@ -25,18 +25,35 @@
#include <simgear/math/point3d.hxx>
#include <simgear/math/sg_random.h>
#include <simgear/scene/material/mat.hxx>
#include <math.h>
#include <vector>
#include <Scenery/scenery.hxx>
#include "AIBallistic.hxx"
SG_USING_STD(vector);
const double FGAIBallistic::slugs_to_kgs = 14.5939029372;
FGAIBallistic::FGAIBallistic() : FGAIBase(otBallistic) {
drag_area = 0.007;
life_timer = 0.0;
gravity = 32;
// buoyancy = 64;
// buoyancy = 64;
no_roll = false;
aero_stabilised = false;
ht_agl_ft = 0;
impact_data = false;
impact_energy = 0;
impact_speed = 0;
impact_lat = 0;
impact_lon = 0;
impact_elev = 0;
load_resistance = 0;
solid = false;
mat_name = "";
}
FGAIBallistic::~FGAIBallistic() {
@ -62,10 +79,16 @@ void FGAIBallistic::readFromScenario(SGPropertyNode* scFileNode) {
setStabilisation(scFileNode->getBoolValue("aero_stabilized", false));
setNoRoll(scFileNode->getBoolValue("no-roll", false));
setRandom(scFileNode->getBoolValue("random", false));
setImpact(scFileNode->getBoolValue("impact", false));
setName(scFileNode->getStringValue("name", "Bomb"));
}
bool FGAIBallistic::init(bool search_in_AI_path) {
FGAIBase::init(search_in_AI_path);
props->setStringValue("material/name", mat_name.c_str());
props->setStringValue("name", name.c_str());
hdg = azimuth;
pitch = elevation;
roll = rotation;
@ -74,15 +97,39 @@ bool FGAIBallistic::init(bool search_in_AI_path) {
}
void FGAIBallistic::bind() {
// FGAIBase::bind();
// FGAIBase::bind();
props->tie("sim/time/elapsed-sec",
SGRawValueMethods<FGAIBallistic,double>(*this,
&FGAIBallistic::_getTime));
props->tie("material/load-resistance",
SGRawValuePointer<double>(&load_resistance));
props->tie("material/solid",
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() {
// FGAIBase::unbind();
// FGAIBase::unbind();
props->untie("sim/time/elapsed-sec");
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) {
@ -147,13 +194,17 @@ void FGAIBallistic::setRandom(bool r) {
random = r;
}
void FGAIBallistic::setImpact(bool i) {
impact = i;
}
void FGAIBallistic::setName(const string& n) {
name = n;
}
void FGAIBallistic::Run(double dt) {
life_timer += dt;
// cout << "life timer 1" << life_timer << dt << endl;
// cout << "life timer 1" << life_timer << dt << endl;
if (life_timer > life) setDie(true);
double speed_north_deg_sec;
@ -231,14 +282,56 @@ void FGAIBallistic::Run(double dt) {
// recalculate total speed
speed = sqrt( vs * vs + hs * hs) / SG_KT_TO_FPS;
if (impact && !impact_data && vs < 0)
handle_impact();
// set destruction flag if altitude less than sea level -1000
if (altitude_ft < -1000.0) setDie(true);
if (altitude_ft < -1000.0)
setDie(true);
} // end Run
double FGAIBallistic::_getTime() const {
// cout << "life timer 2" << life_timer << endl;
// cout << "life timer 2" << life_timer << endl;
return life_timer;
}
void FGAIBallistic::handle_impact() {
double elevation_m;
const SGMaterial* material;
// try terrain intersection
if (!globals->get_scenery()->get_elevation_m(pos.getLatitudeDeg(), pos.getLongitudeDeg(),
10000.0, elevation_m, &material))
return;
if (material) {
const vector<string> names = material->get_names();
if (!names.empty())
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;
}
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;
}
}
// end AIBallistic

View file

@ -54,10 +54,12 @@ public:
void setNoRoll( bool nr );
void setRandom( bool r );
void setName(const string&);
void setImpact(bool i);
double _getTime() const;
virtual const char* getTypeString(void) const { return "ballistic"; }
static const double slugs_to_kgs; //conversion factor
private:
@ -75,9 +77,25 @@ private:
double Cd; // drag coefficient
double mass; // slugs
bool random; // modifier for Cd
double ht_agl_ft; // height above ground level
double load_resistance; // ground load resistanc N/m^2
bool solid; // if true ground is solid for FDMs
bool impact; // if true an impact point on the terrain is calculated
bool impact_data; // if true impact data have been set
double impact_energy;
double impact_speed;
double impact_lat;
double impact_lon;
double impact_elev;
string mat_name;
string name;
void Run(double dt);
void handle_impact();
FGAIBase* ai;
};
#endif // _FG_AIBALLISTIC_HXX

View file

@ -210,6 +210,7 @@ bool FGSubmodelMgr::release(submodel* sm, double dt)
ballist->setStabilisation(sm->aero_stabilised);
ballist->setNoRoll(sm->no_roll);
ballist->setName(sm->name);
ballist->setImpact(sm->impact);
ai->attach(ballist);
if (sm->count > 0)
@ -487,6 +488,7 @@ void FGSubmodelMgr::setData(int id, string& path, bool serviceable)
sm->weight = entry_node->getDoubleValue("weight", 0.25);
sm->aero_stabilised = entry_node->getBoolValue("aero-stabilised", true);
sm->no_roll = entry_node->getBoolValue("no-roll", false);
sm->impact = entry_node->getBoolValue("impact", false);
sm->contents_node = fgGetNode(entry_node->getStringValue("contents", "none"), false);
sm->speed_node = fgGetNode(entry_node->getStringValue("speed-node", "none"), false);

View file

@ -59,6 +59,7 @@ public:
int id;
bool no_roll;
bool serviceable;
bool impact;
}
submodel;
@ -82,6 +83,7 @@ public:
double mass;
int id;
bool no_roll;
bool impact;
}
IC_struct;