From 990b24517970bbf9f928a526f5c23b7c5ffea08e Mon Sep 17 00:00:00 2001 From: Mikko Mononen Date: Tue, 17 Sep 2013 21:29:51 +0200 Subject: [PATCH] Fixed google code Issue 228 - more defencive use of finNearestPoly() in dtCrowd --- DetourCrowd/Source/DetourCrowd.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/DetourCrowd/Source/DetourCrowd.cpp b/DetourCrowd/Source/DetourCrowd.cpp index 5f86554..21cf871 100644 --- a/DetourCrowd/Source/DetourCrowd.cpp +++ b/DetourCrowd/Source/DetourCrowd.cpp @@ -516,8 +516,14 @@ int dtCrowd::addAgent(const float* pos, const dtCrowdAgentParams* params) // Find nearest position on navmesh and place the agent there. float nearest[3]; - dtPolyRef ref; - m_navquery->findNearestPoly(pos, m_ext, &m_filter, &ref, nearest); + dtPolyRef ref = 0; + dtVcopy(nearest, pos); + dtStatus status = m_navquery->findNearestPoly(pos, m_ext, &m_filter, &ref, nearest); + if (dtStatusFailed(status)) + { + dtVcopy(nearest, pos); + ref = 0; + } ag->corridor.reset(ref, nearest); ag->boundary.reset(); @@ -941,8 +947,9 @@ void dtCrowd::checkPathValidity(dtCrowdAgent** agents, const int nagents, const // Current location is not valid, try to reposition. // TODO: this can snap agents, how to handle that? float nearest[3]; + dtVcopy(nearest, agentPos); agentRef = 0; - m_navquery->findNearestPoly(ag->npos, m_ext, &m_filter, &agentRef, nearest); + dtStatus status = m_navquery->findNearestPoly(ag->npos, m_ext, &m_filter, &agentRef, nearest); dtVcopy(agentPos, nearest); if (!agentRef) @@ -971,6 +978,8 @@ void dtCrowd::checkPathValidity(dtCrowdAgent** agents, const int nagents, const { // Current target is not valid, try to reposition. float nearest[3]; + dtVcopy(nearest, ag->targetPos); + ag->targetRef = 0; m_navquery->findNearestPoly(ag->targetPos, m_ext, &m_filter, &ag->targetRef, nearest); dtVcopy(ag->targetPos, nearest); replan = true;