navigation_using_navigationpaths.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 navigation mesh 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 navigation meshes with unequally 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 navigation meshes with equally 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 2D GDScript
  26. extends Node2D
  27. # Basic query for a navigation path using the default navigation map.
  28. func get_navigation_path(p_start_position: Vector2, p_target_position: Vector2) -> PackedVector2Array:
  29. if not is_inside_tree():
  30. return PackedVector2Array()
  31. var default_map_rid: RID = get_world_2d().get_navigation_map()
  32. var path: PackedVector2Array = NavigationServer2D.map_get_path(
  33. default_map_rid,
  34. p_start_position,
  35. p_target_position,
  36. true
  37. )
  38. return path
  39. .. code-tab:: gdscript 3D GDScript
  40. extends Node3D
  41. # Basic query for a navigation path using the default navigation map.
  42. func get_navigation_path(p_start_position: Vector3, p_target_position: Vector3) -> PackedVector3Array:
  43. if not is_inside_tree():
  44. return PackedVector3Array()
  45. var default_map_rid: RID = get_world_3d().get_navigation_map()
  46. var path: PackedVector3Array = NavigationServer3D.map_get_path(
  47. default_map_rid,
  48. p_start_position,
  49. p_target_position,
  50. true
  51. )
  52. return path
  53. A returned ``path`` by the NavigationServer will be a ``PackedVector2Array`` for 2D or a ``PackedVector3Array`` for 3D.
  54. These are just a memory-optimized ``Array`` of vector positions.
  55. All position vectors inside the array are guaranteed to be inside a NavigationPolygon or NavigationMesh.
  56. The path array, if not empty, has the navigation mesh position closest to the starting position at the first index ``path[0]`` position.
  57. The closest available navigation mesh position to the target position is the last index ``path[path.size()-1]`` position.
  58. All indexes between are the path points that an actor should follow to reach the target without leaving the navigation mesh.
  59. .. note::
  60. If the target position is on a different navigation mesh that is not merged or connected
  61. the navigation path will lead to the closest possible position on the starting position navigation mesh.
  62. The following script moves a Node3D inheriting node along a navigation path using
  63. the default navigation map by setting the target position with ``set_movement_target()``.
  64. .. tabs::
  65. .. code-tab:: gdscript GDScript
  66. @onready var default_3d_map_rid: RID = get_world_3d().get_navigation_map()
  67. var movement_speed: float = 4.0
  68. var movement_delta: float
  69. var path_point_margin: float = 0.5
  70. var current_path_index: int = 0
  71. var current_path_point: Vector3
  72. var current_path: PackedVector3Array
  73. func set_movement_target(target_position: Vector3):
  74. var start_position: Vector3 = global_transform.origin
  75. current_path = NavigationServer3D.map_get_path(
  76. default_3d_map_rid,
  77. start_position,
  78. target_position,
  79. true
  80. )
  81. if not current_path.is_empty():
  82. current_path_index = 0
  83. current_path_point = current_path[0]
  84. func _physics_process(delta):
  85. if current_path.is_empty():
  86. return
  87. movement_delta = movement_speed * delta
  88. if global_transform.origin.distance_to(current_path_point) <= path_point_margin:
  89. current_path_index += 1
  90. if current_path_index >= current_path.size():
  91. current_path = []
  92. current_path_index = 0
  93. current_path_point = global_transform.origin
  94. return
  95. current_path_point = current_path[current_path_index]
  96. var new_velocity: Vector3 = global_transform.origin.direction_to(current_path_point) * movement_delta
  97. global_transform.origin = global_transform.origin.move_toward(global_transform.origin + new_velocity, movement_delta)