]> git.mxchange.org Git - flightgear.git/blobdiff - src/AIModel/AIBallistic.cxx
- Added ultra-light traffic is now a separate traffic class that can have its
[flightgear.git] / src / AIModel / AIBallistic.cxx
index 6dd53562dd83ed172caae9efe169390c40df2cb8..c42d0d775c3d936890efb7e3d71b7d2591c91b75 100644 (file)
@@ -36,6 +36,7 @@ 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),
@@ -47,8 +48,7 @@ FGAIBallistic::FGAIBallistic() :
     _solid(false),
     _report_collision(false),
     _report_impact(false),
-    _impact_report_node(fgGetNode("/ai/models/model-impact", true)),
-    _mat_name("")
+    _impact_report_node(fgGetNode("/ai/models/model-impact", true))
 {
     no_roll = false;
 }
@@ -87,12 +87,12 @@ void FGAIBallistic::readFromScenario(SGPropertyNode* scFileNode) {
 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;
@@ -112,8 +112,6 @@ void FGAIBallistic::bind() {
                 SGRawValuePointer<bool>(&_solid));
     props->tie("altitude-agl-ft",
                 SGRawValuePointer<double>(&_ht_agl_ft));
-    props->tie("sub-id",
-                SGRawValuePointer<int>(&_subID));
 }
 
 void FGAIBallistic::unbind() {
@@ -122,7 +120,6 @@ void FGAIBallistic::unbind() {
     props->untie("material/load-resistance");
     props->untie("material/solid");
     props->untie("altitude-agl-ft");
-    props->untie("sub-id");
 }
 
 void FGAIBallistic::update(double dt) {
@@ -227,19 +224,14 @@ void FGAIBallistic::Run(double dt) {
     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
-    double hs;
-
     //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).
+    double Cdm;
+
     if (Mach < 0.7)
         Cdm = 0.0125 * Mach + _Cd;
     else if (Mach < 1.2 )
@@ -261,18 +253,19 @@ void FGAIBallistic::Run(double dt) {
         speed = 0.0;
 
     double speed_fps = speed * SG_KT_TO_FPS;
+    double hs;
 
     // calculate vertical and horizontal speed components
     if (speed == 0.0) {
         hs = vs = 0.0;
     } else {
-        vs = sin( pitch * SG_DEGREES_TO_RADIANS ) * speed_fps;
-        hs = cos( pitch * SG_DEGREES_TO_RADIANS ) * speed_fps;
+        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) {
@@ -281,8 +274,8 @@ void FGAIBallistic::Run(double dt) {
     }
 
     // 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()
@@ -297,12 +290,17 @@ void FGAIBallistic::Run(double dt) {
     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;
@@ -336,34 +334,38 @@ void FGAIBallistic::handle_impact() {
 
     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 properties
     if (_ht_agl_ft <= 0) {
         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 *collision = manager->calcCollision(pos.getElevationFt(),
+    const FGAIBase *object = manager->calcCollision(pos.getElevationFt(),
             pos.getLatitudeDeg(),pos.getLongitudeDeg(), _fuse_range);
 
-    if (collision) {
-        SG_LOG(SG_GENERAL, SG_DEBUG, "AIBallistic: HIT!");
-        report_impact(pos.getElevationM(), collision);
+    if (object) {
+        SG_LOG(SG_GENERAL, SG_DEBUG, "AIBallistic: object hit");
+        report_impact(pos.getElevationM(), object);
         _collision_reported = true;
     }
 }