Skip to content

Commit

Permalink
Validate constraint: #polygons per tile (recastnavigation#449)
Browse files Browse the repository at this point in the history
* Validate constraint: #polygons per tile

When adding a tile to a NavMesh, ensure that the number of polygons in that tile fit in the poly ID address space.

* Improve dtNavMeshParams field documentation
  • Loading branch information
Bromeon authored Nov 19, 2020
1 parent 6624e7a commit 9dc88fc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Detour/Include/DetourNavMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ struct dtNavMeshParams
float orig[3]; ///< The world space origin of the navigation mesh's tile space. [(x, y, z)]
float tileWidth; ///< The width of each tile. (Along the x-axis.)
float tileHeight; ///< The height of each tile. (Along the z-axis.)
int maxTiles; ///< The maximum number of tiles the navigation mesh can contain.
int maxPolys; ///< The maximum number of polygons each tile can contain.
int maxTiles; ///< The maximum number of tiles the navigation mesh can contain. This and maxPolys are used to calculate how many bits are needed to identify tiles and polygons uniquely.
int maxPolys; ///< The maximum number of polygons each tile can contain. This and maxTiles are used to calculate how many bits are needed to identify tiles and polygons uniquely.
};

/// A navigation mesh based on tiles of convex polygons.
Expand Down
7 changes: 7 additions & 0 deletions Detour/Source/DetourNavMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,13 @@ dtStatus dtNavMesh::addTile(unsigned char* data, int dataSize, int flags,
return DT_FAILURE | DT_WRONG_MAGIC;
if (header->version != DT_NAVMESH_VERSION)
return DT_FAILURE | DT_WRONG_VERSION;

#ifndef DT_POLYREF64
// Do not allow adding more polygons than specified in the NavMesh's maxPolys constraint.
// Otherwise, the poly ID cannot be represented with the given number of bits.
if (m_polyBits < dtIlog2(dtNextPow2((unsigned int)header->polyCount)))
return DT_FAILURE | DT_INVALID_PARAM;
#endif

// Make sure the location is free.
if (getTileAt(header->x, header->y, header->layer))
Expand Down

0 comments on commit 9dc88fc

Please sign in to comment.