From 7d924b1eee0daf92ecea88c78b11cf1c7ab0c7e9 Mon Sep 17 00:00:00 2001 From: Jakob Botsch Nielsen Date: Wed, 16 Dec 2015 17:53:34 +0100 Subject: [PATCH] Use intermediate variable for nearest point in findNearestPoly When the same point is used as center and as nearestPt, the function would override center and give wrong results. Fix #118 --- Detour/Source/DetourNavMeshQuery.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Detour/Source/DetourNavMeshQuery.cpp b/Detour/Source/DetourNavMeshQuery.cpp index 7b5cccc..2361705 100644 --- a/Detour/Source/DetourNavMeshQuery.cpp +++ b/Detour/Source/DetourNavMeshQuery.cpp @@ -721,8 +721,13 @@ dtStatus dtNavMeshQuery::findNearestPoly(const float* center, const float* exten if (dtStatusFailed(queryPolygons(center, extents, filter, polys, &polyCount, MAX_SEARCH))) return DT_FAILURE | DT_INVALID_PARAM; + if (polyCount == 0) + return DT_SUCCESS; + // Find nearest polygon amongst the nearby polygons. dtPolyRef nearest = 0; + float nearestPoint[3]; + float nearestDistanceSqr = FLT_MAX; for (int i = 0; i < polyCount; ++i) { @@ -751,15 +756,17 @@ dtStatus dtNavMeshQuery::findNearestPoly(const float* center, const float* exten if (d < nearestDistanceSqr) { - if (nearestPt) - dtVcopy(nearestPt, closestPtPoly); + dtVcopy(nearestPoint, closestPtPoly); + nearestDistanceSqr = d; nearest = ref; } } - if (nearestRef) - *nearestRef = nearest; + *nearestRef = nearest; + + if (nearestPt) + dtVcopy(nearestPt, nearestPoint); return DT_SUCCESS; }