2022-07-04 20:42:47 +02:00
<?xml version="1.0" encoding="UTF-8" ?>
<class name= "AStarGrid2D" inherits= "RefCounted" version= "4.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "../class.xsd" >
<brief_description >
2022-09-07 10:42:19 +02:00
A* (or "A-Star") pathfinding tailored to find the shortest paths on 2D grids.
2022-07-04 20:42:47 +02:00
</brief_description>
<description >
2022-09-07 10:42:19 +02:00
Compared to [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 [AStar2D] class.
In contrast to [AStar2D], you only need set the [member size] of the grid, optionally set the [member cell_size] and then call the [method update] method:
[codeblock]
var astar_grid = AStarGrid2D.new()
astar_grid.size = Vector2i(32, 32)
astar_grid.cell_size = Vector2(16, 16)
astar_grid.update()
print(astar_grid.get_id_path(Vector2i(0, 0), Vector2i(3, 4))) # prints (0, 0), (1, 1), (2, 2), (3, 3), (3, 4)
print(astar_grid.get_point_path(Vector2i(0, 0), Vector2i(3, 4))) # prints (0, 0), (16, 16), (32, 32), (48, 48), (48, 64)
[/codeblock]
2022-07-04 20:42:47 +02:00
</description>
<tutorials >
</tutorials>
<methods >
<method name= "_compute_cost" qualifiers= "virtual const" >
<return type= "float" />
<param index= "0" name= "from_id" type= "Vector2i" />
<param index= "1" name= "to_id" type= "Vector2i" />
<description >
2022-09-07 10:42:19 +02:00
Called when computing the cost between two connected points.
Note that this function is hidden in the default [code]AStarGrid2D[/code] class.
2022-07-04 20:42:47 +02:00
</description>
</method>
<method name= "_estimate_cost" qualifiers= "virtual const" >
<return type= "float" />
<param index= "0" name= "from_id" type= "Vector2i" />
<param index= "1" name= "to_id" type= "Vector2i" />
<description >
2022-09-07 10:42:19 +02:00
Called when estimating the cost between a point and the path's ending point.
Note that this function is hidden in the default [code]AStarGrid2D[/code] class.
2022-07-04 20:42:47 +02:00
</description>
</method>
<method name= "clear" >
<return type= "void" />
<description >
2022-09-07 10:42:19 +02:00
Clears the grid and sets the [member size] to [constant Vector2i.ZERO].
2022-07-04 20:42:47 +02:00
</description>
</method>
<method name= "get_id_path" >
<return type= "PackedVector2Array" />
<param index= "0" name= "from_id" type= "Vector2i" />
<param index= "1" name= "to_id" type= "Vector2i" />
<description >
2022-09-07 10:42:19 +02:00
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.
2022-07-04 20:42:47 +02:00
</description>
</method>
<method name= "get_point_path" >
<return type= "PackedVector2Array" />
<param index= "0" name= "from_id" type= "Vector2i" />
<param index= "1" name= "to_id" type= "Vector2i" />
<description >
2022-09-07 10:42:19 +02:00
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.
[b]Note:[/b] This method is not thread-safe. If called from a [Thread], it will return an empty [PackedVector3Array] and will print an error message.
2022-07-04 20:42:47 +02:00
</description>
</method>
<method name= "is_dirty" qualifiers= "const" >
<return type= "bool" />
<description >
2022-09-07 10:42:19 +02:00
Indicates that the grid parameters were changed and [method update] needs to be called.
2022-07-04 20:42:47 +02:00
</description>
</method>
<method name= "is_in_bounds" qualifiers= "const" >
<return type= "bool" />
<param index= "0" name= "x" type= "int" />
<param index= "1" name= "y" type= "int" />
<description >
2022-09-07 10:42:19 +02:00
Returns [code]true[/code] if the [param x] and [param y] is a valid grid coordinate (id).
2022-07-04 20:42:47 +02:00
</description>
</method>
<method name= "is_in_boundsv" qualifiers= "const" >
<return type= "bool" />
<param index= "0" name= "id" type= "Vector2i" />
<description >
2022-09-07 10:42:19 +02:00
Returns [code]true[/code] if the [param id] vector is a valid grid coordinate.
2022-07-04 20:42:47 +02:00
</description>
</method>
<method name= "is_point_solid" qualifiers= "const" >
<return type= "bool" />
<param index= "0" name= "id" type= "Vector2i" />
<description >
2022-09-07 10:42:19 +02:00
Returns [code]true[/code] if a point is disabled for pathfinding. By default, all points are enabled.
2022-07-04 20:42:47 +02:00
</description>
</method>
<method name= "set_point_solid" >
<return type= "void" />
<param index= "0" name= "id" type= "Vector2i" />
<param index= "1" name= "solid" type= "bool" default= "true" />
<description >
2022-09-07 10:42:19 +02:00
Disables or enables the specified point for pathfinding. Useful for making an obstacle. By default, all points are enabled.
2022-07-04 20:42:47 +02:00
</description>
</method>
<method name= "update" >
<return type= "void" />
<description >
2022-09-07 10:42:19 +02:00
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 [member size], [member cell_size] or [member offset] are changed. [method is_dirty] will return [code]true[/code] if this is the case and this needs to be called.
2022-07-04 20:42:47 +02:00
</description>
</method>
</methods>
<members >
<member name= "cell_size" type= "Vector2" setter= "set_cell_size" getter= "get_cell_size" default= "Vector2(1, 1)" >
2022-09-07 10:42:19 +02:00
The size of the point cell which will be applied to calculate the resulting point position returned by [method get_point_path]. If changed, [method update] needs to be called before finding the next path.
2022-07-04 20:42:47 +02:00
</member>
<member name= "default_heuristic" type= "int" setter= "set_default_heuristic" getter= "get_default_heuristic" enum= "AStarGrid2D.Heuristic" default= "0" >
2022-09-07 10:42:19 +02:00
The default [enum Heuristic] which will be used to calculate the path if [method _compute_cost] and/or [method _estimate_cost] were not overridden.
2022-07-04 20:42:47 +02:00
</member>
<member name= "diagonal_mode" type= "int" setter= "set_diagonal_mode" getter= "get_diagonal_mode" enum= "AStarGrid2D.DiagonalMode" default= "0" >
2022-09-07 10:42:19 +02:00
A specific [enum DiagonalMode] mode which will force the path to avoid or accept the specified diagonals.
2022-07-04 20:42:47 +02:00
</member>
<member name= "jumping_enabled" type= "bool" setter= "set_jumping_enabled" getter= "is_jumping_enabled" default= "false" >
2022-09-07 10:42:19 +02:00
Enables or disables jumping to skip up the intermediate points and speeds up the searching algorithm.
2022-07-04 20:42:47 +02:00
</member>
<member name= "offset" type= "Vector2" setter= "set_offset" getter= "get_offset" default= "Vector2(0, 0)" >
2022-09-07 10:42:19 +02:00
The offset of the grid which will be applied to calculate the resulting point position returned by [method get_point_path]. If changed, [method update] needs to be called before finding the next path.
2022-07-04 20:42:47 +02:00
</member>
<member name= "size" type= "Vector2i" setter= "set_size" getter= "get_size" default= "Vector2i(0, 0)" >
2022-09-07 10:42:19 +02:00
The size of the grid (number of cells of size [member cell_size] on each axis). If changed, [method update] needs to be called before finding the next path.
2022-07-04 20:42:47 +02:00
</member>
</members>
<constants >
<constant name= "HEURISTIC_EUCLIDEAN" value= "0" enum= "Heuristic" >
2022-09-07 10:42:19 +02:00
The Euclidean heuristic to be used for the pathfinding using the following formula:
[codeblock]
dx = abs(to_id.x - from_id.x)
dy = abs(to_id.y - from_id.y)
result = sqrt(dx * dx + dy * dy)
[/codeblock]
2022-07-04 20:42:47 +02:00
</constant>
<constant name= "HEURISTIC_MANHATTAN" value= "1" enum= "Heuristic" >
2022-09-07 10:42:19 +02:00
The Manhattan heuristic to be used for the pathfinding using the following formula:
[codeblock]
dx = abs(to_id.x - from_id.x)
dy = abs(to_id.y - from_id.y)
result = dx + dy
[/codeblock]
2022-07-04 20:42:47 +02:00
</constant>
<constant name= "HEURISTIC_OCTILE" value= "2" enum= "Heuristic" >
2022-09-07 10:42:19 +02:00
The Octile heuristic to be used for the pathfinding using the following formula:
[codeblock]
dx = abs(to_id.x - from_id.x)
dy = abs(to_id.y - from_id.y)
f = sqrt(2) - 1
result = (dx < dy) ? f * dx + dy : f * dy + dx;
[/codeblock]
2022-07-04 20:42:47 +02:00
</constant>
<constant name= "HEURISTIC_CHEBYSHEV" value= "3" enum= "Heuristic" >
2022-09-07 10:42:19 +02:00
The Chebyshev heuristic to be used for the pathfinding using the following formula:
[codeblock]
dx = abs(to_id.x - from_id.x)
dy = abs(to_id.y - from_id.y)
result = max(dx, dy)
[/codeblock]
2022-07-04 20:42:47 +02:00
</constant>
<constant name= "HEURISTIC_MAX" value= "4" enum= "Heuristic" >
2022-09-07 10:42:19 +02:00
Represents the size of the [enum Heuristic] enum.
2022-07-04 20:42:47 +02:00
</constant>
<constant name= "DIAGONAL_MODE_ALWAYS" value= "0" enum= "DiagonalMode" >
2022-09-07 10:42:19 +02:00
The pathfinding algorithm will ignore solid neighbors around the target cell and allow passing using diagonals.
2022-07-04 20:42:47 +02:00
</constant>
<constant name= "DIAGONAL_MODE_NEVER" value= "1" enum= "DiagonalMode" >
2022-09-07 10:42:19 +02:00
The pathfinding algorithm will ignore all diagonals and the way will be always orthogonal.
2022-07-04 20:42:47 +02:00
</constant>
<constant name= "DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE" value= "2" enum= "DiagonalMode" >
2022-09-07 10:42:19 +02:00
The pathfinding algorithm will avoid using diagonals if at least two obstacles have been placed around the neighboring cells of the specific path segment.
2022-07-04 20:42:47 +02:00
</constant>
<constant name= "DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES" value= "3" enum= "DiagonalMode" >
2022-09-07 10:42:19 +02:00
The pathfinding algorithm will avoid using diagonals if any obstacle has been placed around the neighboring cells of the specific path segment.
2022-07-04 20:42:47 +02:00
</constant>
<constant name= "DIAGONAL_MODE_MAX" value= "4" enum= "DiagonalMode" >
2022-09-07 10:42:19 +02:00
Represents the size of the [enum DiagonalMode] enum.
2022-07-04 20:42:47 +02:00
</constant>
</constants>
</class>