NavigationMesh.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // Copyright (c) 2008-2013 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 "ArrayPtr.h"
  24. #include "BoundingBox.h"
  25. #include "Component.h"
  26. class rcContext;
  27. class dtNavMesh;
  28. struct rcHeightfield;
  29. struct rcCompactHeightfield;
  30. struct rcContourSet;
  31. struct rcPolyMesh;
  32. struct rcPolyMeshDetail;
  33. namespace Urho3D
  34. {
  35. class Geometry;
  36. /// Navigation mesh component. Collects the navigation geometry from child nodes with the Navigable component and responds to path queries.
  37. class NavigationMesh : public Component
  38. {
  39. OBJECT(NavigationMesh);
  40. public:
  41. /// Construct.
  42. NavigationMesh(Context* context);
  43. /// Destruct.
  44. virtual ~NavigationMesh();
  45. /// Register object factory.
  46. static void RegisterObject(Context* context);
  47. /// Set cell size.
  48. void SetCellSize(float size);
  49. /// Set cell height.
  50. void SetCellHeight(float height);
  51. /// Set navigation agent height.
  52. void SetAgentHeight(float height);
  53. /// Set navigation agent radius.
  54. void SetAgentRadius(float radius);
  55. /// Set navigation agent max vertical climb.
  56. void SetAgentMaxClimb(float maxClimb);
  57. /// Set navigation agent max slope.
  58. void SetAgentMaxSlope(float maxSlope);
  59. /// Set region minimum size.
  60. void SetRegionMinSize(float size);
  61. /// Set region merge size.
  62. void SetRegionMergeSize(float size);
  63. /// Set edge max length.
  64. void SetEdgeMaxLength(float length);
  65. /// Set edge max error.
  66. void SetEdgeMaxError(float error);
  67. /// Set detail sampling distance.
  68. void SetDetailSampleDistance(float distance);
  69. /// Set detail sampling maximum error.
  70. void SetDetailSampleMaxError(float error);
  71. /// Retun cell size.
  72. float GetCellSize() const { return cellSize_; }
  73. /// Return cell height.
  74. float GetCellHeight() const { return cellHeight_; }
  75. /// Return navigation agent height.
  76. float GetAgentHeight() const { return agentHeight_; }
  77. /// Return navigation agent radius.
  78. float GetAgentRadius() const { return agentRadius_; }
  79. /// Return navigation agent max vertical climb.
  80. float GetAgentMaxClimb() const { return agentMaxClimb_; }
  81. /// Return navigation agent max slope.
  82. float GetAgentMaxSlope() const { return agentMaxSlope_; }
  83. /// Return region minimum size.
  84. float GetRegionMinSize() const { return regionMinSize_; }
  85. /// Return region merge size.
  86. float GetRegionMergeSize() const { return regionMergeSize_; }
  87. /// Return edge max length.
  88. float GetEdgeMaxLength() const { return edgeMaxLength_; }
  89. /// Return edge max error.
  90. float GetEdgeMaxError() const { return edgeMaxError_; }
  91. /// Return detail sampling distance.
  92. float GetDetailSampleDistance() const { return detailSampleDistance_; }
  93. /// Return detail sampling maximum error.
  94. float GetDetailSampleMaxError() const { return detailSampleMaxError_; }
  95. /// Return the world bounding box.
  96. const BoundingBox& GetWorldBoundingBox() const { return worldBoundingBox_; }
  97. /// Build/rebuild the navigation mesh. Return true if successful.
  98. bool Build();
  99. private:
  100. /// Visit nodes and collect navigable geometry.
  101. void CollectGeometries(Node* node, Node* baseNode);
  102. /// Add a geometry to the mesh.
  103. void AddGeometry(Node* node, Geometry* geometry);
  104. /// Release Recast temporary data.
  105. void ReleaseBuildData();
  106. /// Release the Detour navmesh.
  107. void ReleaseNavMesh();
  108. /// Cell size.
  109. float cellSize_;
  110. /// Cell height.
  111. float cellHeight_;
  112. /// Navigation agent height.
  113. float agentHeight_;
  114. /// Navigation agent radius.
  115. float agentRadius_;
  116. /// Navigation agent max vertical climb.
  117. float agentMaxClimb_;
  118. /// Navigation agent max slope.
  119. float agentMaxSlope_;
  120. /// Region minimum size.
  121. float regionMinSize_;
  122. /// Region merge size.
  123. float regionMergeSize_;
  124. /// Edge max length.
  125. float edgeMaxLength_;
  126. /// Edge max error.
  127. float edgeMaxError_;
  128. /// Detail sampling distance.
  129. float detailSampleDistance_;
  130. /// Detail sampling maximum error.
  131. float detailSampleMaxError_;
  132. /// World-space bounding box of the navigation mesh.
  133. BoundingBox worldBoundingBox_;
  134. /// Build phase vertices.
  135. PODVector<Vector3> vertices_;
  136. /// Build phase triangle indices.
  137. PODVector<int> indices_;
  138. /// Recast context.
  139. rcContext* ctx_;
  140. /// Recast heightfield.
  141. rcHeightfield* heightField_;
  142. /// Recast compact heightfield.
  143. rcCompactHeightfield* compactHeightField_;
  144. /// Recast contour set.
  145. rcContourSet* contourSet_;
  146. /// Recast poly mesh.
  147. rcPolyMesh* polyMesh_;
  148. /// Recast detail poly mesh.
  149. rcPolyMeshDetail* polyMeshDetail_;
  150. /// Detour navmesh.
  151. dtNavMesh* navMesh_;
  152. };
  153. }