CCNavMesh.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_H__
  21. #define __CCNAV_MESH_H__
  22. #include "base/ccConfig.h"
  23. #if CC_USE_NAVMESH
  24. #include "base/CCRef.h"
  25. #include "math/Vec3.h"
  26. #include "recast/Detour/DetourNavMesh.h"
  27. #include "recast/Detour/DetourNavMeshQuery.h"
  28. #include "recast/DetourCrowd/DetourCrowd.h"
  29. #include "recast/DetourTileCache/DetourTileCache.h"
  30. #include <string>
  31. #include <vector>
  32. #include "navmesh/CCNavMeshAgent.h"
  33. #include "navmesh/CCNavMeshDebugDraw.h"
  34. #include "navmesh/CCNavMeshObstacle.h"
  35. #include "navmesh/CCNavMeshUtils.h"
  36. NS_CC_BEGIN
  37. /**
  38. * @addtogroup 3d
  39. * @{
  40. */
  41. class Renderer;
  42. /** @brief NavMesh: The NavMesh information container, include mesh, tileCache, and so on. */
  43. class CC_DLL NavMesh : public Ref
  44. {
  45. public:
  46. /**
  47. Create navmesh
  48. @param navFilePath The NavMesh File path.
  49. @param geomFilePath The geometry File Path,include offmesh information,etc.
  50. */
  51. static NavMesh* create(const std::string &navFilePath, const std::string &geomFilePath);
  52. /** update navmesh. */
  53. void update(float dt);
  54. /** Internal method, the updater of debug drawing, need called each frame. */
  55. void debugDraw(Renderer* renderer);
  56. /** Enable debug draw or disable. */
  57. void setDebugDrawEnable(bool enable);
  58. /** Check enabled debug draw. */
  59. bool isDebugDrawEnabled() const;
  60. /** add a agent to navmesh. */
  61. void addNavMeshAgent(NavMeshAgent *agent);
  62. /** remove a agent from navmesh. */
  63. void removeNavMeshAgent(NavMeshAgent *agent);
  64. /** add a obstacle to navmesh. */
  65. void addNavMeshObstacle(NavMeshObstacle *obstacle);
  66. /** remove a obstacle from navmesh. */
  67. void removeNavMeshObstacle(NavMeshObstacle *obstacle);
  68. /**
  69. find a path on navmesh
  70. @param start The start search position in world coordinate system.
  71. @param end The end search position in world coordinate system.
  72. @param pathPoints the key points of path.
  73. */
  74. void findPath(const Vec3 &start, const Vec3 &end, std::vector<Vec3> &pathPoints);
  75. CC_CONSTRUCTOR_ACCESS:
  76. NavMesh();
  77. virtual ~NavMesh();
  78. protected:
  79. bool initWithFilePath(const std::string &navFilePath, const std::string &geomFilePath);
  80. bool read();
  81. bool loadNavMeshFile();
  82. bool loadGeomFile();
  83. void dtDraw();
  84. void drawAgents();
  85. void drawObstacles();
  86. void drawOffMeshConnections();
  87. protected:
  88. dtNavMesh *_navMesh;
  89. dtNavMeshQuery *_navMeshQuery;
  90. dtCrowd *_crowed;
  91. dtTileCache *_tileCache;
  92. LinearAllocator *_allocator;
  93. FastLZCompressor *_compressor;
  94. MeshProcess *_meshProcess;
  95. GeomData *_geomData;
  96. std::vector<NavMeshAgent*> _agentList;
  97. std::vector<NavMeshObstacle*> _obstacleList;
  98. NavMeshDebugDraw _debugDraw;
  99. std::string _navFilePath;
  100. std::string _geomFilePath;
  101. bool _isDebugDrawEnabled;
  102. };
  103. /** @} */
  104. NS_CC_END
  105. #endif //CC_USE_NAVMESH
  106. #endif // __CCNAV_MESH_H__