class_astargrid2d.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/AStarGrid2D.xml.
  6. .. _class_AStarGrid2D:
  7. AStarGrid2D
  8. ===========
  9. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  10. A\* (or "A-Star") pathfinding tailored to find the shortest paths on 2D grids.
  11. Description
  12. -----------
  13. Compared to :ref:`AStar2D<class_AStar2D>` you don't need to manually create points or connect them together. It also supports multiple type of heuristics and modes for diagonal movement. This class also provides a jumping mode which is faster to calculate than without it in the :ref:`AStar2D<class_AStar2D>` class.
  14. In contrast to :ref:`AStar2D<class_AStar2D>`, you only need set the :ref:`size<class_AStarGrid2D_property_size>` of the grid, optionally set the :ref:`cell_size<class_AStarGrid2D_property_cell_size>` and then call the :ref:`update<class_AStarGrid2D_method_update>` method:
  15. ::
  16. var astar_grid = AStarGrid2D.new()
  17. astar_grid.size = Vector2i(32, 32)
  18. astar_grid.cell_size = Vector2(16, 16)
  19. astar_grid.update()
  20. print(astar_grid.get_id_path(Vector2i(0, 0), Vector2i(3, 4))) # prints (0, 0), (1, 1), (2, 2), (3, 3), (3, 4)
  21. print(astar_grid.get_point_path(Vector2i(0, 0), Vector2i(3, 4))) # prints (0, 0), (16, 16), (32, 32), (48, 48), (48, 64)
  22. Properties
  23. ----------
  24. +----------------------------------------------------+------------------------------------------------------------------------+--------------------+
  25. | :ref:`Vector2<class_Vector2>` | :ref:`cell_size<class_AStarGrid2D_property_cell_size>` | ``Vector2(1, 1)`` |
  26. +----------------------------------------------------+------------------------------------------------------------------------+--------------------+
  27. | :ref:`Heuristic<enum_AStarGrid2D_Heuristic>` | :ref:`default_heuristic<class_AStarGrid2D_property_default_heuristic>` | ``0`` |
  28. +----------------------------------------------------+------------------------------------------------------------------------+--------------------+
  29. | :ref:`DiagonalMode<enum_AStarGrid2D_DiagonalMode>` | :ref:`diagonal_mode<class_AStarGrid2D_property_diagonal_mode>` | ``0`` |
  30. +----------------------------------------------------+------------------------------------------------------------------------+--------------------+
  31. | :ref:`bool<class_bool>` | :ref:`jumping_enabled<class_AStarGrid2D_property_jumping_enabled>` | ``false`` |
  32. +----------------------------------------------------+------------------------------------------------------------------------+--------------------+
  33. | :ref:`Vector2<class_Vector2>` | :ref:`offset<class_AStarGrid2D_property_offset>` | ``Vector2(0, 0)`` |
  34. +----------------------------------------------------+------------------------------------------------------------------------+--------------------+
  35. | :ref:`Vector2i<class_Vector2i>` | :ref:`size<class_AStarGrid2D_property_size>` | ``Vector2i(0, 0)`` |
  36. +----------------------------------------------------+------------------------------------------------------------------------+--------------------+
  37. Methods
  38. -------
  39. +-----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  40. | :ref:`float<class_float>` | :ref:`_compute_cost<class_AStarGrid2D_method__compute_cost>` **(** :ref:`Vector2i<class_Vector2i>` from_id, :ref:`Vector2i<class_Vector2i>` to_id **)** |virtual| |const| |
  41. +-----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  42. | :ref:`float<class_float>` | :ref:`_estimate_cost<class_AStarGrid2D_method__estimate_cost>` **(** :ref:`Vector2i<class_Vector2i>` from_id, :ref:`Vector2i<class_Vector2i>` to_id **)** |virtual| |const| |
  43. +-----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  44. | void | :ref:`clear<class_AStarGrid2D_method_clear>` **(** **)** |
  45. +-----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  46. | :ref:`PackedVector2Array<class_PackedVector2Array>` | :ref:`get_id_path<class_AStarGrid2D_method_get_id_path>` **(** :ref:`Vector2i<class_Vector2i>` from_id, :ref:`Vector2i<class_Vector2i>` to_id **)** |
  47. +-----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  48. | :ref:`PackedVector2Array<class_PackedVector2Array>` | :ref:`get_point_path<class_AStarGrid2D_method_get_point_path>` **(** :ref:`Vector2i<class_Vector2i>` from_id, :ref:`Vector2i<class_Vector2i>` to_id **)** |
  49. +-----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  50. | :ref:`bool<class_bool>` | :ref:`is_dirty<class_AStarGrid2D_method_is_dirty>` **(** **)** |const| |
  51. +-----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  52. | :ref:`bool<class_bool>` | :ref:`is_in_bounds<class_AStarGrid2D_method_is_in_bounds>` **(** :ref:`int<class_int>` x, :ref:`int<class_int>` y **)** |const| |
  53. +-----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  54. | :ref:`bool<class_bool>` | :ref:`is_in_boundsv<class_AStarGrid2D_method_is_in_boundsv>` **(** :ref:`Vector2i<class_Vector2i>` id **)** |const| |
  55. +-----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  56. | :ref:`bool<class_bool>` | :ref:`is_point_solid<class_AStarGrid2D_method_is_point_solid>` **(** :ref:`Vector2i<class_Vector2i>` id **)** |const| |
  57. +-----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  58. | void | :ref:`set_point_solid<class_AStarGrid2D_method_set_point_solid>` **(** :ref:`Vector2i<class_Vector2i>` id, :ref:`bool<class_bool>` solid=true **)** |
  59. +-----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  60. | void | :ref:`update<class_AStarGrid2D_method_update>` **(** **)** |
  61. +-----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  62. Enumerations
  63. ------------
  64. .. _enum_AStarGrid2D_Heuristic:
  65. .. _class_AStarGrid2D_constant_HEURISTIC_EUCLIDEAN:
  66. .. _class_AStarGrid2D_constant_HEURISTIC_MANHATTAN:
  67. .. _class_AStarGrid2D_constant_HEURISTIC_OCTILE:
  68. .. _class_AStarGrid2D_constant_HEURISTIC_CHEBYSHEV:
  69. .. _class_AStarGrid2D_constant_HEURISTIC_MAX:
  70. enum **Heuristic**:
  71. - **HEURISTIC_EUCLIDEAN** = **0** --- The Euclidean heuristic to be used for the pathfinding using the following formula:
  72. ::
  73. dx = abs(to_id.x - from_id.x)
  74. dy = abs(to_id.y - from_id.y)
  75. result = sqrt(dx * dx + dy * dy)
  76. - **HEURISTIC_MANHATTAN** = **1** --- The Manhattan heuristic to be used for the pathfinding using the following formula:
  77. ::
  78. dx = abs(to_id.x - from_id.x)
  79. dy = abs(to_id.y - from_id.y)
  80. result = dx + dy
  81. - **HEURISTIC_OCTILE** = **2** --- The Octile heuristic to be used for the pathfinding using the following formula:
  82. ::
  83. dx = abs(to_id.x - from_id.x)
  84. dy = abs(to_id.y - from_id.y)
  85. f = sqrt(2) - 1
  86. result = (dx < dy) ? f * dx + dy : f * dy + dx;
  87. - **HEURISTIC_CHEBYSHEV** = **3** --- The Chebyshev heuristic to be used for the pathfinding using the following formula:
  88. ::
  89. dx = abs(to_id.x - from_id.x)
  90. dy = abs(to_id.y - from_id.y)
  91. result = max(dx, dy)
  92. - **HEURISTIC_MAX** = **4** --- Represents the size of the :ref:`Heuristic<enum_AStarGrid2D_Heuristic>` enum.
  93. ----
  94. .. _enum_AStarGrid2D_DiagonalMode:
  95. .. _class_AStarGrid2D_constant_DIAGONAL_MODE_ALWAYS:
  96. .. _class_AStarGrid2D_constant_DIAGONAL_MODE_NEVER:
  97. .. _class_AStarGrid2D_constant_DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE:
  98. .. _class_AStarGrid2D_constant_DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES:
  99. .. _class_AStarGrid2D_constant_DIAGONAL_MODE_MAX:
  100. enum **DiagonalMode**:
  101. - **DIAGONAL_MODE_ALWAYS** = **0** --- The pathfinding algorithm will ignore solid neighbors around the target cell and allow passing using diagonals.
  102. - **DIAGONAL_MODE_NEVER** = **1** --- The pathfinding algorithm will ignore all diagonals and the way will be always orthogonal.
  103. - **DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE** = **2** --- The pathfinding algorithm will avoid using diagonals if at least two obstacles have been placed around the neighboring cells of the specific path segment.
  104. - **DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES** = **3** --- The pathfinding algorithm will avoid using diagonals if any obstacle has been placed around the neighboring cells of the specific path segment.
  105. - **DIAGONAL_MODE_MAX** = **4** --- Represents the size of the :ref:`DiagonalMode<enum_AStarGrid2D_DiagonalMode>` enum.
  106. Property Descriptions
  107. ---------------------
  108. .. _class_AStarGrid2D_property_cell_size:
  109. - :ref:`Vector2<class_Vector2>` **cell_size**
  110. +-----------+----------------------+
  111. | *Default* | ``Vector2(1, 1)`` |
  112. +-----------+----------------------+
  113. | *Setter* | set_cell_size(value) |
  114. +-----------+----------------------+
  115. | *Getter* | get_cell_size() |
  116. +-----------+----------------------+
  117. The size of the point cell which will be applied to calculate the resulting point position returned by :ref:`get_point_path<class_AStarGrid2D_method_get_point_path>`. If changed, :ref:`update<class_AStarGrid2D_method_update>` needs to be called before finding the next path.
  118. ----
  119. .. _class_AStarGrid2D_property_default_heuristic:
  120. - :ref:`Heuristic<enum_AStarGrid2D_Heuristic>` **default_heuristic**
  121. +-----------+------------------------------+
  122. | *Default* | ``0`` |
  123. +-----------+------------------------------+
  124. | *Setter* | set_default_heuristic(value) |
  125. +-----------+------------------------------+
  126. | *Getter* | get_default_heuristic() |
  127. +-----------+------------------------------+
  128. The default :ref:`Heuristic<enum_AStarGrid2D_Heuristic>` which will be used to calculate the path if :ref:`_compute_cost<class_AStarGrid2D_method__compute_cost>` and/or :ref:`_estimate_cost<class_AStarGrid2D_method__estimate_cost>` were not overridden.
  129. ----
  130. .. _class_AStarGrid2D_property_diagonal_mode:
  131. - :ref:`DiagonalMode<enum_AStarGrid2D_DiagonalMode>` **diagonal_mode**
  132. +-----------+--------------------------+
  133. | *Default* | ``0`` |
  134. +-----------+--------------------------+
  135. | *Setter* | set_diagonal_mode(value) |
  136. +-----------+--------------------------+
  137. | *Getter* | get_diagonal_mode() |
  138. +-----------+--------------------------+
  139. A specific :ref:`DiagonalMode<enum_AStarGrid2D_DiagonalMode>` mode which will force the path to avoid or accept the specified diagonals.
  140. ----
  141. .. _class_AStarGrid2D_property_jumping_enabled:
  142. - :ref:`bool<class_bool>` **jumping_enabled**
  143. +-----------+----------------------------+
  144. | *Default* | ``false`` |
  145. +-----------+----------------------------+
  146. | *Setter* | set_jumping_enabled(value) |
  147. +-----------+----------------------------+
  148. | *Getter* | is_jumping_enabled() |
  149. +-----------+----------------------------+
  150. Enables or disables jumping to skip up the intermediate points and speeds up the searching algorithm.
  151. ----
  152. .. _class_AStarGrid2D_property_offset:
  153. - :ref:`Vector2<class_Vector2>` **offset**
  154. +-----------+-------------------+
  155. | *Default* | ``Vector2(0, 0)`` |
  156. +-----------+-------------------+
  157. | *Setter* | set_offset(value) |
  158. +-----------+-------------------+
  159. | *Getter* | get_offset() |
  160. +-----------+-------------------+
  161. The offset of the grid which will be applied to calculate the resulting point position returned by :ref:`get_point_path<class_AStarGrid2D_method_get_point_path>`. If changed, :ref:`update<class_AStarGrid2D_method_update>` needs to be called before finding the next path.
  162. ----
  163. .. _class_AStarGrid2D_property_size:
  164. - :ref:`Vector2i<class_Vector2i>` **size**
  165. +-----------+--------------------+
  166. | *Default* | ``Vector2i(0, 0)`` |
  167. +-----------+--------------------+
  168. | *Setter* | set_size(value) |
  169. +-----------+--------------------+
  170. | *Getter* | get_size() |
  171. +-----------+--------------------+
  172. The size of the grid (number of cells of size :ref:`cell_size<class_AStarGrid2D_property_cell_size>` on each axis). If changed, :ref:`update<class_AStarGrid2D_method_update>` needs to be called before finding the next path.
  173. Method Descriptions
  174. -------------------
  175. .. _class_AStarGrid2D_method__compute_cost:
  176. - :ref:`float<class_float>` **_compute_cost** **(** :ref:`Vector2i<class_Vector2i>` from_id, :ref:`Vector2i<class_Vector2i>` to_id **)** |virtual| |const|
  177. Called when computing the cost between two connected points.
  178. Note that this function is hidden in the default ``AStarGrid2D`` class.
  179. ----
  180. .. _class_AStarGrid2D_method__estimate_cost:
  181. - :ref:`float<class_float>` **_estimate_cost** **(** :ref:`Vector2i<class_Vector2i>` from_id, :ref:`Vector2i<class_Vector2i>` to_id **)** |virtual| |const|
  182. Called when estimating the cost between a point and the path's ending point.
  183. Note that this function is hidden in the default ``AStarGrid2D`` class.
  184. ----
  185. .. _class_AStarGrid2D_method_clear:
  186. - void **clear** **(** **)**
  187. Clears the grid and sets the :ref:`size<class_AStarGrid2D_property_size>` to :ref:`Vector2i.ZERO<class_Vector2i_constant_ZERO>`.
  188. ----
  189. .. _class_AStarGrid2D_method_get_id_path:
  190. - :ref:`PackedVector2Array<class_PackedVector2Array>` **get_id_path** **(** :ref:`Vector2i<class_Vector2i>` from_id, :ref:`Vector2i<class_Vector2i>` to_id **)**
  191. Returns an array with the IDs of the points that form the path found by AStar2D between the given points. The array is ordered from the starting point to the ending point of the path.
  192. ----
  193. .. _class_AStarGrid2D_method_get_point_path:
  194. - :ref:`PackedVector2Array<class_PackedVector2Array>` **get_point_path** **(** :ref:`Vector2i<class_Vector2i>` from_id, :ref:`Vector2i<class_Vector2i>` to_id **)**
  195. Returns an array with the points that are in the path found by AStarGrid2D between the given points. The array is ordered from the starting point to the ending point of the path.
  196. \ **Note:** This method is not thread-safe. If called from a :ref:`Thread<class_Thread>`, it will return an empty :ref:`PackedVector3Array<class_PackedVector3Array>` and will print an error message.
  197. ----
  198. .. _class_AStarGrid2D_method_is_dirty:
  199. - :ref:`bool<class_bool>` **is_dirty** **(** **)** |const|
  200. Indicates that the grid parameters were changed and :ref:`update<class_AStarGrid2D_method_update>` needs to be called.
  201. ----
  202. .. _class_AStarGrid2D_method_is_in_bounds:
  203. - :ref:`bool<class_bool>` **is_in_bounds** **(** :ref:`int<class_int>` x, :ref:`int<class_int>` y **)** |const|
  204. Returns ``true`` if the ``x`` and ``y`` is a valid grid coordinate (id).
  205. ----
  206. .. _class_AStarGrid2D_method_is_in_boundsv:
  207. - :ref:`bool<class_bool>` **is_in_boundsv** **(** :ref:`Vector2i<class_Vector2i>` id **)** |const|
  208. Returns ``true`` if the ``id`` vector is a valid grid coordinate.
  209. ----
  210. .. _class_AStarGrid2D_method_is_point_solid:
  211. - :ref:`bool<class_bool>` **is_point_solid** **(** :ref:`Vector2i<class_Vector2i>` id **)** |const|
  212. Returns ``true`` if a point is disabled for pathfinding. By default, all points are enabled.
  213. ----
  214. .. _class_AStarGrid2D_method_set_point_solid:
  215. - void **set_point_solid** **(** :ref:`Vector2i<class_Vector2i>` id, :ref:`bool<class_bool>` solid=true **)**
  216. Disables or enables the specified point for pathfinding. Useful for making an obstacle. By default, all points are enabled.
  217. ----
  218. .. _class_AStarGrid2D_method_update:
  219. - void **update** **(** **)**
  220. Updates the internal state of the grid according to the parameters to prepare it to search the path. Needs to be called if parameters like :ref:`size<class_AStarGrid2D_property_size>`, :ref:`cell_size<class_AStarGrid2D_property_cell_size>` or :ref:`offset<class_AStarGrid2D_property_offset>` are changed. :ref:`is_dirty<class_AStarGrid2D_method_is_dirty>` will return ``true`` if this is the case and this needs to be called.
  221. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  222. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  223. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  224. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  225. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  226. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`