]> git.mxchange.org Git - flightgear.git/blobdiff - src/Environment/ridge_lift.cxx
just in case that someone does not like ridge lift at all, set the property
[flightgear.git] / src / Environment / ridge_lift.cxx
index 0edbe2e85505bac6e920d5077391afc385819229..8289953b7025c3623192167d089f77c18208e0c8 100644 (file)
@@ -51,6 +51,10 @@ static string CreateIndexedPropertyName(string Property, int index)
        return Property + "[" + tmp + "]";
 }
 
+static inline double sign(double x) {
+       return x == 0 ? 0 : x > 0 ? 1.0 : -1.0;
+}
+
 static const   double BOUNDARY1_m = 40.0;
 
 //constructor
@@ -64,6 +68,11 @@ FGRidgeLift::FGRidgeLift ()
 
        strength = 0.0;
        timer = 0.0;
+       for( int i = 0; i < 5; i++ )
+               probe_elev_m[i] = probe_lat_deg[i] = probe_lon_deg[i] = 0.0;
+
+       for( int i = 0; i < 4; i++ )
+               slope[i] = 0.0;
 }
 
 //destructor
@@ -74,6 +83,8 @@ FGRidgeLift::~FGRidgeLift()
 
 void FGRidgeLift::init(void)
 {
+       _enabled_node = fgGetNode( "/environment/ridge-lift/enabled", false );
+
        _ridge_lift_fps_node = fgGetNode("/environment/ridge-lift-fps", true);
        _surface_wind_from_deg_node =
                        fgGetNode("/environment/config/boundary/entry[0]/wind-from-heading-deg"
@@ -131,8 +142,15 @@ void FGRidgeLift::unbind() {
 
 void FGRidgeLift::update(double dt) {
 
-       //get the windspeed at ground level
+       if( _enabled_node && false == _enabled_node->getBoolValue() ) {
+               if( strength != 0.0 ) {
+                       strength = 0.0;
+                       _ridge_lift_fps_node->setDoubleValue( 0 );
+               }
+               return;
+       }
 
+       //get the windspeed at ground level
        double ground_wind_from_rad = _surface_wind_from_deg_node->getDoubleValue() * SG_DEGREES_TO_RADIANS;
        double ground_wind_speed_mps = _surface_wind_speed_node->getDoubleValue() * SG_NM_TO_METER / 3600;
 
@@ -151,14 +169,15 @@ void FGRidgeLift::update(double dt) {
        
                for (int i = 0; i < sizeof(probe_lat_rad)/sizeof(probe_lat_rad[0]); i++) {
                        double probe_radius_ratio = dist_probe_m[i]/earth_rad_m;
+                       double sin_probe_radius_ratio = sin(probe_radius_ratio);
 
                        probe_lat_rad[i] = asin(sin(user_latitude_rad)*cos(probe_radius_ratio)
-                                       +cos(user_latitude_rad)*sin(probe_radius_ratio)*cos(ground_wind_from_rad));
+                                       +cos(user_latitude_rad)*sin_probe_radius_ratio*cos(ground_wind_from_rad));
                        if (probe_lat_rad[i] < SG_EPSILON ) {
                                probe_lon_rad[i] = user_latitude_rad; // probe on a pole        
                        } else {
                                probe_lon_rad[i] = fmod((user_longitude_rad+asin(sin(ground_wind_from_rad)
-                                                       *sin(probe_radius_ratio)/cos(probe_lat_rad[i]))+SG_PI)
+                                                       *sin_probe_radius_ratio/cos(probe_lat_rad[i]))+SG_PI)
                                                ,SGD_2PI)-SG_PI;
                        }
                        probe_lat_deg[i]= probe_lat_rad[i] * SG_RADIANS_TO_DEGREES;
@@ -166,10 +185,11 @@ void FGRidgeLift::update(double dt) {
                }
        
                for (int i = 0; i < sizeof(probe_elev_m)/sizeof(probe_elev_m[0]); i++) {
+                       double elevation = 0;
                        if (globals->get_scenery()->get_elevation_m(SGGeod::fromGeodM(
-                               SGGeod::fromRad(probe_lon_rad[i],probe_lat_rad[i]), 20000), alt, 0)) {
-                               if ( alt > 0.1 ) { 
-                                       probe_elev_m[i] = alt
+                               SGGeod::fromRad(probe_lon_rad[i],probe_lat_rad[i]), 20000), elevation, 0)) {
+                               if ( elevation > 0.1 ) { 
+                                       probe_elev_m[i] = elevation
                                } else { 
                                        probe_elev_m[i] = 0.1 ;
                                }
@@ -189,10 +209,10 @@ void FGRidgeLift::update(double dt) {
                        adj_slope[i] = sin(atan(5.0 * pow ( (fabs(slope[i])),1.7) ) ) *sign(slope[i]);
        
                //adjustment
-               adj_slope[0] = 0.2 * adj_slope[0];
-               adj_slope[1] = 0.2 * adj_slope[1];
+               adj_slope[0] *= 0.2;
+               adj_slope[1] *= 0.2;
                if ( adj_slope [2] < 0.0 ) {
-                       adj_slope[2] = 0.5 * adj_slope[2];
+                       adj_slope[2] *= 0.5;
                } else {
                        adj_slope[2] = 0.0 ;
                }
@@ -200,7 +220,7 @@ void FGRidgeLift::update(double dt) {
                if ( ( adj_slope [0] >= 0.0 ) && ( adj_slope [3] < 0.0 ) ) {
                        adj_slope[3] = 0.0;
                } else {
-                       adj_slope[3] = 0.2 * adj_slope[3];
+                       adj_slope[3] *= 0.2;
                }
                lift_factor = adj_slope[0]+adj_slope[1]+adj_slope[2]+adj_slope[3];
        
@@ -212,7 +232,7 @@ void FGRidgeLift::update(double dt) {
        double user_altitude_agl_m = _user_altitude_agl_ft_node->getDoubleValue() * SG_FEET_TO_METER;
        
        //boundaries
-  double boundary2_m = 130.0; // in the lift
+       double boundary2_m = 130.0; // in the lift
        if (lift_factor < 0.0) { // in the sink
                double highest_probe_temp= max ( probe_elev_m[1], probe_elev_m[2] );
                double highest_probe_downwind_m= max ( highest_probe_temp, probe_elev_m[3] );
@@ -233,6 +253,5 @@ void FGRidgeLift::update(double dt) {
        
        //the updraft, finally, in ft per second
        strength = fgGetLowPass( strength, lift_mps * SG_METER_TO_FEET, dt );
-//     if(isnan(strength)) strength=0; 
-        _ridge_lift_fps_node->setDoubleValue( strength );
+       _ridge_lift_fps_node->setDoubleValue( strength );
 }