navigation_using_agent_avoidance.rst 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. .. _doc_navigation_using_agent_avoidance:
  2. Using Agent Avoidance
  3. =====================
  4. This section is about how to use agent avoidance with the NavigationServer and
  5. documents how agent avoidance is implemented in Godot.
  6. For avoidance with NavigationAgents see :ref:`doc_navigation_using_navigationagents`.
  7. Agent avoidance helps to prevent direct collision with other agents or moving obstacles
  8. while the agents still follow their original velocity as best as possible.
  9. Avoidance in Godot is implemented with the help of the RVO library (Reciprocal Velocity Obstacle).
  10. RVO places agents on a flat RVO map and gives each agent a ``radius`` and a ``position``.
  11. Agents with overlapping radius compute a ``safe_velocity`` from their
  12. current ``velocity``. The ``safe_velocity`` then needs to replace the original
  13. submitted ``velocity`` to move the actor behind the agent with custom movement code.
  14. .. note::
  15. RVO avoidance is not involved in regular pathfinding, it is a completely separate system.
  16. If used inappropriately, the RVO avoidance can actively harm the perceived pathfinding quality.
  17. Creating Avoidance Agents with Scripts
  18. --------------------------------------
  19. Agents and obstacles share the same NavigationServer API functions.
  20. Creating agents on the NavigationServer is only required for avoidance but not for normal pathfinding.
  21. Pathfinding is map and region navmesh based while avoidance is purely map and agent based.
  22. .. tabs::
  23. .. code-tab:: gdscript GDScript
  24. extends Node3D
  25. var new_agent_rid: RID = NavigationServer3D.agent_create()
  26. var default_3d_map_rid: RID = get_world_3d().get_navigation_map()
  27. NavigationServer3D.agent_set_map(new_agent_rid, default_3d_map_rid)
  28. NavigationServer3D.agent_set_radius(new_agent_rid, 0.5)
  29. NavigationServer3D.agent_set_position(new_agent_rid, global_transform.origin)
  30. To receive safe_velocity signals for avoidance for the agent a callback needs to be registered on the NavigationServer.
  31. .. tabs::
  32. .. code-tab:: gdscript GDScript
  33. extends Node3D
  34. var agent_rid: RID = NavigationServer3D.agent_create()
  35. NavigationServer3D.agent_set_callback(agent_rid, self.on_safe_velocity_computed)
  36. func on_safe_velocity_computed(safe_velocity: Vector3):
  37. # do your avoidance movement
  38. After the current and new calculated velocity needs to be passed to the NavigationServer each physics frame to trigger the safe_velocity callback when the avoidance processing is finished.
  39. .. tabs::
  40. .. code-tab:: gdscript GDScript
  41. func _physics_process(delta):
  42. NavigationServer3D.agent_set_velocity(current_velocity)
  43. NavigationServer3D.agent_set_target_velocity(new_velocity)
  44. .. warning::
  45. If _process() is used instead of _physics_process() at a higher framerate
  46. than physics the agent velocity should not be updated more than ones each
  47. physics frame e.g. by tracking the Engine.get_physics_frames().