AI: cache radar properties as nodes
When many AI objects, the cost of looking up the radar properties for each object, each frame, is significant.
This commit is contained in:
parent
ec488bf204
commit
ef98b667b4
3 changed files with 49 additions and 24 deletions
|
@ -672,30 +672,24 @@ void FGAIBase::removeSoundFx() {
|
|||
|
||||
double FGAIBase::UpdateRadar(FGAIManager* manager)
|
||||
{
|
||||
bool control = fgGetBool("/sim/controls/radar", true);
|
||||
if (!manager->isRadarEnabled())
|
||||
return 0.0;
|
||||
|
||||
if(!control) return 0;
|
||||
|
||||
double radar_range_m = fgGetDouble("/instrumentation/radar/range");
|
||||
bool force_on = fgGetBool("/instrumentation/radar/debug-mode", false);
|
||||
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;
|
||||
const double radar_range_m = manager->radarRangeM() * 1.1; // + 10%
|
||||
bool force_on = manager->enableRadarDebug();
|
||||
double d = dist(SGVec3d::fromGeod(pos), globals->get_aircraft_position_cart());
|
||||
double dFt = d * SG_METER_TO_FEET;
|
||||
in_range = (d < radar_range_m);
|
||||
|
||||
if (!force_on && in_range) {
|
||||
return dFt * dFt;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
range = d * SG_METER_TO_NM;
|
||||
// calculate bearing to target
|
||||
bearing = SGGeodesy::courseDeg(globals->get_aircraft_position(), pos);
|
||||
|
||||
|
@ -705,7 +699,7 @@ double FGAIBase::UpdateRadar(FGAIManager* manager)
|
|||
|
||||
// calculate elevation to target
|
||||
ht_diff = altitude_ft - globals->get_aircraft_position().getElevationFt();
|
||||
elevation = atan2( ht_diff, range_ft ) * SG_RADIANS_TO_DEGREES;
|
||||
elevation = atan2( ht_diff, dFt ) * SG_RADIANS_TO_DEGREES;
|
||||
|
||||
// calculate look up/down to target
|
||||
vert_offset = elevation - user_pitch;
|
||||
|
@ -731,7 +725,7 @@ double FGAIBase::UpdateRadar(FGAIManager* manager)
|
|||
rotation = hdg - user_heading;
|
||||
SG_NORMALIZE_RANGE(rotation, 0.0, 360.0);
|
||||
|
||||
return range_ft * range_ft;
|
||||
return dFt * dFt;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -133,7 +133,8 @@ FGAIManager::init() {
|
|||
globals->get_commands()->addCommand("load-scenario", this, &FGAIManager::loadScenarioCommand);
|
||||
globals->get_commands()->addCommand("unload-scenario", this, &FGAIManager::unloadScenarioCommand);
|
||||
_environmentVisiblity = fgGetNode("/environment/visibility-m");
|
||||
|
||||
_groundSpeedKts_node = fgGetNode("/velocities/groundspeed-kt", true);
|
||||
|
||||
// Create an (invisible) AIAircraft representation of the current
|
||||
// users's aircraft, that mimicks the user aircraft's behavior.
|
||||
|
||||
|
@ -142,7 +143,16 @@ FGAIManager::init() {
|
|||
_userAircraft->setGeodPos(globals->get_aircraft_position());
|
||||
_userAircraft->setPerformance("", "jet_transport");
|
||||
_userAircraft->setHeading(fgGetDouble("/orientation/heading-deg"));
|
||||
_userAircraft->setSpeed(fgGetDouble("/velocities/groundspeed-kt"));
|
||||
_userAircraft->setSpeed(_groundSpeedKts_node->getDoubleValue());
|
||||
|
||||
// radar properties
|
||||
_simRadarControl = fgGetNode("/sim/controls/radar", true);
|
||||
if (!_simRadarControl->hasValue()) {
|
||||
// default to true, but only if not already set
|
||||
_simRadarControl->setBoolValue(true);
|
||||
}
|
||||
_radarRangeNode = fgGetNode("/instrumentation/radar/range", true);
|
||||
_radarDebugNode = fgGetNode("/instrumentation/radar/debug-mode", true);
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -241,7 +251,12 @@ FGAIManager::update(double dt)
|
|||
return;
|
||||
|
||||
fetchUserState(dt);
|
||||
|
||||
|
||||
// fetch radar state. Ensure we only do this once per frame.
|
||||
_radarEnabled = _simRadarControl->getBoolValue();
|
||||
_radarDebugMode = _radarDebugNode->getBoolValue();
|
||||
_radarRangeM = _radarRangeNode->getDoubleValue() * SG_NM_TO_METER;
|
||||
|
||||
// partition the list into dead followed by alive
|
||||
auto firstAlive =
|
||||
std::stable_partition(ai_list.begin(), ai_list.end(), std::mem_fn(&FGAIBase::getDie));
|
||||
|
@ -348,7 +363,7 @@ FGAIManager::fetchUserState( double dt )
|
|||
|
||||
_userAircraft->setGeodPos(globals->get_aircraft_position());
|
||||
_userAircraft->setHeading(user_heading);
|
||||
_userAircraft->setSpeed(fgGetDouble("/velocities/groundspeed-kt"));
|
||||
_userAircraft->setSpeed(_groundSpeedKts_node->getDoubleValue());
|
||||
_userAircraft->update(dt);
|
||||
}
|
||||
|
||||
|
|
|
@ -92,6 +92,15 @@ public:
|
|||
* avoid correctly.
|
||||
*/
|
||||
FGAIAircraft* getUserAircraft() const;
|
||||
|
||||
bool isRadarEnabled() const
|
||||
{ return _radarEnabled; }
|
||||
|
||||
bool enableRadarDebug() const
|
||||
{ return _radarDebugMode; }
|
||||
|
||||
double radarRangeM() const
|
||||
{ return _radarRangeM; }
|
||||
private:
|
||||
// FGSubmodelMgr is a friend for access to the AI_list
|
||||
friend class FGSubmodelMgr;
|
||||
|
@ -120,7 +129,8 @@ private:
|
|||
SGPropertyNode_ptr wind_from_east_node;
|
||||
SGPropertyNode_ptr wind_from_north_node;
|
||||
SGPropertyNode_ptr _environmentVisiblity;
|
||||
|
||||
SGPropertyNode_ptr _groundSpeedKts_node;
|
||||
|
||||
ai_list_type ai_list;
|
||||
|
||||
double user_altitude_agl;
|
||||
|
@ -146,6 +156,12 @@ private:
|
|||
ScenarioDict _scenarios;
|
||||
|
||||
SGSharedPtr<FGAIAircraft> _userAircraft;
|
||||
|
||||
SGPropertyNode_ptr _simRadarControl,
|
||||
_radarRangeNode, _radarDebugNode;
|
||||
bool _radarEnabled = true,
|
||||
_radarDebugMode = false;
|
||||
double _radarRangeM = 0.0;
|
||||
};
|
||||
|
||||
#endif // _FG_AIMANAGER_HXX
|
||||
|
|
Loading…
Add table
Reference in a new issue