class_astargrid2d.rst 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. 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.
  15. 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:
  16. .. tabs::
  17. .. code-tab:: gdscript
  18. var astar_grid = AStarGrid2D.new()
  19. astar_grid.size = Vector2i(32, 32)
  20. astar_grid.cell_size = Vector2(16, 16)
  21. astar_grid.update()
  22. print(astar_grid.get_id_path(Vector2i(0, 0), Vector2i(3, 4))) # prints (0, 0), (1, 1), (2, 2), (3, 3), (3, 4)
  23. print(astar_grid.get_point_path(Vector2i(0, 0), Vector2i(3, 4))) # prints (0, 0), (16, 16), (32, 32), (48, 48), (48, 64)
  24. .. code-tab:: csharp
  25. AStarGrid2D astarGrid = new AStarGrid2D();
  26. astarGrid.Size = new Vector2i(32, 32);
  27. astarGrid.CellSize = new Vector2i(16, 16);
  28. astarGrid.Update();
  29. GD.Print(astarGrid.GetIdPath(Vector2i.Zero, new Vector2i(3, 4))); // prints (0, 0), (1, 1), (2, 2), (3, 3), (3, 4)
  30. GD.Print(astarGrid.GetPointPath(Vector2i.Zero, new Vector2i(3, 4))); // prints (0, 0), (16, 16), (32, 32), (48, 48), (48, 64)
  31. .. rst-class:: classref-reftable-group
  32. Properties
  33. ----------
  34. .. table::
  35. :widths: auto
  36. +----------------------------------------------------+------------------------------------------------------------------------+--------------------+
  37. | :ref:`Vector2<class_Vector2>` | :ref:`cell_size<class_AStarGrid2D_property_cell_size>` | ``Vector2(1, 1)`` |
  38. +----------------------------------------------------+------------------------------------------------------------------------+--------------------+
  39. | :ref:`Heuristic<enum_AStarGrid2D_Heuristic>` | :ref:`default_heuristic<class_AStarGrid2D_property_default_heuristic>` | ``0`` |
  40. +----------------------------------------------------+------------------------------------------------------------------------+--------------------+
  41. | :ref:`DiagonalMode<enum_AStarGrid2D_DiagonalMode>` | :ref:`diagonal_mode<class_AStarGrid2D_property_diagonal_mode>` | ``0`` |
  42. +----------------------------------------------------+------------------------------------------------------------------------+--------------------+
  43. | :ref:`bool<class_bool>` | :ref:`jumping_enabled<class_AStarGrid2D_property_jumping_enabled>` | ``false`` |
  44. +----------------------------------------------------+------------------------------------------------------------------------+--------------------+
  45. | :ref:`Vector2<class_Vector2>` | :ref:`offset<class_AStarGrid2D_property_offset>` | ``Vector2(0, 0)`` |
  46. +----------------------------------------------------+------------------------------------------------------------------------+--------------------+
  47. | :ref:`Vector2i<class_Vector2i>` | :ref:`size<class_AStarGrid2D_property_size>` | ``Vector2i(0, 0)`` |
  48. +----------------------------------------------------+------------------------------------------------------------------------+--------------------+
  49. .. rst-class:: classref-reftable-group
  50. Methods
  51. -------
  52. .. table::
  53. :widths: auto
  54. +-----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  55. | :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| |
  56. +-----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  57. | :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| |
  58. +-----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  59. | void | :ref:`clear<class_AStarGrid2D_method_clear>` **(** **)** |
  60. +-----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  61. | :ref:`Vector2i[]<class_Vector2i>` | :ref:`get_id_path<class_AStarGrid2D_method_get_id_path>` **(** :ref:`Vector2i<class_Vector2i>` from_id, :ref:`Vector2i<class_Vector2i>` to_id **)** |
  62. +-----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  63. | :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 **)** |
  64. +-----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  65. | :ref:`bool<class_bool>` | :ref:`is_dirty<class_AStarGrid2D_method_is_dirty>` **(** **)** |const| |
  66. +-----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  67. | :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| |
  68. +-----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  69. | :ref:`bool<class_bool>` | :ref:`is_in_boundsv<class_AStarGrid2D_method_is_in_boundsv>` **(** :ref:`Vector2i<class_Vector2i>` id **)** |const| |
  70. +-----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  71. | :ref:`bool<class_bool>` | :ref:`is_point_solid<class_AStarGrid2D_method_is_point_solid>` **(** :ref:`Vector2i<class_Vector2i>` id **)** |const| |
  72. +-----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  73. | void | :ref:`set_point_solid<class_AStarGrid2D_method_set_point_solid>` **(** :ref:`Vector2i<class_Vector2i>` id, :ref:`bool<class_bool>` solid=true **)** |
  74. +-----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  75. | void | :ref:`update<class_AStarGrid2D_method_update>` **(** **)** |
  76. +-----------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  77. .. rst-class:: classref-section-separator
  78. ----
  79. .. rst-class:: classref-descriptions-group
  80. Enumerations
  81. ------------
  82. .. _enum_AStarGrid2D_Heuristic:
  83. .. rst-class:: classref-enumeration
  84. enum **Heuristic**:
  85. .. _class_AStarGrid2D_constant_HEURISTIC_EUCLIDEAN:
  86. .. rst-class:: classref-enumeration-constant
  87. :ref:`Heuristic<enum_AStarGrid2D_Heuristic>` **HEURISTIC_EUCLIDEAN** = ``0``
  88. The Euclidean heuristic to be used for the pathfinding using the following formula:
  89. ::
  90. dx = abs(to_id.x - from_id.x)
  91. dy = abs(to_id.y - from_id.y)
  92. result = sqrt(dx * dx + dy * dy)
  93. .. _class_AStarGrid2D_constant_HEURISTIC_MANHATTAN:
  94. .. rst-class:: classref-enumeration-constant
  95. :ref:`Heuristic<enum_AStarGrid2D_Heuristic>` **HEURISTIC_MANHATTAN** = ``1``
  96. The Manhattan heuristic to be used for the pathfinding using the following formula:
  97. ::
  98. dx = abs(to_id.x - from_id.x)
  99. dy = abs(to_id.y - from_id.y)
  100. result = dx + dy
  101. .. _class_AStarGrid2D_constant_HEURISTIC_OCTILE:
  102. .. rst-class:: classref-enumeration-constant
  103. :ref:`Heuristic<enum_AStarGrid2D_Heuristic>` **HEURISTIC_OCTILE** = ``2``
  104. The Octile heuristic to be used for the pathfinding using the following formula:
  105. ::
  106. dx = abs(to_id.x - from_id.x)
  107. dy = abs(to_id.y - from_id.y)
  108. f = sqrt(2) - 1
  109. result = (dx < dy) ? f * dx + dy : f * dy + dx;
  110. .. _class_AStarGrid2D_constant_HEURISTIC_CHEBYSHEV:
  111. .. rst-class:: classref-enumeration-constant
  112. :ref:`Heuristic<enum_AStarGrid2D_Heuristic>` **HEURISTIC_CHEBYSHEV** = ``3``
  113. The Chebyshev heuristic to be used for the pathfinding using the following formula:
  114. ::
  115. dx = abs(to_id.x - from_id.x)
  116. dy = abs(to_id.y - from_id.y)
  117. result = max(dx, dy)
  118. .. _class_AStarGrid2D_constant_HEURISTIC_MAX:
  119. .. rst-class:: classref-enumeration-constant
  120. :ref:`Heuristic<enum_AStarGrid2D_Heuristic>` **HEURISTIC_MAX** = ``4``
  121. Represents the size of the :ref:`Heuristic<enum_AStarGrid2D_Heuristic>` enum.
  122. .. rst-class:: classref-item-separator
  123. ----
  124. .. _enum_AStarGrid2D_DiagonalMode:
  125. .. rst-class:: classref-enumeration
  126. enum **DiagonalMode**:
  127. .. _class_AStarGrid2D_constant_DIAGONAL_MODE_ALWAYS:
  128. .. rst-class:: classref-enumeration-constant
  129. :ref:`DiagonalMode<enum_AStarGrid2D_DiagonalMode>` **DIAGONAL_MODE_ALWAYS** = ``0``
  130. The pathfinding algorithm will ignore solid neighbors around the target cell and allow passing using diagonals.
  131. .. _class_AStarGrid2D_constant_DIAGONAL_MODE_NEVER:
  132. .. rst-class:: classref-enumeration-constant
  133. :ref:`DiagonalMode<enum_AStarGrid2D_DiagonalMode>` **DIAGONAL_MODE_NEVER** = ``1``
  134. The pathfinding algorithm will ignore all diagonals and the way will be always orthogonal.
  135. .. _class_AStarGrid2D_constant_DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE:
  136. .. rst-class:: classref-enumeration-constant
  137. :ref:`DiagonalMode<enum_AStarGrid2D_DiagonalMode>` **DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE** = ``2``
  138. The pathfinding algorithm will avoid using diagonals if at least two obstacles have been placed around the neighboring cells of the specific path segment.
  139. .. _class_AStarGrid2D_constant_DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES:
  140. .. rst-class:: classref-enumeration-constant
  141. :ref:`DiagonalMode<enum_AStarGrid2D_DiagonalMode>` **DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES** = ``3``
  142. The pathfinding algorithm will avoid using diagonals if any obstacle has been placed around the neighboring cells of the specific path segment.
  143. .. _class_AStarGrid2D_constant_DIAGONAL_MODE_MAX:
  144. .. rst-class:: classref-enumeration-constant
  145. :ref:`DiagonalMode<enum_AStarGrid2D_DiagonalMode>` **DIAGONAL_MODE_MAX** = ``4``
  146. Represents the size of the :ref:`DiagonalMode<enum_AStarGrid2D_DiagonalMode>` enum.
  147. .. rst-class:: classref-section-separator
  148. ----
  149. .. rst-class:: classref-descriptions-group
  150. Property Descriptions
  151. ---------------------
  152. .. _class_AStarGrid2D_property_cell_size:
  153. .. rst-class:: classref-property
  154. :ref:`Vector2<class_Vector2>` **cell_size** = ``Vector2(1, 1)``
  155. .. rst-class:: classref-property-setget
  156. - void **set_cell_size** **(** :ref:`Vector2<class_Vector2>` value **)**
  157. - :ref:`Vector2<class_Vector2>` **get_cell_size** **(** **)**
  158. 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.
  159. .. rst-class:: classref-item-separator
  160. ----
  161. .. _class_AStarGrid2D_property_default_heuristic:
  162. .. rst-class:: classref-property
  163. :ref:`Heuristic<enum_AStarGrid2D_Heuristic>` **default_heuristic** = ``0``
  164. .. rst-class:: classref-property-setget
  165. - void **set_default_heuristic** **(** :ref:`Heuristic<enum_AStarGrid2D_Heuristic>` value **)**
  166. - :ref:`Heuristic<enum_AStarGrid2D_Heuristic>` **get_default_heuristic** **(** **)**
  167. 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.
  168. .. rst-class:: classref-item-separator
  169. ----
  170. .. _class_AStarGrid2D_property_diagonal_mode:
  171. .. rst-class:: classref-property
  172. :ref:`DiagonalMode<enum_AStarGrid2D_DiagonalMode>` **diagonal_mode** = ``0``
  173. .. rst-class:: classref-property-setget
  174. - void **set_diagonal_mode** **(** :ref:`DiagonalMode<enum_AStarGrid2D_DiagonalMode>` value **)**
  175. - :ref:`DiagonalMode<enum_AStarGrid2D_DiagonalMode>` **get_diagonal_mode** **(** **)**
  176. A specific :ref:`DiagonalMode<enum_AStarGrid2D_DiagonalMode>` mode which will force the path to avoid or accept the specified diagonals.
  177. .. rst-class:: classref-item-separator
  178. ----
  179. .. _class_AStarGrid2D_property_jumping_enabled:
  180. .. rst-class:: classref-property
  181. :ref:`bool<class_bool>` **jumping_enabled** = ``false``
  182. .. rst-class:: classref-property-setget
  183. - void **set_jumping_enabled** **(** :ref:`bool<class_bool>` value **)**
  184. - :ref:`bool<class_bool>` **is_jumping_enabled** **(** **)**
  185. Enables or disables jumping to skip up the intermediate points and speeds up the searching algorithm.
  186. .. rst-class:: classref-item-separator
  187. ----
  188. .. _class_AStarGrid2D_property_offset:
  189. .. rst-class:: classref-property
  190. :ref:`Vector2<class_Vector2>` **offset** = ``Vector2(0, 0)``
  191. .. rst-class:: classref-property-setget
  192. - void **set_offset** **(** :ref:`Vector2<class_Vector2>` value **)**
  193. - :ref:`Vector2<class_Vector2>` **get_offset** **(** **)**
  194. 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.
  195. .. rst-class:: classref-item-separator
  196. ----
  197. .. _class_AStarGrid2D_property_size:
  198. .. rst-class:: classref-property
  199. :ref:`Vector2i<class_Vector2i>` **size** = ``Vector2i(0, 0)``
  200. .. rst-class:: classref-property-setget
  201. - void **set_size** **(** :ref:`Vector2i<class_Vector2i>` value **)**
  202. - :ref:`Vector2i<class_Vector2i>` **get_size** **(** **)**
  203. 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.
  204. .. rst-class:: classref-section-separator
  205. ----
  206. .. rst-class:: classref-descriptions-group
  207. Method Descriptions
  208. -------------------
  209. .. _class_AStarGrid2D_method__compute_cost:
  210. .. rst-class:: classref-method
  211. :ref:`float<class_float>` **_compute_cost** **(** :ref:`Vector2i<class_Vector2i>` from_id, :ref:`Vector2i<class_Vector2i>` to_id **)** |virtual| |const|
  212. Called when computing the cost between two connected points.
  213. Note that this function is hidden in the default ``AStarGrid2D`` class.
  214. .. rst-class:: classref-item-separator
  215. ----
  216. .. _class_AStarGrid2D_method__estimate_cost:
  217. .. rst-class:: classref-method
  218. :ref:`float<class_float>` **_estimate_cost** **(** :ref:`Vector2i<class_Vector2i>` from_id, :ref:`Vector2i<class_Vector2i>` to_id **)** |virtual| |const|
  219. Called when estimating the cost between a point and the path's ending point.
  220. Note that this function is hidden in the default ``AStarGrid2D`` class.
  221. .. rst-class:: classref-item-separator
  222. ----
  223. .. _class_AStarGrid2D_method_clear:
  224. .. rst-class:: classref-method
  225. void **clear** **(** **)**
  226. Clears the grid and sets the :ref:`size<class_AStarGrid2D_property_size>` to :ref:`Vector2i.ZERO<class_Vector2i_constant_ZERO>`.
  227. .. rst-class:: classref-item-separator
  228. ----
  229. .. _class_AStarGrid2D_method_get_id_path:
  230. .. rst-class:: classref-method
  231. :ref:`Vector2i[]<class_Vector2i>` **get_id_path** **(** :ref:`Vector2i<class_Vector2i>` from_id, :ref:`Vector2i<class_Vector2i>` to_id **)**
  232. 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.
  233. .. rst-class:: classref-item-separator
  234. ----
  235. .. _class_AStarGrid2D_method_get_point_path:
  236. .. rst-class:: classref-method
  237. :ref:`PackedVector2Array<class_PackedVector2Array>` **get_point_path** **(** :ref:`Vector2i<class_Vector2i>` from_id, :ref:`Vector2i<class_Vector2i>` to_id **)**
  238. 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.
  239. \ **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.
  240. .. rst-class:: classref-item-separator
  241. ----
  242. .. _class_AStarGrid2D_method_is_dirty:
  243. .. rst-class:: classref-method
  244. :ref:`bool<class_bool>` **is_dirty** **(** **)** |const|
  245. Indicates that the grid parameters were changed and :ref:`update<class_AStarGrid2D_method_update>` needs to be called.
  246. .. rst-class:: classref-item-separator
  247. ----
  248. .. _class_AStarGrid2D_method_is_in_bounds:
  249. .. rst-class:: classref-method
  250. :ref:`bool<class_bool>` **is_in_bounds** **(** :ref:`int<class_int>` x, :ref:`int<class_int>` y **)** |const|
  251. Returns ``true`` if the ``x`` and ``y`` is a valid grid coordinate (id).
  252. .. rst-class:: classref-item-separator
  253. ----
  254. .. _class_AStarGrid2D_method_is_in_boundsv:
  255. .. rst-class:: classref-method
  256. :ref:`bool<class_bool>` **is_in_boundsv** **(** :ref:`Vector2i<class_Vector2i>` id **)** |const|
  257. Returns ``true`` if the ``id`` vector is a valid grid coordinate.
  258. .. rst-class:: classref-item-separator
  259. ----
  260. .. _class_AStarGrid2D_method_is_point_solid:
  261. .. rst-class:: classref-method
  262. :ref:`bool<class_bool>` **is_point_solid** **(** :ref:`Vector2i<class_Vector2i>` id **)** |const|
  263. Returns ``true`` if a point is disabled for pathfinding. By default, all points are enabled.
  264. .. rst-class:: classref-item-separator
  265. ----
  266. .. _class_AStarGrid2D_method_set_point_solid:
  267. .. rst-class:: classref-method
  268. void **set_point_solid** **(** :ref:`Vector2i<class_Vector2i>` id, :ref:`bool<class_bool>` solid=true **)**
  269. Disables or enables the specified point for pathfinding. Useful for making an obstacle. By default, all points are enabled.
  270. .. rst-class:: classref-item-separator
  271. ----
  272. .. _class_AStarGrid2D_method_update:
  273. .. rst-class:: classref-method
  274. void **update** **(** **)**
  275. 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.
  276. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  277. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  278. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  279. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  280. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  281. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`