2022-07-04 18:42:47 +00:00
<?xml version="1.0" encoding="UTF-8" ?>
2023-07-06 08:08:05 +00:00
<class name= "AStarGrid2D" inherits= "RefCounted" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "../class.xsd" >
2022-07-04 18:42:47 +00:00
<brief_description >
2023-04-28 20:59:03 +00:00
An implementation of A* for finding the shortest path between two points on a partial 2D grid.
2022-07-04 18:42:47 +00:00
</brief_description>
<description >
2023-04-28 20:59:03 +00:00
[AStarGrid2D] is a variant of [AStar2D] that is specialized for partial 2D grids. It is simpler to use because it doesn't require you to manually create points and connect them together. This class also supports multiple types of heuristics, modes for diagonal movement, and a jumping mode to speed up calculations.
To use [AStarGrid2D], you only need to set the [member region] of the grid, optionally set the [member cell_size], and then call the [method update] method:
2022-09-15 20:04:32 +00:00
[codeblocks]
[gdscript]
2022-09-07 08:42:19 +00:00
var astar_grid = AStarGrid2D.new()
2023-02-28 09:30:10 +00:00
astar_grid.region = Rect2i(0, 0, 32, 32)
2022-09-07 08:42:19 +00:00
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)
2022-09-15 20:04:32 +00:00
[/gdscript]
[csharp]
AStarGrid2D astarGrid = new AStarGrid2D();
2023-07-22 16:34:38 +00:00
astarGrid.Region = new Rect2I(0, 0, 32, 32);
2022-12-07 15:11:39 +00:00
astarGrid.CellSize = new Vector2I(16, 16);
2022-09-15 20:04:32 +00:00
astarGrid.Update();
2022-12-07 15:11:39 +00:00
GD.Print(astarGrid.GetIdPath(Vector2I.Zero, new Vector2I(3, 4))); // prints (0, 0), (1, 1), (2, 2), (3, 3), (3, 4)
GD.Print(astarGrid.GetPointPath(Vector2I.Zero, new Vector2I(3, 4))); // prints (0, 0), (16, 16), (32, 32), (48, 48), (48, 64)
2022-09-15 20:04:32 +00:00
[/csharp]
[/codeblocks]
2023-04-28 20:59:03 +00:00
To remove a point from the pathfinding grid, it must be set as "solid" with [method set_point_solid].
2022-07-04 18:42:47 +00: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 08:42:19 +00:00
Called when computing the cost between two connected points.
2023-10-02 18:11:43 +00:00
Note that this function is hidden in the default [AStarGrid2D] class.
2022-07-04 18:42:47 +00: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 08:42:19 +00:00
Called when estimating the cost between a point and the path's ending point.
2023-10-02 18:11:43 +00:00
Note that this function is hidden in the default [AStarGrid2D] class.
2022-07-04 18:42:47 +00:00
</description>
</method>
<method name= "clear" >
<return type= "void" />
<description >
2023-02-28 09:30:10 +00:00
Clears the grid and sets the [member region] to [code]Rect2i(0, 0, 0, 0)[/code].
2022-07-04 18:42:47 +00:00
</description>
</method>
2023-07-15 06:10:50 +00:00
<method name= "fill_solid_region" >
<return type= "void" />
<param index= "0" name= "region" type= "Rect2i" />
<param index= "1" name= "solid" type= "bool" default= "true" />
<description >
Fills the given [param region] on the grid with the specified value for the solid flag.
[b]Note:[/b] Calling [method update] is not needed after the call of this function.
</description>
</method>
<method name= "fill_weight_scale_region" >
<return type= "void" />
<param index= "0" name= "region" type= "Rect2i" />
<param index= "1" name= "weight_scale" type= "float" />
<description >
Fills the given [param region] on the grid with the specified value for the weight scale.
[b]Note:[/b] Calling [method update] is not needed after the call of this function.
</description>
</method>
2022-07-04 18:42:47 +00:00
<method name= "get_id_path" >
2022-09-30 09:46:55 +00:00
<return type= "Vector2i[]" />
2022-07-04 18:42:47 +00:00
<param index= "0" name= "from_id" type= "Vector2i" />
<param index= "1" name= "to_id" type= "Vector2i" />
2024-02-07 04:20:13 +00:00
<param index= "2" name= "allow_partial_path" type= "bool" default= "false" />
2022-07-04 18:42:47 +00:00
<description >
2022-09-07 08:42:19 +00: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.
2024-02-07 04:20:13 +00:00
If there is no valid path to the target, and [param allow_partial_path] is [code]true[/code], returns a path to the point closest to the target that can be reached.
2022-07-04 18:42:47 +00: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" />
2024-02-07 04:20:13 +00:00
<param index= "2" name= "allow_partial_path" type= "bool" default= "false" />
2022-07-04 18:42:47 +00:00
<description >
2023-10-02 18:11:43 +00: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.
2024-02-07 04:20:13 +00:00
If there is no valid path to the target, and [param allow_partial_path] is [code]true[/code], returns a path to the point closest to the target that can be reached.
2024-03-24 18:50:59 +00:00
[b]Note:[/b] This method is not thread-safe. If called from a [Thread], it will return an empty array and will print an error message.
2022-07-04 18:42:47 +00:00
</description>
</method>
2022-12-20 20:21:38 +00:00
<method name= "get_point_position" qualifiers= "const" >
<return type= "Vector2" />
<param index= "0" name= "id" type= "Vector2i" />
<description >
Returns the position of the point associated with the given [param id].
</description>
</method>
2022-12-20 08:48:53 +00:00
<method name= "get_point_weight_scale" qualifiers= "const" >
<return type= "float" />
<param index= "0" name= "id" type= "Vector2i" />
<description >
Returns the weight scale of the point associated with the given [param id].
</description>
</method>
2022-07-04 18:42:47 +00:00
<method name= "is_dirty" qualifiers= "const" >
<return type= "bool" />
<description >
2022-09-07 08:42:19 +00:00
Indicates that the grid parameters were changed and [method update] needs to be called.
2022-07-04 18:42:47 +00: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 >
2023-10-03 12:24:12 +00:00
Returns [code]true[/code] if the [param x] and [param y] is a valid grid coordinate (id), i.e. if it is inside [member region]. Equivalent to [code]region.has_point(Vector2i(x, y))[/code].
2022-07-04 18:42:47 +00:00
</description>
</method>
<method name= "is_in_boundsv" qualifiers= "const" >
<return type= "bool" />
<param index= "0" name= "id" type= "Vector2i" />
<description >
2023-10-03 12:24:12 +00:00
Returns [code]true[/code] if the [param id] vector is a valid grid coordinate, i.e. if it is inside [member region]. Equivalent to [code]region.has_point(id)[/code].
2022-07-04 18:42:47 +00:00
</description>
</method>
<method name= "is_point_solid" qualifiers= "const" >
<return type= "bool" />
<param index= "0" name= "id" type= "Vector2i" />
<description >
2022-09-07 08:42:19 +00:00
Returns [code]true[/code] if a point is disabled for pathfinding. By default, all points are enabled.
2022-07-04 18:42:47 +00: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 08:42:19 +00:00
Disables or enables the specified point for pathfinding. Useful for making an obstacle. By default, all points are enabled.
2022-12-14 13:35:02 +00:00
[b]Note:[/b] Calling [method update] is not needed after the call of this function.
2022-07-04 18:42:47 +00:00
</description>
</method>
2022-12-20 08:48:53 +00:00
<method name= "set_point_weight_scale" >
<return type= "void" />
<param index= "0" name= "id" type= "Vector2i" />
<param index= "1" name= "weight_scale" type= "float" />
<description >
Sets the [param weight_scale] for the point with the given [param id]. The [param weight_scale] is multiplied by the result of [method _compute_cost] when determining the overall cost of traveling across a segment from a neighboring point to this point.
[b]Note:[/b] Calling [method update] is not needed after the call of this function.
</description>
</method>
2022-07-04 18:42:47 +00:00
<method name= "update" >
<return type= "void" />
<description >
2023-02-28 09:30:10 +00: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 region], [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.
[b]Note:[/b] All point data (solidity and weight scale) will be cleared.
2022-07-04 18:42:47 +00:00
</description>
</method>
</methods>
<members >
2023-09-03 11:08:35 +00:00
<member name= "cell_shape" type= "int" setter= "set_cell_shape" getter= "get_cell_shape" enum= "AStarGrid2D.CellShape" default= "0" >
The cell shape. Affects how the positions are placed in the grid. If changed, [method update] needs to be called before finding the next path.
</member>
2022-07-04 18:42:47 +00:00
<member name= "cell_size" type= "Vector2" setter= "set_cell_size" getter= "get_cell_size" default= "Vector2(1, 1)" >
2022-09-07 08:42:19 +00: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 18:42:47 +00:00
</member>
2022-12-23 20:37:04 +00:00
<member name= "default_compute_heuristic" type= "int" setter= "set_default_compute_heuristic" getter= "get_default_compute_heuristic" enum= "AStarGrid2D.Heuristic" default= "0" >
The default [enum Heuristic] which will be used to calculate the cost between two points if [method _compute_cost] was not overridden.
</member>
<member name= "default_estimate_heuristic" type= "int" setter= "set_default_estimate_heuristic" getter= "get_default_estimate_heuristic" enum= "AStarGrid2D.Heuristic" default= "0" >
The default [enum Heuristic] which will be used to calculate the cost between the point and the end point if [method _estimate_cost] was not overridden.
2022-07-04 18:42:47 +00:00
</member>
<member name= "diagonal_mode" type= "int" setter= "set_diagonal_mode" getter= "get_diagonal_mode" enum= "AStarGrid2D.DiagonalMode" default= "0" >
2022-09-07 08:42:19 +00:00
A specific [enum DiagonalMode] mode which will force the path to avoid or accept the specified diagonals.
2022-07-04 18:42:47 +00:00
</member>
<member name= "jumping_enabled" type= "bool" setter= "set_jumping_enabled" getter= "is_jumping_enabled" default= "false" >
2022-09-07 08:42:19 +00:00
Enables or disables jumping to skip up the intermediate points and speeds up the searching algorithm.
2022-12-20 08:48:53 +00:00
[b]Note:[/b] Currently, toggling it on disables the consideration of weight scaling in pathfinding.
2022-07-04 18:42:47 +00:00
</member>
<member name= "offset" type= "Vector2" setter= "set_offset" getter= "get_offset" default= "Vector2(0, 0)" >
2022-09-07 08:42:19 +00: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 18:42:47 +00:00
</member>
2023-02-28 09:30:10 +00:00
<member name= "region" type= "Rect2i" setter= "set_region" getter= "get_region" default= "Rect2i(0, 0, 0, 0)" >
The region of grid cells available for pathfinding. If changed, [method update] needs to be called before finding the next path.
</member>
2024-02-12 13:55:02 +00:00
<member name= "size" type= "Vector2i" setter= "set_size" getter= "get_size" default= "Vector2i(0, 0)" deprecated= "Use [member region] instead." >
2022-09-07 08:42:19 +00: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 18:42:47 +00:00
</member>
</members>
<constants >
<constant name= "HEURISTIC_EUCLIDEAN" value= "0" enum= "Heuristic" >
2022-12-14 13:35:02 +00:00
The [url=https://en.wikipedia.org/wiki/Euclidean_distance]Euclidean heuristic[/url] to be used for the pathfinding using the following formula:
2022-09-07 08:42:19 +00:00
[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-12-14 13:35:02 +00:00
[b]Note:[/b] This is also the internal heuristic used in [AStar3D] and [AStar2D] by default (with the inclusion of possible z-axis coordinate).
2022-07-04 18:42:47 +00:00
</constant>
<constant name= "HEURISTIC_MANHATTAN" value= "1" enum= "Heuristic" >
2022-12-14 13:35:02 +00:00
The [url=https://en.wikipedia.org/wiki/Taxicab_geometry]Manhattan heuristic[/url] to be used for the pathfinding using the following formula:
2022-09-07 08:42:19 +00:00
[codeblock]
dx = abs(to_id.x - from_id.x)
dy = abs(to_id.y - from_id.y)
result = dx + dy
[/codeblock]
2022-12-14 13:35:02 +00:00
[b]Note:[/b] This heuristic is intended to be used with 4-side orthogonal movements, provided by setting the [member diagonal_mode] to [constant DIAGONAL_MODE_NEVER].
2022-07-04 18:42:47 +00:00
</constant>
<constant name= "HEURISTIC_OCTILE" value= "2" enum= "Heuristic" >
2022-09-07 08:42:19 +00: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 18:42:47 +00:00
</constant>
<constant name= "HEURISTIC_CHEBYSHEV" value= "3" enum= "Heuristic" >
2022-12-14 13:35:02 +00:00
The [url=https://en.wikipedia.org/wiki/Chebyshev_distance]Chebyshev heuristic[/url] to be used for the pathfinding using the following formula:
2022-09-07 08:42:19 +00:00
[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 18:42:47 +00:00
</constant>
<constant name= "HEURISTIC_MAX" value= "4" enum= "Heuristic" >
2022-09-07 08:42:19 +00:00
Represents the size of the [enum Heuristic] enum.
2022-07-04 18:42:47 +00:00
</constant>
<constant name= "DIAGONAL_MODE_ALWAYS" value= "0" enum= "DiagonalMode" >
2022-09-07 08:42:19 +00:00
The pathfinding algorithm will ignore solid neighbors around the target cell and allow passing using diagonals.
2022-07-04 18:42:47 +00:00
</constant>
<constant name= "DIAGONAL_MODE_NEVER" value= "1" enum= "DiagonalMode" >
2022-09-07 08:42:19 +00:00
The pathfinding algorithm will ignore all diagonals and the way will be always orthogonal.
2022-07-04 18:42:47 +00:00
</constant>
<constant name= "DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE" value= "2" enum= "DiagonalMode" >
2022-09-07 08:42:19 +00: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 18:42:47 +00:00
</constant>
<constant name= "DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES" value= "3" enum= "DiagonalMode" >
2022-09-07 08:42:19 +00: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 18:42:47 +00:00
</constant>
<constant name= "DIAGONAL_MODE_MAX" value= "4" enum= "DiagonalMode" >
2022-09-07 08:42:19 +00:00
Represents the size of the [enum DiagonalMode] enum.
2022-07-04 18:42:47 +00:00
</constant>
2023-09-03 11:08:35 +00:00
<constant name= "CELL_SHAPE_SQUARE" value= "0" enum= "CellShape" >
Rectangular cell shape.
</constant>
<constant name= "CELL_SHAPE_ISOMETRIC_RIGHT" value= "1" enum= "CellShape" >
Diamond cell shape (for isometric look). Cell coordinates layout where the horizontal axis goes up-right, and the vertical one goes down-right.
</constant>
<constant name= "CELL_SHAPE_ISOMETRIC_DOWN" value= "2" enum= "CellShape" >
Diamond cell shape (for isometric look). Cell coordinates layout where the horizontal axis goes down-right, and the vertical one goes down-left.
</constant>
<constant name= "CELL_SHAPE_MAX" value= "3" enum= "CellShape" >
Represents the size of the [enum CellShape] enum.
</constant>
2022-07-04 18:42:47 +00:00
</constants>
</class>