From a8d1cba68c92dd7f1083f64bc75c8675d40874ab Mon Sep 17 00:00:00 2001 From: Mikko Mononen Date: Tue, 17 Sep 2013 21:07:41 +0200 Subject: [PATCH] Fixed google code Issue 237 - check query status of moveAlongSurface() in path corridor --- DetourCrowd/Include/DetourPathCorridor.h | 6 ++- DetourCrowd/Source/DetourPathCorridor.cpp | 53 +++++++++++++---------- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/DetourCrowd/Include/DetourPathCorridor.h b/DetourCrowd/Include/DetourPathCorridor.h index 9544ea5..863447a 100644 --- a/DetourCrowd/Include/DetourPathCorridor.h +++ b/DetourCrowd/Include/DetourPathCorridor.h @@ -92,14 +92,16 @@ public: /// @param[in] npos The desired new position. [(x, y, z)] /// @param[in] navquery The query object used to build the corridor. /// @param[in] filter The filter to apply to the operation. - void movePosition(const float* npos, dtNavMeshQuery* navquery, const dtQueryFilter* filter); + /// @return Returns true if move succeeded. + bool movePosition(const float* npos, dtNavMeshQuery* navquery, const dtQueryFilter* filter); /// Moves the target from the curent location to the desired location, adjusting the corridor /// as needed to reflect the change. /// @param[in] npos The desired new target position. [(x, y, z)] /// @param[in] navquery The query object used to build the corridor. /// @param[in] filter The filter to apply to the operation. - void moveTargetPosition(const float* npos, dtNavMeshQuery* navquery, const dtQueryFilter* filter); + /// @return Returns true if move succeeded. + bool moveTargetPosition(const float* npos, dtNavMeshQuery* navquery, const dtQueryFilter* filter); /// Loads a new path and target into the corridor. /// @param[in] target The target location within the last polygon of the path. [(x, y, z)] diff --git a/DetourCrowd/Source/DetourPathCorridor.cpp b/DetourCrowd/Source/DetourPathCorridor.cpp index a1bfe0d..54a2ab8 100644 --- a/DetourCrowd/Source/DetourPathCorridor.cpp +++ b/DetourCrowd/Source/DetourPathCorridor.cpp @@ -436,7 +436,7 @@ depends on local polygon density, query search extents, etc. The resulting position will differ from the desired position if the desired position is not on the navigation mesh, or it can't be reached using a local search. */ -void dtPathCorridor::movePosition(const float* npos, dtNavMeshQuery* navquery, const dtQueryFilter* filter) +bool dtPathCorridor::movePosition(const float* npos, dtNavMeshQuery* navquery, const dtQueryFilter* filter) { dtAssert(m_path); dtAssert(m_npath); @@ -446,15 +446,19 @@ void dtPathCorridor::movePosition(const float* npos, dtNavMeshQuery* navquery, c static const int MAX_VISITED = 16; dtPolyRef visited[MAX_VISITED]; int nvisited = 0; - navquery->moveAlongSurface(m_path[0], m_pos, npos, filter, - result, visited, &nvisited, MAX_VISITED); - m_npath = dtMergeCorridorStartMoved(m_path, m_npath, m_maxPath, visited, nvisited); - - // Adjust the position to stay on top of the navmesh. - float h = m_pos[1]; - navquery->getPolyHeight(m_path[0], result, &h); - result[1] = h; - dtVcopy(m_pos, result); + dtStatus status = navquery->moveAlongSurface(m_path[0], m_pos, npos, filter, + result, visited, &nvisited, MAX_VISITED); + if (dtStatusSucceed(status)) { + m_npath = dtMergeCorridorStartMoved(m_path, m_npath, m_maxPath, visited, nvisited); + + // Adjust the position to stay on top of the navmesh. + float h = m_pos[1]; + navquery->getPolyHeight(m_path[0], result, &h); + result[1] = h; + dtVcopy(m_pos, result); + return true; + } + return false; } /** @@ -470,7 +474,7 @@ The expected use case is that the desired target will be 'near' the current corr The resulting target will differ from the desired target if the desired target is not on the navigation mesh, or it can't be reached using a local search. */ -void dtPathCorridor::moveTargetPosition(const float* npos, dtNavMeshQuery* navquery, const dtQueryFilter* filter) +bool dtPathCorridor::moveTargetPosition(const float* npos, dtNavMeshQuery* navquery, const dtQueryFilter* filter) { dtAssert(m_path); dtAssert(m_npath); @@ -480,17 +484,22 @@ void dtPathCorridor::moveTargetPosition(const float* npos, dtNavMeshQuery* navqu static const int MAX_VISITED = 16; dtPolyRef visited[MAX_VISITED]; int nvisited = 0; - navquery->moveAlongSurface(m_path[m_npath-1], m_target, npos, filter, - result, visited, &nvisited, MAX_VISITED); - m_npath = dtMergeCorridorEndMoved(m_path, m_npath, m_maxPath, visited, nvisited); - - // TODO: should we do that? - // Adjust the position to stay on top of the navmesh. - /* float h = m_target[1]; - navquery->getPolyHeight(m_path[m_npath-1], result, &h); - result[1] = h;*/ - - dtVcopy(m_target, result); + dtStatus status = navquery->moveAlongSurface(m_path[m_npath-1], m_target, npos, filter, + result, visited, &nvisited, MAX_VISITED); + if (dtStatusSucceed(status)) + { + m_npath = dtMergeCorridorEndMoved(m_path, m_npath, m_maxPath, visited, nvisited); + // TODO: should we do that? + // Adjust the position to stay on top of the navmesh. + /* float h = m_target[1]; + navquery->getPolyHeight(m_path[m_npath-1], result, &h); + result[1] = h;*/ + + dtVcopy(m_target, result); + + return true; + } + return false; } /// @par