navigation_using_navigationlayers.rst 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. .. _doc_navigation_advanced_using_navigationlayers:
  2. Using NavigationLayers
  3. ======================
  4. NavigationLayers are an optional feature to further control which navigation meshes are considered in a path query.
  5. They work similar to how physics layers control collision between collision objects or how visual layers control what is rendered to the Viewport.
  6. NavigationLayers can be named in the **ProjectSettings** the same as physics layers or visual layers.
  7. .. image:: img/navigationlayers_naming.png
  8. If a region has not a single compatible navigation layer with the ``navigation_layers`` parameter of a path query this regions navigation mesh will be skipped in pathfinding.
  9. See :ref:`doc_navigation_using_navigationpaths` for more information on querying the NavigationServer for paths.
  10. NavigationLayers are a single ``int`` value that is used as a **bitmask**.
  11. Many navigation related nodes have ``set_navigation_layer_value()`` and
  12. ``get_navigation_layer_value()`` functions to set and get a layer number directly
  13. without the need for more complex bitwise operations.
  14. In scripts the following helper functions can be used to work with the ``navigation_layers`` bitmask.
  15. .. tabs::
  16. .. code-tab:: gdscript 2D GDScript
  17. func change_layers():
  18. var region: NavigationRegion2D = get_node("NavigationRegion2D")
  19. # enables 4-th layer for this region
  20. region.navigation_layers = enable_bitmask_inx(region.navigation_layers, 4)
  21. # disables 1-rst layer for this region
  22. region.navigation_layers = disable_bitmask_inx(region.navigation_layers, 1)
  23. var agent: NavigationAgent2D = get_node("NavigationAgent2D")
  24. # make future path queries of this agent ignore regions with 4-th layer
  25. agent.navigation_layers = disable_bitmask_inx(agent.navigation_layers, 4)
  26. var path_query_navigation_layers: int = 0
  27. path_query_navigation_layers = enable_bitmask_inx(path_query_navigation_layers, 2)
  28. # get a path that only considers 2-nd layer regions
  29. var path: PackedVector2Array = NavigationServer2D.map_get_path(
  30. map,
  31. start_position,
  32. target_position,
  33. true,
  34. path_query_navigation_layers
  35. )
  36. static func is_bitmask_inx_enabled(_bitmask: int, _index: int) -> bool:
  37. return _bitmask & (1 << _index) != 0
  38. static func enable_bitmask_inx(_bitmask: int, _index: int) -> int:
  39. return _bitmask | (1 << _index)
  40. static func disable_bitmask_inx(_bitmask: int, _index: int) -> int:
  41. return _bitmask & ~(1 << _index)
  42. .. code-tab:: csharp 2D C#
  43. using Godot;
  44. public partial class MyNode2D : Node2D
  45. {
  46. private Rid _map;
  47. private Vector2 _startPosition;
  48. private Vector2 _targetPosition;
  49. private void ChangeLayers()
  50. {
  51. NavigationRegion2D region = GetNode<NavigationRegion2D>("NavigationRegion2D");
  52. // Enables the 4th layer for this region.
  53. region.NavigationLayers = EnableBitmaskInx(region.NavigationLayers, 4);
  54. // Disables the 1st layer for this region.
  55. region.NavigationLayers = DisableBitmaskInx(region.NavigationLayers, 1);
  56. NavigationAgent2D agent = GetNode<NavigationAgent2D>("NavigationAgent2D");
  57. // Make future path queries of this agent ignore regions with the 4th layer.
  58. agent.NavigationLayers = DisableBitmaskInx(agent.NavigationLayers, 4);
  59. uint pathQueryNavigationLayers = 0;
  60. pathQueryNavigationLayers = EnableBitmaskInx(pathQueryNavigationLayers, 2);
  61. // Get a path that only considers 2nd layer regions.
  62. Vector2[] path = NavigationServer2D.MapGetPath(
  63. _map,
  64. _startPosition,
  65. _targetPosition,
  66. true,
  67. pathQueryNavigationLayers
  68. );
  69. }
  70. private static bool IsBitmaskInxEnabled(uint bitmask, int index)
  71. {
  72. return (bitmask & (1 << index)) != 0;
  73. }
  74. private static uint EnableBitmaskInx(uint bitmask, int index)
  75. {
  76. return bitmask | (1u << index);
  77. }
  78. private static uint DisableBitmaskInx(uint bitmask, int index)
  79. {
  80. return bitmask & ~(1u << index);
  81. }
  82. }
  83. .. code-tab:: gdscript 3D GDScript
  84. func change_layers():
  85. var region: NavigationRegion3D = get_node("NavigationRegion3D")
  86. # enables 4-th layer for this region
  87. region.navigation_layers = enable_bitmask_inx(region.navigation_layers, 4)
  88. # disables 1-rst layer for this region
  89. region.navigation_layers = disable_bitmask_inx(region.navigation_layers, 1)
  90. var agent: NavigationAgent3D = get_node("NavigationAgent3D")
  91. # make future path queries of this agent ignore regions with 4-th layer
  92. agent.navigation_layers = disable_bitmask_inx(agent.navigation_layers, 4)
  93. var path_query_navigation_layers: int = 0
  94. path_query_navigation_layers = enable_bitmask_inx(path_query_navigation_layers, 2)
  95. # get a path that only considers 2-nd layer regions
  96. var path: PackedVector3Array = NavigationServer3D.map_get_path(
  97. map,
  98. start_position,
  99. target_position,
  100. true,
  101. path_query_navigation_layers
  102. )
  103. static func is_bitmask_inx_enabled(_bitmask: int, _index: int) -> bool:
  104. return _bitmask & (1 << _index) != 0
  105. static func enable_bitmask_inx(_bitmask: int, _index: int) -> int:
  106. return _bitmask | (1 << _index)
  107. static func disable_bitmask_inx(_bitmask: int, _index: int) -> int:
  108. return _bitmask & ~(1 << _index)
  109. .. code-tab:: csharp 3D C#
  110. using Godot;
  111. public partial class MyNode3D : Node3D
  112. {
  113. private Rid _map;
  114. private Vector3 _startPosition;
  115. private Vector3 _targetPosition;
  116. private void ChangeLayers()
  117. {
  118. NavigationRegion3D region = GetNode<NavigationRegion3D>("NavigationRegion3D");
  119. // Enables the 4th layer for this region.
  120. region.NavigationLayers = EnableBitmaskInx(region.NavigationLayers, 4);
  121. // Disables the 1st layer for this region.
  122. region.NavigationLayers = DisableBitmaskInx(region.NavigationLayers, 1);
  123. NavigationAgent3D agent = GetNode<NavigationAgent3D>("NavigationAgent2D");
  124. // Make future path queries of this agent ignore regions with the 4th layer.
  125. agent.NavigationLayers = DisableBitmaskInx(agent.NavigationLayers, 4);
  126. uint pathQueryNavigationLayers = 0;
  127. pathQueryNavigationLayers = EnableBitmaskInx(pathQueryNavigationLayers, 2);
  128. // Get a path that only considers 2nd layer regions.
  129. Vector3[] path = NavigationServer3D.MapGetPath(
  130. _map,
  131. _startPosition,
  132. _targetPosition,
  133. true,
  134. pathQueryNavigationLayers
  135. );
  136. }
  137. private static bool IsBitmaskInxEnabled(uint bitmask, int index)
  138. {
  139. return (bitmask & (1 << index)) != 0;
  140. }
  141. private static uint EnableBitmaskInx(uint bitmask, int index)
  142. {
  143. return bitmask | (1u << index);
  144. }
  145. private static uint DisableBitmaskInx(uint bitmask, int index)
  146. {
  147. return bitmask & ~(1u << index);
  148. }
  149. }
  150. Changing navigation layers for path queries is a performance friendly alternative to
  151. enabling / disabling entire navigation regions. Compared to region changes a
  152. navigation path query with different navigation layers does not
  153. trigger large scale updates on the NavigationServer.
  154. Changing the navigation layers of NavigationAgent nodes will have an immediate
  155. effect on the next path query. Changing the navigation layers of
  156. regions will have an effect after the next NavigationServer sync.