navigation_introduction_2d.rst 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. .. _doc_navigation_overview_2d:
  2. 2D Navigation Overview
  3. ======================
  4. Godot provides multiple objects, classes and servers to facilitate grid-based or mesh-based navigation and pathfinding for 2D and 3D games.
  5. The following section provides a quick overview over all available navigation related objects in Godot for 2D scenes and their primary use.
  6. Godot provides the following objects and classes for 2D navigation:
  7. - :ref:`Astar2D<class_Astar2D>`
  8. ``Astar2D`` objects provide an option to find the shortest path in a graph of weighted **points**.
  9. The AStar2D class is best suited for cellbased 2D gameplay that does not require actors to reach any possible position within an area but only predefined, distinct positions.
  10. - :ref:`NavigationServer2D<class_NavigationServer2D>`
  11. ``NavigationServer2D`` provides a powerful server API to find the shortest path between two positions on a area defined by a navigation mesh.
  12. The NavigationServer is best suited for 2D realtime gameplay that does require actors to reach any possible position within an navmesh defined area.
  13. Meshbased navigation scales well with large gameworlds as a large area can often be defined with a single polygon when it would require many, many grid cells.
  14. The NavigationServer holds different navigation maps that each consist of regions that hold navigation mesh data.
  15. Agents can be placed on a map for avoidance calculation.
  16. RIDs are used to reference the internal maps, regions and agents when communicating with the server.
  17. The following NavigationServer RID types are available.
  18. - NavMap RID
  19. Reference to a specific navigation map that holds regions and agents.
  20. The map will attempt to join changed navigation meshes of regions by proximity.
  21. The map will synchronise regions and agents each physics frame.
  22. - NavRegion RID
  23. Reference to a specific navigation region that can hold navigation mesh data.
  24. The region can be enabled / disabled or the use restricted with a navigationlayer bitmask.
  25. - NavAgent RID
  26. Reference to a specific avoidance agent with a radius value use solely in avoidance.
  27. The following SceneTree Nodes are available as helpers to work with the NavigationServer2D API.
  28. - :ref:`NavigationRegion2D<class_NavigationRegion2D>` Node
  29. A Node that holds a NavigationPolygon resource that defines a navigation mesh for the NavigationServer2D.
  30. The region can be enabled / disabled.
  31. The use in pathfinding can be further restricted through the navigationlayers bitmask.
  32. Regions can join their navigation meshes by proximity for a combined navigation mesh.
  33. - :ref:`NavigationAgent2D<class_NavigationAgent2D>` Node
  34. An optional helper Node to facilitate common NavigationServer2D API calls for pathfinding and avoidance
  35. for a Node2D inheriting parent Node.
  36. - :ref:`NavigationObstacle2D<class_NavigationObstacle2D>` Node
  37. A Node that acts as an agent with avoidance radius, to work it needs to be added under a Node2D
  38. inheriting parent Node. Obstacles are intended as a last resort option for constantly moving objects
  39. that cannot be re(baked) to a navigation mesh efficiently. This node also only works if RVO processing
  40. is being used.
  41. The 2D navigationm eshes are defined with the following resources:
  42. - :ref:`NavigationPolygon<class_NavigationPolygon>` Resource
  43. A resource that holds 2D navigation mesh data and provides polygon drawtools to define navigation areas inside the Editor as well as at runtime.
  44. - The NavigationRegion2D Node uses this resource to define its navigation area.
  45. - The NavigationServer2D uses this resource to update navmesh of individual regions.
  46. - The TileSet Editor creates and uses this resource internally when defining tile navigation areas.
  47. Setup for 2D scene
  48. ------------------
  49. The following steps show the basic setup for a minimum viable navigation in 2D that uses the
  50. NavigationServer2D and a NavigationAgent2D for path movement.
  51. #. Add a NavigationRegion2D Node to the scene.
  52. #. Click on the region node and add a new NavigationPolygon Resource to the region node.
  53. .. image:: img/nav_2d_min_setup_step1.png
  54. #. Define the moveable navigation area with the NavigationPolygon draw tool.
  55. .. image:: img/nav_2d_min_setup_step2.png
  56. .. note::
  57. The navigation mesh defines the area where an actor can stand and move with its center.
  58. Leave enough margin between the navpolygon edges and collision objects to not get path
  59. following actors repeatedly stuck on collision.
  60. #. Add a CharacterBody2D below the region node with a basic collision shape and a sprite or mesh
  61. for visuals.
  62. #. Add a NavigationAgent2D node below the character node.
  63. .. image:: img/nav_2d_min_setup_step3.png
  64. #. Add the following script to the CharacterBody2D node. We make sure to set a movement target
  65. after the scene has fully loaded and the NavigationServer had time to sync.
  66. .. tabs::
  67. .. code-tab:: gdscript GDScript
  68. extends CharacterBody2D
  69. var movement_speed : float = 200.0
  70. var movement_target_position : Vector2 = Vector2(60.0,180.0)
  71. @onready var navigation_agent : NavigationAgent2D = $NavigationAgent2D
  72. func _ready():
  73. # These values need to be adjusted for the actor's speed
  74. # and the navigation layout.
  75. navigation_agent.path_desired_distance = 4.0
  76. navigation_agent.target_desired_distance = 4.0
  77. # Make sure to not await during _ready.
  78. call_deferred("actor_setup")
  79. func actor_setup():
  80. # Wait for the first physics frame so the NavigationServer can sync.
  81. await get_tree().physics_frame
  82. # Now that the navigation map is no longer empty, set the movement target.
  83. set_movement_target(movement_target_position)
  84. func set_movement_target(movement_target : Vector2):
  85. navigation_agent.set_target_location(movement_target)
  86. func _physics_process(delta):
  87. if navigation_agent.is_target_reached():
  88. return
  89. var current_agent_position : Vector2 = global_transform.origin
  90. var next_path_position : Vector2 = navigation_agent.get_next_location()
  91. var new_velocity : Vector2 = next_path_position - current_agent_position
  92. new_velocity = new_velocity.normalized()
  93. new_velocity = new_velocity * movement_speed
  94. set_velocity(new_velocity)
  95. move_and_slide()
  96. .. code-tab:: csharp C#
  97. using Godot;
  98. public partial class MyCharacterBody2D : CharacterBody2D
  99. {
  100. private NavigationAgent2D _navigationAgent;
  101. private float _movementSpeed = 200.0f;
  102. private Vector2 _movementTargetPosition = new Vector2(70.0f, 226.0f);
  103. public Vector2 MovementTarget
  104. {
  105. get { return _navigationAgent.TargetLocation; }
  106. set { _navigationAgent.TargetLocation = value; }
  107. }
  108. public override void _Ready()
  109. {
  110. base._Ready();
  111. _navigationAgent = GetNode<NavigationAgent2D>("NavigationAgent2D");
  112. // These values need to be adjusted for the actor's speed
  113. // and the navigation layout.
  114. _navigationAgent.PathDesiredDistance = 4.0f;
  115. _navigationAgent.TargetDesiredDistance = 4.0f;
  116. // Make sure to not await during _Ready.
  117. Callable.From(ActorSetup).CallDeferred();
  118. }
  119. public override void _PhysicsProcess(double delta)
  120. {
  121. base._PhysicsProcess(delta);
  122. if (_navigationAgent.IsTargetReached())
  123. {
  124. return;
  125. }
  126. Vector2 currentAgentPosition = GlobalTransform.Origin;
  127. Vector2 nextPathPosition = _navigationAgent.GetNextLocation();
  128. Vector2 newVelocity = (nextPathPosition - currentAgentPosition).Normalized();
  129. newVelocity *= _movementSpeed;
  130. Velocity = newVelocity;
  131. MoveAndSlide();
  132. }
  133. private async void ActorSetup()
  134. {
  135. // Wait for the first physics frame so the NavigationServer can sync.
  136. await ToSignal(GetTree(), SceneTree.SignalName.PhysicsFrame);
  137. // Now that the navigation map is no longer empty, set the movement target.
  138. MovementTarget = _movementTargetPosition;
  139. }
  140. }
  141. .. note::
  142. On the first frame the NavigationServer map has not synchronised region data and any path query
  143. will return empty. Await one frame to pause scripts until the NavigationServer had time to sync.