real_time_navigation_3d.rst 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. .. _doc_real_time_navigation_3d:
  2. Real Time Navigation (3D)
  3. =========================
  4. Introduction
  5. ------------
  6. Pathfinding in a 3D environment is crucial for many games, it's commonly
  7. how non directly controlled characters or entities find their way around
  8. an environment. Godot provides several nodes for this purpose:
  9. - :ref:`Navigation<class_Navigation>` (deprecated)
  10. - :ref:`NavigationMeshInstance<class_NavigationMeshInstance>`
  11. - :ref:`NavigationAgent<class_NavigationAgent>`
  12. - :ref:`NavigationObstacle<class_NavigationObstacle>`
  13. The map and navigation regions
  14. ------------------------------
  15. The "map" is the entire world for navigation, it's similar to "space" for
  16. the physics engine. It's comprised of navigation regions, these regions
  17. define parts of the world that can be navigated around by navigation
  18. agents.
  19. To create a navigation region add the :ref:`NavigationMeshInstance<class_NavigationMeshInstance>`
  20. node to a 3D scene. Next in the inspector for that mesh create or add a
  21. :ref:`NavigationMesh<class_NavigationMesh>`. The navmesh contains options
  22. for how it will be generated when it's baked. The geometry options control
  23. which nodes, and types of nodes, are used to bake the mesh. A full
  24. description of each setting and how it works can be found in the :ref:`NavigationMesh class reference<class_NavigationMesh>`.
  25. Once the settings have been properly configured press the "Bake NavMesh"
  26. button at the top of the inspector to generate it.
  27. .. image:: img/bake_navmesh.png
  28. .. note::
  29. It can also be generated at runtime using the `bake_navigation_region()`
  30. method of the navigation region node.
  31. Once the mesh has finished generating you should see the transparent
  32. navigation mesh above the areas in the scene that can be navigated to.
  33. .. image:: img/baked_navmesh.png
  34. Keep in mind that the navmesh shows where the center of an entity can
  35. go. For example, if you set the agent radius to 0.5 then the
  36. navigation mesh will have a distance of 0.5 from any ledges or walls
  37. to prevent clipping into the wall or hanging off of the edge.
  38. Navigation agents can moved from one region to another if they are next
  39. to each other. Additionally a baked navmesh can be moved at runtime and
  40. agents will still be able to navigate onto it from another region.
  41. For example, navigating onto a moving platform that has stopped will work.
  42. NavigationAgent3D
  43. -----------------
  44. Navigation agent nodes are what actually does the pathfinding in a scene,
  45. one can be attached to the root node of an entity that needs to navigate.
  46. To have it pathfind use its `set_target_location` method. Once the target
  47. has been set a path will be generated to the node using navigation regions,
  48. with several points on the way to the final destination.
  49. RVO processing
  50. --------------
  51. RVO stands for reciprocal velocity obstacle. RVO processing is a way to
  52. pathfind while taking into account other agents and physics bodies that
  53. are also moving.
  54. To use it set a target like normal. Then an agent needs to fetch its next
  55. nav path location, and compute its velocity to that location. Instead
  56. of using that value to move use it to set the velocity on the agent
  57. with `set_velocity`. Then a new velocity that takes into account other
  58. agents and obstacles is generated and emitted with the signal `velocity_computed`.
  59. However agents can only take into account a set number of other nearby
  60. agents, this is the :ref:`max neighbors<class_NavigationAgent_property_max_neighbors>`
  61. property of an agent and can be adjusted. This is **not** a limit for
  62. how many agents can use a navigation region at the same time.
  63. NavigationObstacle3D
  64. --------------------
  65. This node is used to mark physics bodies that move around a navigation area
  66. that agents need to avoid (this will only work if you use RVO processing).
  67. For example, this node would be useful for pieces of debris in a destructible
  68. environment. Add it as the child of a physics body and navigation agent
  69. nodes will avoid it while pathfinding.
  70. Generating a path (old method)
  71. ------------------------------
  72. This is the old method for generating a navigation path, it will be
  73. removed in Godot 4. First, add a navigation node to the scene, then
  74. add a navigation mesh instance as it's child and set up a navigation
  75. mesh.
  76. To get a path between two areas on a map you use the navigation node
  77. method ``get_simple_path()``. The first argument is a Vector3 of the
  78. starting location, the next is a Vector3 of the end location. And the
  79. last argument is a boolean for whether or not agent properties of a
  80. navmesh are considered when generating a path.
  81. The method will return a :ref:`PoolVector3Array <class_PoolVector3Array>` consisting of
  82. points that make a path. If there is no way to navigate to the end
  83. location the method will return a blank :ref:`PoolVector3Array <class_PoolVector3Array>`.