]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/YASim/Airplane.cpp
FGPUIDialog: fix reading from already free'd memory.
[flightgear.git] / src / FDM / YASim / Airplane.cpp
index 07d434e09c7d4eff65f0fc755ecd0edf90f94562..15fd4d683bb1d1acf3096425bf379ff9a59f4a6e 100644 (file)
@@ -1,3 +1,7 @@
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
 #include "Atmosphere.hpp"
 #include "ControlMap.hpp"
 #include "Gear.hpp"
@@ -28,8 +32,6 @@ const float STHRESH = 1;
 // oscillate.
 const float SOLVE_TWEAK = 0.3226;
 
-const float GRAV = 9.8f;
-
 Airplane::Airplane()
 {
     _emptyWeight = 0;
@@ -53,6 +55,8 @@ Airplane::Airplane()
     _liftRatio = 1;
     _cruiseAoA = 0;
     _tailIncidence = 0;
+
+    _failureMsg = 0;
 }
 
 Airplane::~Airplane()
@@ -125,15 +129,18 @@ void Airplane::getPilotAccel(float* out)
 
     // Gravity
     Glue::geodUp(s->pos, out);
-    Math::mul3(GRAV, out, out);
+    Math::mul3(-9.8f, out, out);
+    Math::vmul33(s->orient, out, out);
+    out[0] = -out[0];
 
     // The regular acceleration
     float tmp[3];
-    Math::mul3(-1, s->acc, tmp);
-    Math::add3(tmp, out, out);
-
     // Convert to aircraft coordinates
-    Math::vmul33(s->orient, out, out);
+    Math::vmul33(s->orient, s->acc, tmp);
+    tmp[1] = -tmp[1];
+    tmp[2] = -tmp[2];
+
+    Math::add3(tmp, out, out);
 
     // FIXME: rotational & centripetal acceleration needed
 }
@@ -419,7 +426,7 @@ float Airplane::getTailIncidence()
     return _tailIncidence;
 }
 
-char* Airplane::getFailureMsg()
+const char* Airplane::getFailureMsg()
 {
     return _failureMsg;
 }
@@ -509,6 +516,10 @@ float Airplane::compileFuselage(Fuselage* f)
     float fwd[3];
     Math::sub3(f->front, f->back, fwd);
     float len = Math::mag3(fwd);
+    if (len == 0) {
+        _failureMsg = "Zero length fuselage";
+       return 0;
+    }
     float wid = f->width;
     int segs = (int)Math::ceil(len/wid);
     float segWgt = len*wid/segs;
@@ -595,7 +606,7 @@ void Airplane::compileContactPoints()
     // Give it a spring constant such that at full compression it will
     // hold up 10 times the planes mass.  That's about right.  Yeah.
     float mass = _model.getBody()->getTotalMass();
-    float spring = (1/DIST) * GRAV * 10.0f * mass;
+    float spring = (1/DIST) * 9.8f * 10.0f * mass;
     float damp = 2 * Math::sqrt(spring * mass);
 
     int i;
@@ -667,8 +678,8 @@ void Airplane::compile()
         t->handle = body->addMass(0, t->pos);
         totalFuel += t->cap;
     }
-    _cruiseWeight = _emptyWeight + totalFuel*0.5f;
-    _approachWeight = _emptyWeight + totalFuel*0.2f;
+    _cruiseWeight = _emptyWeight + totalFuel*_cruiseFuel;
+    _approachWeight = _emptyWeight + totalFuel*_approachFuel;
 
     body->recalc();
 
@@ -690,6 +701,10 @@ void Airplane::compile()
         _model.setGroundEffect(gepos, gespan, 0.15f);
     }
 
+    // solve function below resets failure message
+    // so check if we have any problems and abort here
+    if (_failureMsg) return;
+
     solveGear();
     if(_wing && _tail) solve();
     else
@@ -787,8 +802,7 @@ void Airplane::setupWeights(bool isApproach)
 
 void Airplane::runCruise()
 {
-    __builtin_printf("runCruise()\n");
-    setupState(_cruiseAoA, _cruiseSpeed,_approachGlideAngle, &_cruiseState);
+    setupState(_cruiseAoA, _cruiseSpeed,_cruiseGlideAngle, &_cruiseState);
     _model.setState(&_cruiseState);
     _model.setAir(_cruiseP, _cruiseT,
                   Atmosphere::calcStdDensity(_cruiseP, _cruiseT));
@@ -831,7 +845,6 @@ void Airplane::runCruise()
 
 void Airplane::runApproach()
 {
-    __builtin_printf("runApproach()\n");
     setupState(_approachAoA, _approachSpeed,_approachGlideAngle, &_approachState);
     _model.setState(&_approachState);
     _model.setAir(_approachP, _approachT,
@@ -940,7 +953,7 @@ void Airplane::solve()
        runCruise();
 
        _model.getThrust(tmp);
-        float thrust = tmp[0] + GRAV * _cruiseWeight * Math::sin(_cruiseGlideAngle);
+        float thrust = tmp[0] + _cruiseWeight * Math::sin(_cruiseGlideAngle) * 9.81;
 
        _model.getBody()->getAccel(tmp);
         Math::tmul33(_cruiseState.orient, tmp, tmp);
@@ -981,7 +994,7 @@ void Airplane::solve()
        float pitch1 = tmp[1];
 
        // Now calculate:
-       float awgt = GRAV * _approachWeight;
+       float awgt = 9.8f * _approachWeight;
 
        float dragFactor = thrust / (thrust-xforce);
        float liftFactor = awgt / (awgt+alift);
@@ -989,10 +1002,8 @@ void Airplane::solve()
        float tailDelta = -pitch0 * (ARCMIN/(pitch1-pitch0));
 
         // Sanity:
-        if(dragFactor <= 0 || liftFactor <= 0) {
-            __builtin_printf("NEGATIVE drag %f lift %f\n", dragFactor, liftFactor);
+        if(dragFactor <= 0 || liftFactor <= 0)
             break;
-        }
 
         // And the elevator control in the approach.  This works just
         // like the tail incidence computation (it's solving for the
@@ -1011,7 +1022,7 @@ void Airplane::solve()
         // Now apply the values we just computed.  Note that the
         // "minor" variables are deferred until we get the lift/drag
         // numbers in the right ballpark.
-        __builtin_printf("Apply drag %f lift %f\n", dragFactor, liftFactor);
+
        applyDragFactor(dragFactor);
        applyLiftRatio(liftFactor);
 
@@ -1022,8 +1033,6 @@ void Airplane::solve()
            continue;
        }
 
-        __builtin_printf("Apply aoa %f tail %f\n", SOLVE_TWEAK*aoaDelta, SOLVE_TWEAK*tailDelta);
-
        // OK, now we can adjust the minor variables:
        _cruiseAoA += SOLVE_TWEAK*aoaDelta;
        _tailIncidence += SOLVE_TWEAK*tailDelta;
@@ -1040,8 +1049,6 @@ void Airplane::solve()
             if(abs(elevDelta) < STHRESH*0.0001)
                 break;
 
-            __builtin_printf("Apply elev %f\n", SOLVE_TWEAK*elevDelta);
-
             // Otherwise, adjust and do the next iteration
             _approachElevator.val += SOLVE_TWEAK * elevDelta;
             if(abs(_approachElevator.val) > 1) {