diff --git a/components/detournavigator/settingsutils.hpp b/components/detournavigator/settingsutils.hpp index 285920e5a0..a4c68e3cd5 100644 --- a/components/detournavigator/settingsutils.hpp +++ b/components/detournavigator/settingsutils.hpp @@ -50,12 +50,24 @@ namespace DetourNavigator return static_cast(settings.mTileSize) * settings.mCellSize; } + inline int getTilePosition(const RecastSettings& settings, float position) + { + const float v = std::floor(position / getTileSize(settings)); + if (v < static_cast(std::numeric_limits::min())) + return std::numeric_limits::min(); + if (v > static_cast(std::numeric_limits::max() - 1)) + return std::numeric_limits::max() - 1; + return static_cast(v); + } + + inline TilePosition getTilePosition(const RecastSettings& settings, const osg::Vec2f& position) + { + return TilePosition(getTilePosition(settings, position.x()), getTilePosition(settings, position.y())); + } + inline TilePosition getTilePosition(const RecastSettings& settings, const osg::Vec3f& position) { - return TilePosition( - static_cast(std::floor(position.x() / getTileSize(settings))), - static_cast(std::floor(position.z() / getTileSize(settings))) - ); + return getTilePosition(settings, osg::Vec2f(position.x(), position.z())); } inline TileBounds makeTileBounds(const RecastSettings& settings, const TilePosition& tilePosition)