From d70e26d87b5294315ca6d67b8c11fabe51abbc4b Mon Sep 17 00:00:00 2001 From: James Turner Date: Sat, 16 Mar 2013 14:47:41 +0000 Subject: [PATCH] Don't duplicate userpos in in AIObjects. --- src/AIModel/AIBallistic.cxx | 22 +++-- src/AIModel/AIBallistic.hxx | 1 - src/AIModel/AIBase.cxx | 156 +++++++++++++----------------------- src/AIModel/AIBase.hxx | 4 - src/AIModel/AIStorm.cxx | 16 ++-- src/AIModel/AIThermal.cxx | 22 ++--- src/AIModel/AIWingman.cxx | 8 +- 7 files changed, 78 insertions(+), 151 deletions(-) diff --git a/src/AIModel/AIBallistic.cxx b/src/AIModel/AIBallistic.cxx index fc4ea30c1..962795e88 100644 --- a/src/AIModel/AIBallistic.cxx +++ b/src/AIModel/AIBallistic.cxx @@ -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::min()) { diff --git a/src/AIModel/AIBallistic.hxx b/src/AIModel/AIBallistic.hxx index 9b28efdae..fbbeeb4d7 100644 --- a/src/AIModel/AIBallistic.hxx +++ b/src/AIModel/AIBallistic.hxx @@ -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; diff --git a/src/AIModel/AIBase.cxx b/src/AIModel/AIBase.cxx index 999659c64..01c0c2691 100644 --- a/src/AIModel/AIBase.cxx +++ b/src/AIModel/AIBase.cxx @@ -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; } diff --git a/src/AIModel/AIBase.hxx b/src/AIModel/AIBase.hxx index 98f889aa5..70e7c9718 100644 --- a/src/AIModel/AIBase.hxx +++ b/src/AIModel/AIBase.hxx @@ -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); diff --git a/src/AIModel/AIStorm.cxx b/src/AIModel/AIStorm.cxx index a4241df46..e11411777 100644 --- a/src/AIModel/AIStorm.cxx +++ b/src/AIModel/AIStorm.cxx @@ -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); diff --git a/src/AIModel/AIThermal.cxx b/src/AIModel/AIThermal.cxx index 4b1d653f8..7007bb249 100644 --- a/src/AIModel/AIThermal.cxx +++ b/src/AIModel/AIThermal.cxx @@ -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 diff --git a/src/AIModel/AIWingman.cxx b/src/AIModel/AIWingman.cxx index 10ffd89cc..7c1478997 100644 --- a/src/AIModel/AIWingman.cxx +++ b/src/AIModel/AIWingman.cxx @@ -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(); } -- 2.39.5