From 16a54a4409a130b42f02a5f70e18aec5ec6be1c3 Mon Sep 17 00:00:00 2001 From: James Turner Date: Wed, 26 Oct 2011 17:26:11 +0100 Subject: [PATCH] Change a hot-spot in the AI code, to use cartesian instead of geodetic math. --- src/AIModel/AIAircraft.cxx | 11 ++++++----- src/Main/globals.cxx | 5 +++++ src/Main/globals.hxx | 2 ++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/AIModel/AIAircraft.cxx b/src/AIModel/AIAircraft.cxx index a87a94fe6..2722886c6 100644 --- a/src/AIModel/AIAircraft.cxx +++ b/src/AIModel/AIAircraft.cxx @@ -838,11 +838,12 @@ bool FGAIAircraft::leadPointReached(FGAIWaypoint* curr) { } -bool FGAIAircraft::aiTrafficVisible() { - SGGeod userPos(SGGeod::fromDeg(fgGetDouble("/position/longitude-deg"), - fgGetDouble("/position/latitude-deg"))); - - return (SGGeodesy::distanceNm(userPos, pos) <= TRAFFICTOAIDISTTODIE); +bool FGAIAircraft::aiTrafficVisible() +{ + SGVec3d cartPos = SGVec3d::fromGeod(pos); + const double d2 = (TRAFFICTOAIDISTTODIE * SG_NM_TO_METER) * + (TRAFFICTOAIDISTTODIE * SG_NM_TO_METER); + return (distSqr(cartPos, globals->get_aircraft_positon_cart()) < d2); } diff --git a/src/Main/globals.cxx b/src/Main/globals.cxx index 4d6d7bf57..c4bb125a7 100644 --- a/src/Main/globals.cxx +++ b/src/Main/globals.cxx @@ -380,6 +380,11 @@ FGGlobals::get_aircraft_position() const throw sg_exception("Can't get aircraft position", "FGGlobals::get_aircraft_position()" ); } +const SGVec3d& +FGGlobals::get_aircraft_positon_cart() const +{ + return SGVec3d::fromGeod(get_aircraft_position()); +} // Save the current state as the initial state. diff --git a/src/Main/globals.hxx b/src/Main/globals.hxx index 54b777a71..365ce3988 100644 --- a/src/Main/globals.hxx +++ b/src/Main/globals.hxx @@ -267,6 +267,8 @@ public: const SGGeod & get_aircraft_position() const; + const SGVec3d& get_aircraft_positon_cart() const; + inline FGModelMgr *get_model_mgr () { return model_mgr; } inline void set_model_mgr (FGModelMgr * mgr)