CCNavMeshUtils.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /****************************************************************************
  2. Copyright (c) 2015-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #ifndef __CCNAV_MESH_TOOL_H__
  21. #define __CCNAV_MESH_TOOL_H__
  22. #include "base/ccConfig.h"
  23. #if CC_USE_NAVMESH
  24. #include "platform/CCPlatformMacros.h"
  25. #include "math/CCMath.h"
  26. #include "recast/Detour/DetourCommon.h"
  27. #include "recast/Detour/DetourNavMesh.h"
  28. #include "recast/Detour/DetourNavMeshQuery.h"
  29. #include "recast/DetourTileCache/DetourTileCache.h"
  30. #include "recast/DetourTileCache/DetourTileCacheBuilder.h"
  31. NS_CC_BEGIN
  32. /**
  33. * @addtogroup 3d
  34. * @{
  35. */
  36. struct LinearAllocator : public dtTileCacheAlloc
  37. {
  38. unsigned char* buffer;
  39. int capacity;
  40. int top;
  41. int high;
  42. LinearAllocator(const int cap);
  43. ~LinearAllocator();
  44. void resize(const int cap);
  45. virtual void reset();
  46. virtual void* alloc(const int size);
  47. virtual void free(void* /*ptr*/);
  48. };
  49. struct FastLZCompressor : public dtTileCacheCompressor
  50. {
  51. virtual int maxCompressedSize(const int bufferSize);
  52. virtual dtStatus compress(const unsigned char* buffer, const int bufferSize,
  53. unsigned char* compressed, const int /*maxCompressedSize*/, int* compressedSize);
  54. virtual dtStatus decompress(const unsigned char* compressed, const int compressedSize,
  55. unsigned char* buffer, const int maxBufferSize, int* bufferSize);
  56. };
  57. struct GeomData
  58. {
  59. static const int MAX_OFFMESH_CONNECTIONS = 256;
  60. float offMeshConVerts[MAX_OFFMESH_CONNECTIONS * 3 * 2];
  61. float offMeshConRads[MAX_OFFMESH_CONNECTIONS];
  62. unsigned char offMeshConDirs[MAX_OFFMESH_CONNECTIONS];
  63. unsigned char offMeshConAreas[MAX_OFFMESH_CONNECTIONS];
  64. unsigned short offMeshConFlags[MAX_OFFMESH_CONNECTIONS];
  65. unsigned int offMeshConId[MAX_OFFMESH_CONNECTIONS];
  66. int offMeshConCount;
  67. };
  68. struct MeshProcess : public dtTileCacheMeshProcess
  69. {
  70. const GeomData *data;
  71. MeshProcess(const GeomData *geom);
  72. virtual ~MeshProcess();
  73. //void init(InputGeom* geom)
  74. //{
  75. // m_geom = geom;
  76. //}
  77. virtual void process(struct dtNavMeshCreateParams* params,
  78. unsigned char* polyAreas, unsigned short* polyFlags) override;
  79. };
  80. bool inRange(const float* v1, const float* v2, const float r, const float h);
  81. int fixupCorridor(dtPolyRef* path, const int npath, const int maxPath,
  82. const dtPolyRef* visited, const int nvisited);
  83. // This function checks if the path has a small U-turn, that is,
  84. // a polygon further in the path is adjacent to the first polygon
  85. // in the path. If that happens, a shortcut is taken.
  86. // This can happen if the target (T) location is at tile boundary,
  87. // and we're (S) approaching it parallel to the tile edge.
  88. // The choice at the vertex can be arbitrary,
  89. // +---+---+
  90. // |:::|:::|
  91. // +-S-+-T-+
  92. // |:::| | <-- the step can end up in here, resulting U-turn path.
  93. // +---+---+
  94. int fixupShortcuts(dtPolyRef* path, int npath, dtNavMeshQuery* navQuery);
  95. bool getSteerTarget(dtNavMeshQuery* navQuery, const float* startPos, const float* endPos,
  96. const float minTargetDist,
  97. const dtPolyRef* path, const int pathSize,
  98. float* steerPos, unsigned char& steerPosFlag, dtPolyRef& steerPosRef,
  99. float* outPoints = nullptr, int* outPointCount = nullptr);
  100. /** @} */
  101. NS_CC_END
  102. #endif //CC_USE_NAVMESH
  103. #endif // __CCNAV_MESH_H__