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
This commit is contained in:
Jakob Botsch Nielsen 2015-12-16 17:53:34 +01:00
parent 7b4d18f459
commit 7d924b1eee

View File

@ -721,8 +721,13 @@ dtStatus dtNavMeshQuery::findNearestPoly(const float* center, const float* exten
if (dtStatusFailed(queryPolygons(center, extents, filter, polys, &polyCount, MAX_SEARCH))) if (dtStatusFailed(queryPolygons(center, extents, filter, polys, &polyCount, MAX_SEARCH)))
return DT_FAILURE | DT_INVALID_PARAM; return DT_FAILURE | DT_INVALID_PARAM;
if (polyCount == 0)
return DT_SUCCESS;
// Find nearest polygon amongst the nearby polygons. // Find nearest polygon amongst the nearby polygons.
dtPolyRef nearest = 0; dtPolyRef nearest = 0;
float nearestPoint[3];
float nearestDistanceSqr = FLT_MAX; float nearestDistanceSqr = FLT_MAX;
for (int i = 0; i < polyCount; ++i) for (int i = 0; i < polyCount; ++i)
{ {
@ -751,15 +756,17 @@ dtStatus dtNavMeshQuery::findNearestPoly(const float* center, const float* exten
if (d < nearestDistanceSqr) if (d < nearestDistanceSqr)
{ {
if (nearestPt) dtVcopy(nearestPoint, closestPtPoly);
dtVcopy(nearestPt, closestPtPoly);
nearestDistanceSqr = d; nearestDistanceSqr = d;
nearest = ref; nearest = ref;
} }
} }
if (nearestRef) *nearestRef = nearest;
*nearestRef = nearest;
if (nearestPt)
dtVcopy(nearestPt, nearestPoint);
return DT_SUCCESS; return DT_SUCCESS;
} }