]> git.mxchange.org Git - flightgear.git/commitdiff
Don't duplicate userpos in in AIObjects.
authorJames Turner <zakalawe@mac.com>
Sat, 16 Mar 2013 14:47:41 +0000 (14:47 +0000)
committerJames Turner <zakalawe@mac.com>
Sat, 16 Mar 2013 16:53:12 +0000 (16:53 +0000)
src/AIModel/AIBallistic.cxx
src/AIModel/AIBallistic.hxx
src/AIModel/AIBase.cxx
src/AIModel/AIBase.hxx
src/AIModel/AIStorm.cxx
src/AIModel/AIThermal.cxx
src/AIModel/AIWingman.cxx

index fc4ea30c19d4a5439a9285101f18d31045a8fb6b..962795e88fc743e09ca074fa69fd001fc62bc33b 100644 (file)
@@ -255,9 +255,9 @@ void FGAIBallistic::bind() {
 
 }
 
-void FGAIBallistic::update(double dt) {
+void FGAIBallistic::update(double dt)
+{
     FGAIBase::update(dt);
-    _setUserPos();
 
     if (_slave_to_ac){
         slaveToAC(dt);
@@ -623,7 +623,7 @@ void FGAIBallistic::slaveToAC(double dt){
         pch = manager->get_user_pitch();
         rll = manager->get_user_roll();
 //        agl = manager->get_user_agl();
-        setOffsetPos(userpos, hdg, pch, rll);
+        setOffsetPos(globals->get_aircraft_position(), hdg, pch, rll);
         setSpeed(manager->get_user_speed());
     }
 
@@ -1011,15 +1011,11 @@ void FGAIBallistic::report_impact(double elevation, const FGAIBase *object)
     _impact_report_node->setStringValue(props->getPath());
 }
 
-SGVec3d FGAIBallistic::getCartUserPos() const {
-    SGVec3d cartUserPos = SGVec3d::fromGeod(userpos);
-    return cartUserPos;
-}
-
 SGVec3d FGAIBallistic::getCartHitchPos() const{
 
     // convert geodetic positions to geocentered
-    SGVec3d cartuserPos = SGVec3d::fromGeod(userpos);
+    SGVec3d cartuserPos = globals->get_aircraft_position_cart();
+    
     //SGVec3d cartPos = getCartPos();
 
     // Transform to the right coordinate frame, configuration is done in
@@ -1031,7 +1027,7 @@ SGVec3d FGAIBallistic::getCartHitchPos() const{
             -_z_offset * SG_FEET_TO_METER);
 
     // Transform the user position to the horizontal local coordinate system.
-    SGQuatd hlTrans = SGQuatd::fromLonLat(userpos);
+    SGQuatd hlTrans = SGQuatd::fromLonLat(globals->get_aircraft_position());
 
     // and postrotate the orientation of the user model wrt the horizontal
     // local frame
@@ -1103,7 +1099,7 @@ double FGAIBallistic::getRelBrgHitchToUser() const {
     //calculate the relative bearing 
     double az1, az2, distance;
 
-    geo_inverse_wgs_84(_offsetpos, userpos, &az1, &az2, &distance);
+    geo_inverse_wgs_84(_offsetpos, globals->get_aircraft_position(), &az1, &az2, &distance);
 
     double rel_brg = az1 - hdg;
 
@@ -1116,14 +1112,14 @@ double FGAIBallistic::getElevHitchToUser() const {
 
     //calculate the distance from the user position
     SGVec3d carthitchPos = getCartHitchPos();
-    SGVec3d cartuserPos = getCartUserPos();
+    SGVec3d cartuserPos = globals->get_aircraft_position_cart();
 
     SGVec3d diff = cartuserPos - carthitchPos;
 
     double distance = norm(diff);
     double angle = 0;
 
-    double daltM = userpos.getElevationM() - _offsetpos.getElevationM();
+    double daltM = globals->get_aircraft_position().getElevationM() - _offsetpos.getElevationM();
 
     // now the angle, positive angles are upwards
     if (fabs(distance) < SGLimits<float>::min()) {
index 9b28efdae9144de7c0940f4473bbe8306eb36ca1..fbbeeb4d7eb54f28560b65569658d6741c6c3478 100644 (file)
@@ -230,7 +230,6 @@ private:
     //void setOffsetPos(SGGeod pos, double heading, double pitch, double roll);
     //void setOffsetVelocity(double dt, SGGeod pos);
 
-    SGVec3d getCartUserPos() const;
     SGVec3d getCartOffsetPos(SGGeod pos, double heading, double pitch, double roll) const;
 
     //double getDistanceLoadToHitch() const;
index 999659c64608eda35754ba28a3e3fffd12d73d1d..01c0c26916aa2c6a1f8f8ead4960abf8c30f4eec 100644 (file)
@@ -100,8 +100,6 @@ FGAIBase::FGAIBase(object_type ot, bool enableHot) :
     _roll_offset = 0;
     _yaw_offset = 0;
 
-    userpos = SGGeod::fromDeg(0, 0);
-
     pos = SGGeod::fromDeg(0, 0);
     speed = 0;
     altitude_ft = 0;
@@ -479,106 +477,68 @@ void FGAIBase::removeSoundFx() {
     }
 }
 
-double FGAIBase::UpdateRadar(FGAIManager* manager) {
+double FGAIBase::UpdateRadar(FGAIManager* manager)
+{
     bool control = fgGetBool("/sim/controls/radar", true);
 
     if(!control) return 0;
 
-    double radar_range_ft2 = fgGetDouble("/instrumentation/radar/range");
+    double radar_range_m = fgGetDouble("/instrumentation/radar/range");
     bool force_on = fgGetBool("/instrumentation/radar/debug-mode", false);
-    radar_range_ft2 *= SG_NM_TO_METER * SG_METER_TO_FEET * 1.1; // + 10%
-    radar_range_ft2 *= radar_range_ft2;
-
-    double user_latitude  = manager->get_user_latitude();
-    double user_longitude = manager->get_user_longitude();
-    double lat_range = fabs(pos.getLatitudeDeg() - user_latitude) * ft_per_deg_lat;
-    double lon_range = fabs(pos.getLongitudeDeg() - user_longitude) * ft_per_deg_lon;
-    double range_ft2 = lat_range*lat_range + lon_range*lon_range;
-
-    //
-    // Test whether the target is within radar range.
-    //
-    in_range = (range_ft2 && (range_ft2 <= radar_range_ft2));
-
-    if ( in_range || force_on ) {
-        props->setBoolValue("radar/in-range", true);
-
-        // copy values from the AIManager
-        double user_altitude  = manager->get_user_altitude();
-        double user_heading   = manager->get_user_heading();
-        double user_pitch     = manager->get_user_pitch();
-        //double user_yaw       = manager->get_user_yaw();
-        //double user_speed     = manager->get_user_speed();
-
-        // calculate range to target in feet and nautical miles
-        double range_ft = sqrt( range_ft2 );
-        range = range_ft / 6076.11549;
-
-        // calculate bearing to target
-        if (pos.getLatitudeDeg() >= user_latitude) {
-            bearing = atan2(lat_range, lon_range) * SG_RADIANS_TO_DEGREES;
-            if (pos.getLongitudeDeg() >= user_longitude) {
-                bearing = 90.0 - bearing;
-            } else {
-                bearing = 270.0 + bearing;
-            }
-        } else {
-            bearing = atan2(lon_range, lat_range) * SG_RADIANS_TO_DEGREES;
-            if (pos.getLongitudeDeg() >= user_longitude) {
-                bearing = 180.0 - bearing;
-            } else {
-                bearing = 180.0 + bearing;
-            }
-        }
-
-        // This is an alternate way to compute bearing and distance which
-        // agrees with the original scheme within about 0.1 degrees.
-        //
-        // Point3D start( user_longitude * SGD_DEGREES_TO_RADIANS,
-        //                user_latitude * SGD_DEGREES_TO_RADIANS, 0 );
-        // Point3D dest( pos.getLongitudeRad(), pos.getLatitudeRad(), 0 );
-        // double gc_bearing, gc_range;
-        // calc_gc_course_dist( start, dest, &gc_bearing, &gc_range );
-        // gc_range *= SG_METER_TO_NM;
-        // gc_bearing *= SGD_RADIANS_TO_DEGREES;
-        // printf("orig b = %.3f %.2f  gc b= %.3f, %.2f\n",
-        //        bearing, range, gc_bearing, gc_range);
-
-        // calculate look left/right to target, without yaw correction
-        horiz_offset = bearing - user_heading;
-        if (horiz_offset > 180.0) horiz_offset -= 360.0;
-        if (horiz_offset < -180.0) horiz_offset += 360.0;
-
-        // calculate elevation to target
-        elevation = atan2( altitude_ft - user_altitude, range_ft ) * SG_RADIANS_TO_DEGREES;
-
-        // calculate look up/down to target
-        vert_offset = elevation - user_pitch;
-
-        /* this calculation needs to be fixed, but it isn't important anyway
-        // calculate range rate
-        double recip_bearing = bearing + 180.0;
-        if (recip_bearing > 360.0) recip_bearing -= 360.0;
-        double my_horiz_offset = recip_bearing - hdg;
-        if (my_horiz_offset > 180.0) my_horiz_offset -= 360.0;
-        if (my_horiz_offset < -180.0) my_horiz_offset += 360.0;
-        rdot = (-user_speed * cos( horiz_offset * SG_DEGREES_TO_RADIANS ))
-        +(-speed * 1.686 * cos( my_horiz_offset * SG_DEGREES_TO_RADIANS ));
-        */
-
-        // now correct look left/right for yaw
-        // horiz_offset += user_yaw; // FIXME: WHY WOULD WE WANT TO ADD IN SIDE-SLIP HERE?
-
-        // calculate values for radar display
-        y_shift = range * cos( horiz_offset * SG_DEGREES_TO_RADIANS);
-        x_shift = range * sin( horiz_offset * SG_DEGREES_TO_RADIANS);
-        rotation = hdg - user_heading;
-        if (rotation < 0.0) rotation += 360.0;
-        ht_diff = altitude_ft - user_altitude;
-
+    radar_range_m *= SG_NM_TO_METER  * 1.1; // + 10%
+    radar_range_m *= radar_range_m; // squared
+    
+    double d2 = distSqr(SGVec3d::fromGeod(pos), globals->get_aircraft_position_cart());
+    double range_ft = sqrt(d2) * SG_METER_TO_FEET;
+    
+    if (!force_on && (d2 > radar_range_m)) {
+        return range_ft * range_ft;
     }
+    
+    props->setBoolValue("radar/in-range", true);
+
+    // copy values from the AIManager
+    double user_heading   = manager->get_user_heading();
+    double user_pitch     = manager->get_user_pitch();
+  
+    range = range_ft * SG_FEET_TO_METER * SG_METER_TO_NM;
+
+    // calculate bearing to target
+    bearing = SGGeodesy::courseDeg(globals->get_aircraft_position(), pos);
+
+    // calculate look left/right to target, without yaw correction
+    horiz_offset = bearing - user_heading;
+    SG_NORMALIZE_RANGE(horiz_offset, -180.0, 180.0);
+   
+    // calculate elevation to target
+    ht_diff = altitude_ft - globals->get_aircraft_position().getElevationFt();
+    elevation = atan2( ht_diff, range_ft ) * SG_RADIANS_TO_DEGREES;
+
+    // calculate look up/down to target
+    vert_offset = elevation - user_pitch;
+
+    /* this calculation needs to be fixed, but it isn't important anyway
+    // calculate range rate
+    double recip_bearing = bearing + 180.0;
+    if (recip_bearing > 360.0) recip_bearing -= 360.0;
+    double my_horiz_offset = recip_bearing - hdg;
+    if (my_horiz_offset > 180.0) my_horiz_offset -= 360.0;
+    if (my_horiz_offset < -180.0) my_horiz_offset += 360.0;
+    rdot = (-user_speed * cos( horiz_offset * SG_DEGREES_TO_RADIANS ))
+    +(-speed * 1.686 * cos( my_horiz_offset * SG_DEGREES_TO_RADIANS ));
+    */
+
+    // now correct look left/right for yaw
+    // horiz_offset += user_yaw; // FIXME: WHY WOULD WE WANT TO ADD IN SIDE-SLIP HERE?
+
+    // calculate values for radar display
+    y_shift = range * cos( horiz_offset * SG_DEGREES_TO_RADIANS);
+    x_shift = range * sin( horiz_offset * SG_DEGREES_TO_RADIANS);
+    
+    rotation = hdg - user_heading;
+    SG_NORMALIZE_RANGE(rotation, 0.0, 360.0);
 
-    return range_ft2;
+    return range_ft * range_ft;
 }
 
 /*
@@ -637,12 +597,6 @@ void FGAIBase::_setLatitude ( double latitude )  {
     pos.setLatitudeDeg(latitude);
 }
 
-void FGAIBase::_setUserPos(){
-    userpos.setLatitudeDeg(manager->get_user_latitude());
-    userpos.setLongitudeDeg(manager->get_user_longitude());
-    userpos.setElevationM(manager->get_user_altitude() * SG_FEET_TO_METER);
-}
-
 void FGAIBase::_setSubID( int s ) {
     _subID = s;
 }
index 98f889aa593e401018b78c9d8d6196a5fa13dfd0..70e7c97184c834ab0ebbea4ad8349cbf52661647 100644 (file)
@@ -140,9 +140,6 @@ public:
     std::string _name;
     string _parent;
 
-    SGGeod userpos;
-
-
 protected:
     /**
      * Tied-properties helper, record nodes which are tied for easy un-tie-ing
@@ -255,7 +252,6 @@ public:
     void _setLongitude( double longitude );
     void _setLatitude ( double latitude );
     void _setSubID( int s );
-    void _setUserPos();
 
     double _getAltitudeAGL(SGGeod inpos, double start);
 
index a4241df465c2f3e17399dae1a0c687f3c5589a04..e11411777f9493555b97be843f023e24754ca34e 100644 (file)
@@ -155,17 +155,11 @@ void FGAIStorm::Run(double dt) {
    // ***************************************************
 
    // copy user's position from the AIManager
-   double user_latitude  = manager->get_user_latitude();
-   double user_longitude = manager->get_user_longitude();
-   double user_altitude  = manager->get_user_altitude();
-
-   // calculate range to target in feet and nautical miles
-   double lat_range = fabs(pos.getLatitudeDeg() - user_latitude) * ft_per_deg_lat;
-   double lon_range = fabs(pos.getLongitudeDeg() - user_longitude) * ft_per_deg_lon;
-   double range_ft = sqrt(lat_range*lat_range + lon_range*lon_range);
-   range = range_ft / 6076.11549;
-
-   if (range < (diameter * 0.5) &&
+   double d = dist(SGVec3d::fromGeod(pos), globals->get_aircraft_position_cart());
+   double rangeNm =  d * SG_METER_TO_NM;
+   double user_altitude = globals->get_aircraft_position().getElevationFt();
+    
+   if (rangeNm < (diameter * 0.5) &&
        user_altitude > (altitude_ft - 1000.0) &&
        user_altitude < height) {
               turb_mag_node->setDoubleValue(strength_norm);
index 4b1d653f80a4648ff67277154876ac048e4690c4..7007bb2491c9ecc79a3ae7a5351134e464895585 100644 (file)
@@ -201,14 +201,6 @@ double slice_center_lat;
 
 
 
-// **************************************
-// various variables relative to the user
-// **************************************
-
-double user_latitude  = manager->get_user_latitude();
-double user_longitude = manager->get_user_longitude();
-double user_altitude  = manager->get_user_altitude(); // MSL
-
 //we need to know the thermal foot AGL altitude
 
 
@@ -227,6 +219,9 @@ if (dt_count >= 10.0 ) {
 }
 
 //user altitude relative to the thermal height, seen AGL from the thermal foot
+    
+
+double user_altitude = globals->get_aircraft_position().getElevationFt();
 if ( user_altitude < 1.0 ) { user_altitude = 1.0 ;}; // an ugly way to avoid NaNs for users at alt 0
 double user_altitude_agl= ( user_altitude - ground_elev_ft ) ;
 alt_rel = user_altitude_agl / altitude_agl_ft;
@@ -343,14 +338,9 @@ double dt_slice_lat = dt_slice_lat_FT / ft_per_deg_lat;
 slice_center_lon = thermal_foot_lon + dt_slice_lon;
 slice_center_lat = thermal_foot_lat + dt_slice_lat;
 
-double dist_center_lon = fabs(slice_center_lon - user_longitude)* ft_per_deg_lon;
-double dist_center_lat = fabs(slice_center_lat - user_latitude)* ft_per_deg_lat;
-
-double dist_center_FT = sqrt ( dist_center_lon*dist_center_lon + dist_center_lat*dist_center_lat ); // feet
-
-dist_center = dist_center_FT/ 6076.11549; //nautic miles
-
-
+dist_center = SGGeodesy::distanceNm(SGGeod::fromDeg(slice_center_lon, slice_center_lat),
+                                    globals->get_aircraft_position());
+    
 // Now we can calculate Vup
 
 if ( max_strength >=0.0 ) { // this is a thermal
index 10ffd89cca9c107483dd7140e0cee9054762ac54..7c14789971ae5079450a561e306e0f2180eee8a7 100644 (file)
@@ -243,7 +243,6 @@ void FGAIWingman::formateToAC(double dt){
     double p_hdg, p_pch, p_rll, p_agl, p_ht, p_wow = 0;
 
     setTgtOffsets(dt, 25);
-    _setUserPos();
 
     if (_pnode != 0) {
         setParentPos();
@@ -257,8 +256,8 @@ void FGAIWingman::formateToAC(double dt){
         p_hdg = manager->get_user_heading();
         p_pch = manager->get_user_pitch();
         p_rll = manager->get_user_roll();
-        p_ht  = manager->get_user_altitude();
-        setOffsetPos(userpos, p_hdg,p_pch, p_rll);
+        p_ht  = globals->get_aircraft_position().getElevationFt();
+        setOffsetPos(globals->get_aircraft_position(), p_hdg,p_pch, p_rll);
         setSpeed(manager->get_user_speed());
     }
 
@@ -341,7 +340,6 @@ void FGAIWingman::Join(double dt) {
     double p_hdg, p_pch, p_rll = 0;
 
     setTgtOffsets(dt, 25);
-    _setUserPos();
 
     if (_pnode != 0) {
         setParentPos();
@@ -355,7 +353,7 @@ void FGAIWingman::Join(double dt) {
         p_hdg = manager->get_user_heading();
         p_pch = manager->get_user_pitch();
         p_rll = manager->get_user_roll();
-        setOffsetPos(userpos, p_hdg, p_pch, p_rll);
+        setOffsetPos(globals->get_aircraft_position(), p_hdg, p_pch, p_rll);
         parent_hdg = manager->get_user_heading();
         parent_spd = manager->get_user_speed();
     }