2
0

navigation_using_navigationagents.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. .. _doc_navigation_using_navigationagents:
  2. Using NavigationAgents
  3. ======================
  4. NavigationsAgents are helper nodes to facilitate common calls to the NavigationServer API
  5. on behalf of the parent actor node in a more convenient manner for beginners.
  6. 2D and 3D version of NavigationAgents are available as
  7. :ref:`NavigationAgent2D<class_NavigationAgent2D>` and
  8. :ref:`NavigationAgent3D<class_NavigationAgent3D>` respectively.
  9. NavigationsAgents are entirely optional for navigation pathfinding.
  10. The functionality of NavigationsAgents can be recreated with scripts and direct
  11. calls to the NavigationServer API. If the default NavigationsAgent does not do what you want
  12. for your game feel free to design your own NavigationsAgent with scripts.
  13. .. warning::
  14. NavigationsAgent nodes and NavigationServer ``agents`` are not the same.
  15. The later is an RVO avoidance agent and solely used for avoidance.
  16. RVO avoidance agents are not involved in regular pathfinding.
  17. NavigationAgent Pathfinding
  18. ---------------------------
  19. To use NavigationAgents for pathfinding, place a NavigationAgent2D/3D Node below a Node2D/3D inheriting parent node.
  20. To have the agent query a path to a target position use the ``set_target_position()`` method.
  21. Once the target has been set, the next position to follow in the path
  22. can be retrieved with the ``get_next_path_position()`` function. Move the parent actor node
  23. to this position with your own movement code. On the next ``physics_frame``, call
  24. ``get_next_path_position()`` again for the next position and repeat this until the path ends.
  25. NavigationAgents have their own internal logic to proceed with the current path and call for updates.
  26. NavigationAgents recognize by distance when a path point or the final target is reached.
  27. NavigationAgents refresh a path automatically when too far away from the current pathpoint.
  28. The important updates are all triggered with the ``get_next_path_position()`` function
  29. when called in ``_physics_process()``.
  30. Be careful calling other NavigationAgent functions not required for path movement
  31. while the actor is following a path, as many function trigger a full path refresh.
  32. .. note::
  33. New NavigationAgents will automatically join the
  34. default navigation map for their 2D/3D dimension.
  35. .. warning::
  36. Resetting the path every frame (by accident) might get the actor to stutter or spin around in place.
  37. NavigationAgents were designed with ``_physics_process()`` in mind to keep in sync with both :ref:`NavigationServer3D<class_NavigationServer3D>` and :ref:`PhysicsServer3D<class_PhysicsServer3D>`.
  38. They work well out of the box with :ref:`CharacterBody2D<class_CharacterBody2D>` and :ref:`CharacterBody3D<class_CharacterBody3D>` as well as any rigid bodies.
  39. .. warning::
  40. The important restriction for non-physics characters is that the NavigationAgent node only accepts a single update each ``physics_frame`` as further updates will be blocked.
  41. .. warning::
  42. If a NavigationAgent is used with ``_process()`` at high framerate make sure to accumulate the values of multiple frames and call the NavigationAgent function only once each ``physics_frame``.
  43. .. _doc_navigation_script_templates:
  44. NavigationAgent Avoidance
  45. -------------------------
  46. This section explains how to use the built-in avoidance specific
  47. to NavigationAgent nodes. For general avoidance use and more technical details
  48. on RVO avoidance see :ref:`doc_navigation_using_agent_avoidance`.
  49. In order for NavigationAgents to use the avoidance feature the ``enable_avoidance`` property must be set to ``true``.
  50. .. image:: img/agent_avoidance_enabled.png
  51. .. note::
  52. Only other agents on the same map that are registered for avoidance themself will be considered in the avoidance calculation.
  53. The following NavigationAgent properties are relevant for avoidance:
  54. - The property ``radius`` controls the size of the avoidance circle around the agent. This area describes the agents body and not the avoidance maneuver distance.
  55. - The property ``neighbor_distance`` controls the search radius of the agent when searching for other agents that should be avoided. A lower value reduces processing cost.
  56. - The property ``max_neighbors`` controls how many other agents are considered in the avoidance calculation if they all have overlapping radius.
  57. A lower value reduces processing cost but a too low value may result in agents ignoring the avoidance.
  58. - The property ``time_horizion`` controls the avoidance maneuver start and end distance.
  59. How early and for how long an agents reacts to other agents within the ``neighbor_distance`` radius to correct its own velocity.
  60. A lower value results in avoidance kicking in with a very intense velocity change at a short distance while a high value results in very early but small velocity changes.
  61. - The property ``max_speed`` controls the maximum velocity assumed for the agents avoidance calculation.
  62. If the agents parents moves faster than this value the avoidance ``safe_velocity`` might not be accurate enough to avoid collision.
  63. The ``velocity_computed`` signal of the agent node must be connected to receive the ``safe_velocity`` calculation result.
  64. .. image:: img/agent_safevelocity_signal.png
  65. Additional the current velocity of the agents parent must be set for the agent in ``_physics_process()`` with ``set_velocity()``.
  66. After a short wait for processing the avoidance (still in the same frame) the ``safe_velocity`` vector will be received with the signal.
  67. This velocity vector should be used to move the NavigationAgent's parent node in order to avoidance collision with other avoidance registered agents in proximity.
  68. RVO exists in its own space and has no information from navigation meshes or physics collision.
  69. Behind the scene avoidance agents are just circles with different radius on a flat plane.
  70. In narrow places obstructed with collision objects, the avoidance maneuver radius needs to be
  71. reduced considerably or disabled else the avoidance velocity will get actors stuck on collision easily.
  72. .. note::
  73. Avoidance should be seen as a last resort option for constantly moving objects that cannot be re(baked) to a navigationmesh efficiently in order to move around them.
  74. .. warning::
  75. Actors that move according to their avoidance agent velocity will not move at
  76. full speed, can leave the navigation mesh bounds and can make movement
  77. pauses when the avoidance simulation becomes unsolvable.
  78. Using the NavigationAgent ``enable_avoidance`` property is the preferred option
  79. to toggle avoidance but the following scripts for NavigationAgents can be
  80. used to create or delete avoidance callbacks for the agent RID.
  81. .. tabs::
  82. .. code-tab:: gdscript GDScript
  83. extends NavigationAgent2D
  84. var agent: RID = get_rid()
  85. # Create avoidance callback
  86. NavigationServer2D.agent_set_callback(agent, Callable(self, "_avoidance_done"))
  87. # Delete avoidance callback
  88. NavigationServer2D.agent_set_callback(agent, Callable())
  89. .. tabs::
  90. .. code-tab:: gdscript GDScript
  91. extends NavigationAgent3D
  92. var agent: RID = get_rid()
  93. # Create avoidance callback
  94. NavigationServer3D.agent_set_callback(agent, Callable(self, "_avoidance_done"))
  95. # Delete avoidance callback
  96. NavigationServer3D.agent_set_callback(agent, Callable())
  97. NavigationAgent Script Templates
  98. --------------------------------
  99. The following sections provides script templates for nodes commonly used with NavigationAgents.
  100. Actor as Node3D
  101. ~~~~~~~~~~~~~~~
  102. This script adds basic navigation movement to a Node3D with a NavigationAgent3D child node.
  103. .. tabs::
  104. .. code-tab:: gdscript GDScript
  105. extends Node3D
  106. @export var movement_speed: float = 4.0
  107. @onready var navigation_agent: NavigationAgent3D = get_node("NavigationAgent3D")
  108. var movement_delta: float
  109. func _ready() -> void:
  110. navigation_agent.velocity_computed.connect(Callable(_on_velocity_computed))
  111. func set_movement_target(movement_target: Vector3):
  112. navigation_agent.set_target_position(movement_target)
  113. func _physics_process(delta):
  114. if navigation_agent.is_navigation_finished():
  115. return
  116. movement_delta = movement_speed * delta
  117. var next_path_position: Vector3 = navigation_agent.get_next_path_position()
  118. var current_agent_position: Vector3 = global_position
  119. var new_velocity: Vector3 = (next_path_position - current_agent_position).normalized() * movement_delta
  120. if navigation_agent.avoidance_enabled:
  121. navigation_agent.set_velocity(new_velocity)
  122. else:
  123. _on_velocity_computed(new_velocity)
  124. func _on_velocity_computed(safe_velocity: Vector3) -> void:
  125. global_position = global_position.move_toward(global_position + safe_velocity, movement_delta)
  126. Actor as CharacterBody3D
  127. ~~~~~~~~~~~~~~~~~~~~~~~~
  128. This script adds basic navigation movement to a CharacterBody3D with a NavigationAgent3D child node.
  129. .. tabs::
  130. .. code-tab:: gdscript GDScript
  131. extends CharacterBody3D
  132. @export var movement_speed: float = 4.0
  133. @onready var navigation_agent: NavigationAgent3D = get_node("NavigationAgent3D")
  134. func _ready() -> void:
  135. navigation_agent.velocity_computed.connect(Callable(_on_velocity_computed))
  136. func set_movement_target(movement_target: Vector3):
  137. navigation_agent.set_target_position(movement_target)
  138. func _physics_process(delta):
  139. if navigation_agent.is_navigation_finished():
  140. return
  141. var next_path_position: Vector3 = navigation_agent.get_next_path_position()
  142. var current_agent_position: Vector3 = global_position
  143. var new_velocity: Vector3 = (next_path_position - current_agent_position).normalized() * movement_speed
  144. if navigation_agent.avoidance_enabled:
  145. navigation_agent.set_velocity(new_velocity)
  146. else:
  147. _on_velocity_computed(new_velocity)
  148. func _on_velocity_computed(safe_velocity: Vector3):
  149. velocity = safe_velocity
  150. move_and_slide()
  151. Actor as RigidBody3D
  152. ~~~~~~~~~~~~~~~~~~~~
  153. This script adds basic navigation movement to a RigidBody3D with a NavigationAgent3D child node.
  154. .. tabs::
  155. .. code-tab:: gdscript GDScript
  156. extends RigidBody3D
  157. @export var movement_speed: float = 4.0
  158. @onready var navigation_agent: NavigationAgent3D = get_node("NavigationAgent3D")
  159. func _ready() -> void:
  160. navigation_agent.velocity_computed.connect(Callable(_on_velocity_computed))
  161. func set_movement_target(movement_target: Vector3):
  162. navigation_agent.set_target_position(movement_target)
  163. func _physics_process(delta):
  164. if navigation_agent.is_navigation_finished():
  165. return
  166. var next_path_position: Vector3 = navigation_agent.get_next_path_position()
  167. var current_agent_position: Vector3 = global_position
  168. var new_velocity: Vector3 = (next_path_position - current_agent_position).normalized() * movement_speed
  169. if navigation_agent.avoidance_enabled:
  170. navigation_agent.set_velocity(new_velocity)
  171. else:
  172. _on_velocity_computed(new_velocity)
  173. func _on_velocity_computed(safe_velocity: Vector3):
  174. linear_velocity = safe_velocity