diff --git a/src/AIModel/AIAircraft.cxx b/src/AIModel/AIAircraft.cxx index 3a7f74db3..e4c5de02e 100644 --- a/src/AIModel/AIAircraft.cxx +++ b/src/AIModel/AIAircraft.cxx @@ -237,77 +237,93 @@ void FGAIAircraft::Run(double dt) { //###########################// // do calculations for radar // //###########################// + double radar_range_ft2 = fgGetDouble("/instrumentation/radar/range"); + radar_range_ft2 *= SG_NM_TO_METER * SG_METER_TO_FEET; + radar_range_ft2 *= radar_range_ft2; - // copy values from the AIManager double user_latitude = manager->get_user_latitude(); double user_longitude = manager->get_user_longitude(); - 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 lat_range = fabs(pos.lat() - user_latitude) * ft_per_deg_lat; double lon_range = fabs(pos.lon() - user_longitude) * ft_per_deg_lon; - double range_ft = sqrt( lat_range*lat_range + lon_range*lon_range ); - range = range_ft / 6076.11549; + double range_ft2 = lat_range*lat_range + lon_range*lon_range; - // calculate bearing to target - if (pos.lat() >= user_latitude) { - bearing = atan2(lat_range, lon_range) * SG_RADIANS_TO_DEGREES; + // + // Test whether the target is within radar range. + // + in_range = (range_ft2 <= radar_range_ft2); + if ( in_range ) + { + 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.lat() >= user_latitude) { + bearing = atan2(lat_range, lon_range) * SG_RADIANS_TO_DEGREES; if (pos.lon() >= user_longitude) { bearing = 90.0 - bearing; } else { bearing = 270.0 + bearing; } - } else { - bearing = atan2(lon_range, lat_range) * SG_RADIANS_TO_DEGREES; + } else { + bearing = atan2(lon_range, lat_range) * SG_RADIANS_TO_DEGREES; if (pos.lon() >= user_longitude) { bearing = 180.0 - bearing; } else { bearing = 180.0 + bearing; } - } + } - // 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 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 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; + // 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 )); + // 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; + // now correct look left/right for yaw + horiz_offset += user_yaw; - // 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; + // 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; + + } else + props->setBoolValue("radar/in-range", false); //************************************// // Tanker code // //************************************// if ( isTanker) { - if ( (range_ft < 250.0) && + if ( (range_ft2 < 250.0 * 250.0) && (y_shift > 0.0) && (elevation > 0.0) ) { refuel_node->setBoolValue(true); diff --git a/src/AIModel/AIBase.cxx b/src/AIModel/AIBase.cxx index f27067ad6..143cdd9dd 100644 --- a/src/AIModel/AIBase.cxx +++ b/src/AIModel/AIBase.cxx @@ -48,6 +48,7 @@ FGAIBase::FGAIBase() { tgt_roll = roll = tgt_pitch = tgt_yaw = tgt_vs = vs = pitch = 0.0; bearing = elevation = range = rdot = 0.0; x_shift = y_shift = rotation = 0.0; + in_range = false; invisible = true; no_roll = true; model_path = ""; @@ -139,6 +140,7 @@ void FGAIBase::bind() { props->tie("orientation/roll-deg", SGRawValuePointer(&roll)); props->tie("orientation/true-heading-deg", SGRawValuePointer(&hdg)); + props->tie("radar/in-range", SGRawValuePointer(&in_range)); props->tie("radar/bearing-deg", SGRawValuePointer(&bearing)); props->tie("radar/elevation-deg", SGRawValuePointer(&elevation)); props->tie("radar/range-nm", SGRawValuePointer(&range)); @@ -167,6 +169,7 @@ void FGAIBase::unbind() { props->untie("orientation/roll-deg"); props->untie("orientation/true-heading-deg"); + props->untie("radar/in-range"); props->untie("radar/bearing-deg"); props->untie("radar/elevation-deg"); props->untie("radar/range-nm"); diff --git a/src/AIModel/AIBase.hxx b/src/AIModel/AIBase.hxx index b98cb5d26..f178f2dbf 100644 --- a/src/AIModel/AIBase.hxx +++ b/src/AIModel/AIBase.hxx @@ -86,6 +86,7 @@ protected: double tgt_vs; // these describe radar information for the user + bool in_range; // true if in range of the radar, otherwise false double bearing; // true bearing from user to this model double elevation; // elevation in degrees from user to this model double range; // range from user to this model, nm