NavigationMesh.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  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. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Container/ArrayPtr.h"
  24. #include "../Math/BoundingBox.h"
  25. #include "../Scene/Component.h"
  26. #include "../Container/HashSet.h"
  27. #include "../Math/Matrix3x4.h"
  28. class dtNavMesh;
  29. class dtNavMeshQuery;
  30. class dtQueryFilter;
  31. struct dtNavMeshCreateParams;
  32. class rcContext;
  33. struct rcHeightfield;
  34. struct rcCompactHeightfield;
  35. struct rcContourSet;
  36. struct rcPolyMesh;
  37. struct rcPolyMeshDetail;
  38. struct rcHeightFieldLayerSet;
  39. namespace Atomic
  40. {
  41. enum NavmeshPartitionType
  42. {
  43. NAVMESH_PARTITION_WATERSHED,
  44. NAVMESH_PARTITION_MONOTONE,
  45. };
  46. class Geometry;
  47. struct FindPathData;
  48. struct NavBuildData;
  49. /// Description of a navigation mesh geometry component, with transform and bounds information.
  50. struct NavigationGeometryInfo
  51. {
  52. /// Component.
  53. Component* component_;
  54. /// Geometry LOD level if applicable.
  55. unsigned lodLevel_;
  56. /// Transform relative to the navigation mesh root node.
  57. Matrix3x4 transform_;
  58. /// Bounding box relative to the navigation mesh root node.
  59. BoundingBox boundingBox_;
  60. };
  61. /// Navigation mesh component. Collects the navigation geometry from child nodes with the Navigable component and responds to path queries.
  62. class ATOMIC_API NavigationMesh : public Component
  63. {
  64. OBJECT(NavigationMesh);
  65. friend class DetourCrowdManager;
  66. public:
  67. /// Construct.
  68. NavigationMesh(Context* context);
  69. /// Destruct.
  70. virtual ~NavigationMesh();
  71. /// Register object factory.
  72. static void RegisterObject(Context* context);
  73. /// Visualize the component as debug geometry.
  74. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  75. /// Set tile size.
  76. void SetTileSize(int size);
  77. /// Set cell size.
  78. void SetCellSize(float size);
  79. /// Set cell height.
  80. void SetCellHeight(float height);
  81. /// Set navigation agent height.
  82. void SetAgentHeight(float height);
  83. /// Set navigation agent radius.
  84. void SetAgentRadius(float radius);
  85. /// Set navigation agent max vertical climb.
  86. void SetAgentMaxClimb(float maxClimb);
  87. /// Set navigation agent max slope.
  88. void SetAgentMaxSlope(float maxSlope);
  89. /// Set region minimum size.
  90. void SetRegionMinSize(float size);
  91. /// Set region merge size.
  92. void SetRegionMergeSize(float size);
  93. /// Set edge max length.
  94. void SetEdgeMaxLength(float length);
  95. /// Set edge max error.
  96. void SetEdgeMaxError(float error);
  97. /// Set detail sampling distance.
  98. void SetDetailSampleDistance(float distance);
  99. /// Set detail sampling maximum error.
  100. void SetDetailSampleMaxError(float error);
  101. /// Set padding of the navigation mesh bounding box. Having enough padding allows to add geometry on the extremities of the navigation mesh when doing partial rebuilds.
  102. void SetPadding(const Vector3& padding);
  103. /// Set the cost of an area.
  104. void SetAreaCost(unsigned areaID, float cost);
  105. /// Rebuild the navigation mesh. Return true if successful.
  106. virtual bool Build();
  107. /// Rebuild part of the navigation mesh contained by the world-space bounding box. Return true if successful.
  108. virtual bool Build(const BoundingBox& boundingBox);
  109. /// Find the nearest point on the navigation mesh to a given point. Extens specifies how far out from the specified point to check along each axis.
  110. Vector3 FindNearestPoint(const Vector3& point, const Vector3& extents=Vector3::ONE);
  111. /// Try to move along the surface from one point to another.
  112. Vector3 MoveAlongSurface(const Vector3& start, const Vector3& end, const Vector3& extents=Vector3::ONE, int maxVisited=3);
  113. /// Find a path between world space points. Return non-empty list of points if successful. Extents specifies how far off the navigation mesh the points can be.
  114. void FindPath(PODVector<Vector3>& dest, const Vector3& start, const Vector3& end, const Vector3& extents = Vector3::ONE);
  115. /// Return a random point on the navigation mesh.
  116. Vector3 GetRandomPoint();
  117. /// Return a random point on the navigation mesh within a circle. The circle radius is only a guideline and in practice the returned point may be further away.
  118. Vector3 GetRandomPointInCircle(const Vector3& center, float radius, const Vector3& extents = Vector3::ONE);
  119. /// Return distance to wall from a point. Maximum search radius must be specified.
  120. float GetDistanceToWall(const Vector3& point, float radius, const Vector3& extents = Vector3::ONE);
  121. /// Perform a walkability raycast on the navigation mesh between start and end and return the point where a wall was hit, or the end point if no walls.
  122. Vector3 Raycast(const Vector3& start, const Vector3& end, const Vector3& extents = Vector3::ONE);
  123. /// Add debug geometry to the debug renderer.
  124. void DrawDebugGeometry(bool depthTest);
  125. /// Return the given name of this navigation mesh.
  126. String GetMeshName() const { return meshName_; }
  127. /// Set the name of this navigation mesh.
  128. void SetMeshName(const String& newName);
  129. /// Return tile size.
  130. int GetTileSize() const { return tileSize_; }
  131. /// Return cell size.
  132. float GetCellSize() const { return cellSize_; }
  133. /// Return cell height.
  134. float GetCellHeight() const { return cellHeight_; }
  135. /// Return navigation agent height.
  136. float GetAgentHeight() const { return agentHeight_; }
  137. /// Return navigation agent radius.
  138. float GetAgentRadius() const { return agentRadius_; }
  139. /// Return navigation agent max vertical climb.
  140. float GetAgentMaxClimb() const { return agentMaxClimb_; }
  141. /// Return navigation agent max slope.
  142. float GetAgentMaxSlope() const { return agentMaxSlope_; }
  143. /// Return region minimum size.
  144. float GetRegionMinSize() const { return regionMinSize_; }
  145. /// Return region merge size.
  146. float GetRegionMergeSize() const { return regionMergeSize_; }
  147. /// Return edge max length.
  148. float GetEdgeMaxLength() const { return edgeMaxLength_; }
  149. /// Return edge max error.
  150. float GetEdgeMaxError() const { return edgeMaxError_; }
  151. /// Return detail sampling distance.
  152. float GetDetailSampleDistance() const { return detailSampleDistance_; }
  153. /// Return detail sampling maximum error.
  154. float GetDetailSampleMaxError() const { return detailSampleMaxError_; }
  155. /// Return navigation mesh bounding box padding.
  156. const Vector3& GetPadding() const { return padding_; }
  157. /// Get the current cost of an area
  158. float GetAreaCost(unsigned areaID) const;
  159. /// Return whether has been initialized with valid navigation data.
  160. bool IsInitialized() const { return navMesh_ != 0; }
  161. /// Return local space bounding box of the navigation mesh.
  162. const BoundingBox& GetBoundingBox() const { return boundingBox_; }
  163. /// Return world space bounding box of the navigation mesh.
  164. BoundingBox GetWorldBoundingBox() const;
  165. /// Return number of tiles.
  166. IntVector2 GetNumTiles() const { return IntVector2(numTilesX_, numTilesZ_); }
  167. /// Set the partition type used for polygon generation.
  168. void SetPartitionType(NavmeshPartitionType aType);
  169. /// Return Partition Type.
  170. NavmeshPartitionType GetPartitionType() const { return partitionType_; }
  171. /// Set navigation data attribute.
  172. virtual void SetNavigationDataAttr(const PODVector<unsigned char>& value);
  173. /// Return navigation data attribute.
  174. virtual PODVector<unsigned char> GetNavigationDataAttr() const;
  175. protected:
  176. /// Collect geometry from under Navigable components.
  177. void CollectGeometries(Vector<NavigationGeometryInfo>& geometryList);
  178. /// Visit nodes and collect navigable geometry.
  179. void CollectGeometries(Vector<NavigationGeometryInfo>& geometryList, Node* node, HashSet<Node*>& processedNodes, bool recursive);
  180. /// Get geometry data within a bounding box.
  181. void GetTileGeometry(NavBuildData* build, Vector<NavigationGeometryInfo>& geometryList, BoundingBox& box);
  182. /// Add a triangle mesh to the geometry data.
  183. void AddTriMeshGeometry(NavBuildData* build, Geometry* geometry, const Matrix3x4& transform);
  184. /// Build one tile of the navigation mesh. Return true if successful.
  185. virtual bool BuildTile(Vector<NavigationGeometryInfo>& geometryList, int x, int z);
  186. /// Ensure that the navigation mesh query is initialized. Return true if successful.
  187. bool InitializeQuery();
  188. /// Release the navigation mesh and the query.
  189. virtual void ReleaseNavigationMesh();
  190. /// Identifying name for this navigation mesh.
  191. String meshName_;
  192. /// Detour navigation mesh.
  193. dtNavMesh* navMesh_;
  194. /// Detour navigation mesh query.
  195. dtNavMeshQuery* navMeshQuery_;
  196. /// Detour navigation mesh query filter.
  197. dtQueryFilter* queryFilter_;
  198. /// Temporary data for finding a path.
  199. FindPathData* pathData_;
  200. /// Tile size.
  201. int tileSize_;
  202. /// Cell size.
  203. float cellSize_;
  204. /// Cell height.
  205. float cellHeight_;
  206. /// Navigation agent height.
  207. float agentHeight_;
  208. /// Navigation agent radius.
  209. float agentRadius_;
  210. /// Navigation agent max vertical climb.
  211. float agentMaxClimb_;
  212. /// Navigation agent max slope.
  213. float agentMaxSlope_;
  214. /// Region minimum size.
  215. float regionMinSize_;
  216. /// Region merge size.
  217. float regionMergeSize_;
  218. /// Edge max length.
  219. float edgeMaxLength_;
  220. /// Edge max error.
  221. float edgeMaxError_;
  222. /// Detail sampling distance.
  223. float detailSampleDistance_;
  224. /// Detail sampling maximum error.
  225. float detailSampleMaxError_;
  226. /// Bounding box padding.
  227. Vector3 padding_;
  228. /// Number of tiles in X direction.
  229. int numTilesX_;
  230. /// Number of tiles in Z direction.
  231. int numTilesZ_;
  232. /// Whole navigation mesh bounding box.
  233. BoundingBox boundingBox_;
  234. /// Type of the heightfield partitioning.
  235. NavmeshPartitionType partitionType_;
  236. /// Keep internal build resources for debug draw modes.
  237. bool keepInterResults_;
  238. /// Internal build resources for creating the navmesh.
  239. HashMap<Pair<int, int>, NavBuildData*> builds_;
  240. };
  241. /// Register Navigation library objects.
  242. void ATOMIC_API RegisterNavigationLibrary(Context* context);
  243. }