navigation_using_navigationmeshes.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. .. _doc_navigation_using_navigationmeshes:
  2. Using NavigationMeshes
  3. ======================
  4. 2D and 3D version of the navigation mesh are available as
  5. :ref:`NavigationPolygon<class_NavigationPolygon>` and
  6. :ref:`NavigationMesh<class_NavigationMesh>` respectively.
  7. .. note::
  8. A navigation mesh describes the traversable safe area for an agent with its center position at zero radius.
  9. If you want pathfinding to account for an agent's (collision) size you need to shrink the navigation mesh accordingly.
  10. Navigation works independent from other engine parts like rendering and physics. A navigation mesh is the data format to exchange information from those other systems as it describes the traversable safe area for a specific agent. All the necessary information from other engine parts need to be already factored in when creating a navigation mesh. E.g. like visuals that an agent should not clip through or physics collision shapes that an agent should not collide with. This process of factoring in all those wanted navigation restrictions from other engine parts like visuals and collision is commonly called navigation mesh baking.
  11. If you experience clipping or collision problems while following navigation paths always remember that you need to tell the navigation system through an appropriated navigation mesh what your intentions are. By itself the navigation system will never know "this is a tree / rock / wall collision shape or visual mesh" because it only knows "here I was told I can path safely cause it is on navigation mesh".
  12. .. _doc_navigation_navmesh_baking:
  13. Creating 2D NavigationMeshes
  14. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  15. Navigation meshes in the 2D editor are created with the help of the NavigationPolygon draw tools
  16. that appear in the top bar of the editor when a NavigationRegion2D is selected.
  17. .. image:: img/nav_polydrawtool.png
  18. The NavigationPolygon draw tools can be used to create and edit navigation meshes by defining ``outline`` polygons.
  19. The outline polygons are later converted to real NavigationMesh resources for the NavigationServer regions.
  20. .. image:: img/nav_polymatroschka.png
  21. Multiple outlines can be added to the same NavPolygon resource as long as they **do not intersect or overlap**.
  22. Each additional outline will cut a hole in the polygon created by the larger outline.
  23. If the larger polygon is already a hole it will create a new navigation mesh polygon inside.
  24. Outlines are not a replacement if the intention is to merge aligned polygons e.g. from grid cells.
  25. Outlines, as the name would suggest, cannot intersect each other or have any overlapping vertex positions.
  26. .. image:: img/nav_polyoutlinefail.png
  27. Outline layouts like seen in this picture will fail the convex partitioning required by the navigation mesh generation.
  28. In this layout cases the outline tool cannot be used. Use the :ref:`Geometry2D<class_Geometry2D>` class for
  29. polygon merge or intersect operations to create a valid merged mesh for navigation.
  30. .. note::
  31. The NavigationServer does not connect navigation mesh islands from the same NavigationMesh resource.
  32. Do not create multiple disconnected islands in the same NavigationRegion2D and NavPoly resource if they should be later connected.
  33. For 2D no similar navigation mesh baking with geometry parsing exists like in 3D.
  34. The Geometry2D class functions for offset, merge, intersect and clip can be used
  35. to shrink or enlarge existing NavigationPolygons to different actor sizes.
  36. Creating 3D NavigationMeshes
  37. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  38. .. image:: img/baked_navmesh.png
  39. Navigation meshes in the 3D editor are created with the help of the
  40. :ref:`NavigationMeshGenerator<class_NavigationMeshGenerator>` singleton
  41. and the NavigationMesh bake settings that appear in the editor inspector.
  42. NavigationMesh baking is the process of creating a simplified mesh used for pathfinding out of (complex) 3D level geometry.
  43. For this process Godot parses scene geometry and hands the raw mesh or collision data to the
  44. third-party ReCast library for processing and creation of the final navigationmesh.
  45. The resulting NavigationMesh is an approximation of the source geometry surfaces
  46. for both performance and technical reasons. Do not expect the NavigationMesh
  47. to perfectly follow the original surfaces. Especially navigation polygons placed
  48. over ramps will not keep an equal distance to the ground surface. To align an
  49. actor perfectly with the ground use other means like physics.
  50. .. warning::
  51. Meshes need to be triangulated to work as navigation meshes. Other mesh face formats like quad or ngon are not supported.
  52. NavigationMesh rebaking at runtime
  53. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  54. To rebake a ``NavigationMesh`` at runtime, use the NavigationRegion3D.bake_navigation_mesh() function.
  55. Another option is to use the NavigationMeshGenerator.bake() Singleton function with the NavigationMesh resource directly.
  56. If the navigation mesh resource is already prepared, the region can be updated with the NavigationServer3D API directly as well.
  57. .. tabs::
  58. .. code-tab:: gdscript GDScript
  59. extends NavigationRegion3D
  60. func update_navigation_mesh():
  61. # use bake and update function of region
  62. var on_thread: bool = true
  63. bake_navigation_mesh(on_thread)
  64. # or use the NavigationMeshGenerator Singleton
  65. var _navigationmesh: NavigationMesh = navigation_mesh
  66. NavigationMeshGenerator.bake(_navigationmesh, self)
  67. # remove old resource first to trigger a full update
  68. navigation_mesh = null
  69. navigation_mesh = _navigationmesh
  70. # or use NavigationServer API to update region with prepared navigation mesh
  71. var region_rid: RID = get_region_rid()
  72. NavigationServer3D.region_set_navigation_mesh(region_rid, navigation_mesh)
  73. .. note::
  74. Baking a NavigationMesh at runtime is a costly operation.
  75. Complex navigation mesh take some time to bake and if done on the main thread can freeze a game.
  76. (Re)baking a large navigation mesh is preferably done in a separate thread.
  77. .. warning::
  78. Property values on a NavigationMesh resource like ``cell_size`` need
  79. to match the actual mesh data stored inside in order to merge
  80. different navigation meshes without issues.
  81. NavigationRegion2D and Navigation3D both use meshes to mark traversable areas, only the tools to create them are different.
  82. For 2D NavigationPolygon resources are used to draw outline points in the editor. From these outline points the NavigationServer2D creates a mesh to upload navigation data to the NavigationServer.
  83. For 3D NavigationMesh resources are used. Instead of providing draw tools the 3D variant
  84. provides an extensive amount of parameters to bake a navigation mesh directly from 3D source geometry.
  85. .. note::
  86. Technically there is no hard distinction between 2D and 3D how to use the given toolsets to create flat navigation meshes. The 2D drawing tool can be used to create a flat 3D navmesh and the 3D baking tool can be used to parse flat 3D geometry into 2D appropriated navigationmeshes.
  87. 2D Navmesh from CollisionPolygons
  88. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  89. The following script parses all child nodes of a NavigationRegion2D for CollisionPolygons
  90. and bakes their shape into the NavigationPolygon. As the NavigationPolygon creates the
  91. navigationmesh from outline data the shapes cannot overlap.
  92. .. tabs::
  93. .. code-tab:: gdscript GDScript
  94. extends NavigationRegion2D
  95. var new_navigation_polygon: NavigationPolygon = get_navigation_polygon()
  96. func _ready():
  97. parse_2d_collisionshapes(self)
  98. new_navigation_polygon.make_polygons_from_outlines()
  99. set_navigation_polygon(new_navigation_polygon)
  100. func parse_2d_collisionshapes(root_node: Node2D):
  101. for node in root_node.get_children():
  102. if node.get_child_count() > 0:
  103. parse_2d_collisionshapes(node)
  104. if node is CollisionPolygon2D:
  105. var collisionpolygon_transform: Transform2D = node.get_global_transform()
  106. var collisionpolygon: PackedVector2Array = node.polygon
  107. var new_collision_outline: PackedVector2Array = collisionpolygon_transform * collisionpolygon
  108. new_navigation_polygon.add_outline(new_collision_outline)
  109. Procedual 2D NavigationMesh
  110. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  111. The following script creates a new 2D navigation region and fills it with procedual generated navigation mesh data from a NavigationPolygon resource.
  112. .. tabs::
  113. .. code-tab:: gdscript GDScript
  114. extends Node2D
  115. var new_2d_region_rid: RID = NavigationServer2D.region_create()
  116. var default_2d_map_rid: RID = get_world_2d().get_navigation_map()
  117. NavigationServer2D.region_set_map(new_2d_region_rid, default_2d_map_rid)
  118. var new_navigation_polygon: NavigationPolygon = NavigationPolygon.new()
  119. var new_outline: PackedVector2Array = PackedVector2Array([
  120. Vector2(0.0, 0.0),
  121. Vector2(50.0, 0.0),
  122. Vector2(50.0, 50.0),
  123. Vector2(0.0, 50.0),
  124. ])
  125. new_navigation_polygon.add_outline(new_outline)
  126. new_navigation_polygon.make_polygons_from_outlines()
  127. NavigationServer2D.region_set_navigation_polygon(new_2d_region_rid, new_navigation_polygon)
  128. Procedual 3D NavigationMesh
  129. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  130. The following script creates a new 3D navigation region and fills it with procedual generated navigation mesh data from a NavigationMesh resource.
  131. .. tabs::
  132. .. code-tab:: gdscript GDScript
  133. extends Node3D
  134. var new_3d_region_rid: RID = NavigationServer3D.region_create()
  135. var default_3d_map_rid: RID = get_world_3d().get_navigation_map()
  136. NavigationServer3D.region_set_map(new_3d_region_rid, default_3d_map_rid)
  137. var new_navigation_mesh: NavigationMesh = NavigationMesh.new()
  138. # Add vertices for a triangle.
  139. new_navigation_mesh.vertices = PackedVector3Array([
  140. Vector3(-1.0, 0.0, 1.0),
  141. Vector3(1.0, 0.0, 1.0),
  142. Vector3(1.0, 0.0, -1.0)
  143. ])
  144. # Add indices for the polygon.
  145. new_navigation_mesh.add_polygon(
  146. PackedInt32Array([0, 1, 2])
  147. )
  148. NavigationServer3D.region_set_navigation_mesh(new_3d_region_rid, new_navigation_mesh)
  149. Navmesh for 3D GridMaps
  150. ~~~~~~~~~~~~~~~~~~~~~~~
  151. The following script creates a new 3D navigation mesh for each GridMap items, clears the current grid cells and adds new procedual grid cells with the new navigation mesh.
  152. .. tabs::
  153. .. code-tab:: gdscript GDScript
  154. extends GridMap
  155. # enable navigation mesh for grid items
  156. set_bake_navigation(true)
  157. # get grid items, create and set a new navigation mesh for each item in the MeshLibrary
  158. var gridmap_item_list: PackedInt32Array = mesh_library.get_item_list()
  159. for item in gridmap_item_list:
  160. var new_item_navigation_mesh: NavigationMesh = NavigationMesh.new()
  161. # Add vertices and polygons that describe the traversable ground surface.
  162. # E.g. for a convex polygon that resembles a flat square.
  163. new_item_navigation_mesh.vertices = PackedVector3Array([
  164. Vector3(-1.0, 0.0, 1.0),
  165. Vector3(1.0, 0.0, 1.0),
  166. Vector3(1.0, 0.0, -1.0),
  167. Vector3(-1.0, 0.0, -1.0),
  168. ])
  169. new_item_navigation_mesh.add_polygon(
  170. PackedInt32Array([0, 1, 2, 3])
  171. )
  172. mesh_library.set_item_navigation_mesh(item, new_item_navigation_mesh)
  173. mesh_library.set_item_navigation_mesh_transform(item, Transform3D())
  174. # clear the cells
  175. clear()
  176. # add procedual cells using the first item
  177. var _position: Vector3i = Vector3i(global_transform.origin)
  178. var _item: int = 0
  179. var _orientation: int = 0
  180. for i in range(0,10):
  181. for j in range(0,10):
  182. _position.x = i
  183. _position.z = j
  184. gridmap.set_cell_item(_position, _item, _orientation)
  185. _position.x = -i
  186. _position.z = -j
  187. gridmap.set_cell_item(_position, _item, _orientation)