navigation_using_navigationpaths.rst 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. .. _doc_navigation_using_navigationpaths:
  2. Using NavigationPaths
  3. =====================
  4. Obtaining a Navigationpath
  5. --------------------------
  6. Navigation paths can be directly queried from the NavigationServer and do not require any
  7. additional nodes or objects as long as the navigation map has a navigationmesh to work with.
  8. To obtain a 2D path, use ``NavigationServer2D.map_get_path(map, from, to, optimize, navigation_layers)``.
  9. To obtain a 3D path, use ``NavigationServer3D.map_get_path(map, from, to, optimize, navigation_layers)``.
  10. For more customizable navigation path queries that require additional setup see :ref:`doc_navigation_using_navigationpathqueryobjects`.
  11. One of the required parameters for the query is the RID of the navigation map.
  12. Each game ``World`` has a default navigation map automatically created.
  13. The default navigation maps can be retrieved with ``get_world_2d().get_navigation_map()`` from
  14. any Node2D inheriting node or ``get_world_3d().get_navigation_map()`` from any Node3D inheriting node.
  15. The second and third parameters are the starting position and the target position as Vector2 for 2D or Vector3 for 3D.
  16. If the ``optimized`` parameter is ``true``, path positions will be shortened along polygon
  17. corners with an additional funnel algorithm pass. This works well for free movement
  18. on navigationmeshes with unequal sized polygons as the path will hug around corners
  19. along the polygon corridor found by the A* algorithm. With small cells the A* algorithm
  20. creates a very narrow funnel corridor that can create ugly corner paths when used with grids.
  21. If the ``optimized`` parameter is ``false``, path positions will be placed at the center of each polygon edge.
  22. This works well for pure grid movement on navmeshes with equal sized polygons as the path will go through the center of the grid cells.
  23. Outside of grids due to polygons often covering large open areas with a single, long edge this can create paths with unnecessary long detours.
  24. .. tabs::
  25. .. code-tab:: gdscript GDScript
  26. extends Node2D
  27. # basic query for a navigation path in 2D using the default navigation map
  28. var default_2d_map_rid: RID = get_world_2d().get_navigation_map()
  29. var start_position: Vector2 = Vector2(0.0, 0.0)
  30. var target_position: Vector2 = Vector2(5.0, 0.0)
  31. var path: PackedVector2Array = NavigationServer2D.map_get_path(
  32. default_2d_map_rid,
  33. start_position,
  34. target_position,
  35. true
  36. )
  37. .. tabs::
  38. .. code-tab:: gdscript GDScript
  39. extends Node3D
  40. # basic query for a navigation path in 3D using the default navigation map
  41. var default_3d_map_rid: RID = get_world_3d().get_navigation_map()
  42. var start_position: Vector3 = Vector3(0.0, 0.0, 0.0)
  43. var target_position: Vector3 = Vector3(5.0, 0.0, 3.0)
  44. var path: PackedVector3Array = NavigationServer3D.map_get_path(
  45. default_3d_map_rid,
  46. start_position,
  47. target_position,
  48. true
  49. )
  50. A returned ``path`` by the NavigationServer will be a ``PackedVector2Array`` for 2D or a ``PackedVector3Array`` for 3D.
  51. These are just a memory-optimized ``Array`` of vector positions.
  52. All position vectors inside the array are guaranteed to be inside a NavigationPolygon or NavigationMesh.
  53. The path array, if not empty, has the navigationmesh position closest to the starting position at the first index ``path[0]`` position.
  54. The closest available navigationmesh position to the target position is the last index ``path[path.size()-1]`` position.
  55. All index between are the pathpoints that an actor should follow to reach the target without leaving the navigation mesh.
  56. .. note::
  57. If the target position is on a different navigation mesh that is not merged or connected
  58. the navigation path will lead to the closest possible position on the starting position navigation mesh.
  59. The following script moves a Node3D inheriting node along a navigation path using
  60. the default navigation map by setting the target position with ``set_movement_target()``.
  61. .. tabs::
  62. .. code-tab:: gdscript GDScript
  63. var movement_speed: float = 4.0
  64. var movement_delta: float
  65. var path_point_margin: float = 0.5
  66. var default_3d_map_rid: RID = get_world_3d().get_navigation_map()
  67. var current_path_index: int = 0
  68. var current_path_point: Vector3
  69. var current_path: PackedVector3Array
  70. func set_movement_target(target_position: Vector3):
  71. var start_position: Vector3 = global_transform.origin
  72. current_path = NavigationServer3D.map_get_path(
  73. default_3d_map_rid,
  74. start_position,
  75. target_position,
  76. true
  77. )
  78. if not current_path.is_empty():
  79. current_path_index = 0
  80. current_path_point = current_path[0]
  81. func _physics_process(delta):
  82. if current_path.is_empty():
  83. return
  84. movement_delta = move_speed * delta
  85. if global_transform.origin.distance_to(current_path_point) <= path_point_margin:
  86. current_path_index += 1
  87. if current_path_index >= current_path.size():
  88. current_path = []
  89. current_path_index = 0
  90. current_path_point = global_transform.origin
  91. return
  92. current_path_point = current_path[current_path_index]
  93. var new_velocity: Vector3 = (current_path_point - global_transform.origin).normalized() * movement_delta
  94. global_transform.origin = global_transform.origin.move_toward(global_transform.origin + new_velocity, movement_delta)