Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/doc/classes/AStarGrid2D.xml
20997 views
1
<?xml version="1.0" encoding="UTF-8" ?>
2
<class name="AStarGrid2D" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
3
<brief_description>
4
An implementation of A* for finding the shortest path between two points on a partial 2D grid.
5
</brief_description>
6
<description>
7
[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.
8
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:
9
[codeblocks]
10
[gdscript]
11
var astar_grid = AStarGrid2D.new()
12
astar_grid.region = Rect2i(0, 0, 32, 32)
13
astar_grid.cell_size = Vector2(16, 16)
14
astar_grid.update()
15
print(astar_grid.get_id_path(Vector2i(0, 0), Vector2i(3, 4))) # Prints [(0, 0), (1, 1), (2, 2), (3, 3), (3, 4)]
16
print(astar_grid.get_point_path(Vector2i(0, 0), Vector2i(3, 4))) # Prints [(0, 0), (16, 16), (32, 32), (48, 48), (48, 64)]
17
[/gdscript]
18
[csharp]
19
AStarGrid2D astarGrid = new AStarGrid2D();
20
astarGrid.Region = new Rect2I(0, 0, 32, 32);
21
astarGrid.CellSize = new Vector2I(16, 16);
22
astarGrid.Update();
23
GD.Print(astarGrid.GetIdPath(Vector2I.Zero, new Vector2I(3, 4))); // Prints [(0, 0), (1, 1), (2, 2), (3, 3), (3, 4)]
24
GD.Print(astarGrid.GetPointPath(Vector2I.Zero, new Vector2I(3, 4))); // Prints [(0, 0), (16, 16), (32, 32), (48, 48), (48, 64)]
25
[/csharp]
26
[/codeblocks]
27
To remove a point from the pathfinding grid, it must be set as "solid" with [method set_point_solid].
28
</description>
29
<tutorials>
30
<link title="Grid-based Navigation with AStarGrid2D Demo">https://godotengine.org/asset-library/asset/2723</link>
31
</tutorials>
32
<methods>
33
<method name="_compute_cost" qualifiers="virtual const">
34
<return type="float" />
35
<param index="0" name="from_id" type="Vector2i" />
36
<param index="1" name="to_id" type="Vector2i" />
37
<description>
38
Called when computing the cost between two connected points.
39
Note that this function is hidden in the default [AStarGrid2D] class.
40
</description>
41
</method>
42
<method name="_estimate_cost" qualifiers="virtual const">
43
<return type="float" />
44
<param index="0" name="from_id" type="Vector2i" />
45
<param index="1" name="end_id" type="Vector2i" />
46
<description>
47
Called when estimating the cost between a point and the path's ending point.
48
Note that this function is hidden in the default [AStarGrid2D] class.
49
</description>
50
</method>
51
<method name="clear">
52
<return type="void" />
53
<description>
54
Clears the grid and sets the [member region] to [code]Rect2i(0, 0, 0, 0)[/code].
55
</description>
56
</method>
57
<method name="fill_solid_region">
58
<return type="void" />
59
<param index="0" name="region" type="Rect2i" />
60
<param index="1" name="solid" type="bool" default="true" />
61
<description>
62
Fills the given [param region] on the grid with the specified value for the solid flag.
63
[b]Note:[/b] Calling [method update] is not needed after the call of this function.
64
</description>
65
</method>
66
<method name="fill_weight_scale_region">
67
<return type="void" />
68
<param index="0" name="region" type="Rect2i" />
69
<param index="1" name="weight_scale" type="float" />
70
<description>
71
Fills the given [param region] on the grid with the specified value for the weight scale.
72
[b]Note:[/b] Calling [method update] is not needed after the call of this function.
73
</description>
74
</method>
75
<method name="get_id_path">
76
<return type="Vector2i[]" />
77
<param index="0" name="from_id" type="Vector2i" />
78
<param index="1" name="to_id" type="Vector2i" />
79
<param index="2" name="allow_partial_path" type="bool" default="false" />
80
<description>
81
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.
82
If [param from_id] point is disabled, returns an empty array (even if [code]from_id == to_id[/code]).
83
If [param from_id] point is not disabled, 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.
84
[b]Note:[/b] When [param allow_partial_path] is [code]true[/code] and [param to_id] is solid the search may take an unusually long time to finish.
85
</description>
86
</method>
87
<method name="get_point_data_in_region" qualifiers="const">
88
<return type="Dictionary[]" />
89
<param index="0" name="region" type="Rect2i" />
90
<description>
91
Returns an array of dictionaries with point data ([code]id[/code]: [Vector2i], [code]position[/code]: [Vector2], [code]solid[/code]: [bool], [code]weight_scale[/code]: [float]) within a [param region].
92
</description>
93
</method>
94
<method name="get_point_path">
95
<return type="PackedVector2Array" />
96
<param index="0" name="from_id" type="Vector2i" />
97
<param index="1" name="to_id" type="Vector2i" />
98
<param index="2" name="allow_partial_path" type="bool" default="false" />
99
<description>
100
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.
101
If [param from_id] point is disabled, returns an empty array (even if [code]from_id == to_id[/code]).
102
If [param from_id] point is not disabled, 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.
103
[b]Note:[/b] This method is not thread-safe; it can only be used from a single [Thread] at a given time. Consider using [Mutex] to ensure exclusive access to one thread to avoid race conditions.
104
Additionally, when [param allow_partial_path] is [code]true[/code] and [param to_id] is solid the search may take an unusually long time to finish.
105
</description>
106
</method>
107
<method name="get_point_position" qualifiers="const">
108
<return type="Vector2" />
109
<param index="0" name="id" type="Vector2i" />
110
<description>
111
Returns the position of the point associated with the given [param id].
112
</description>
113
</method>
114
<method name="get_point_weight_scale" qualifiers="const">
115
<return type="float" />
116
<param index="0" name="id" type="Vector2i" />
117
<description>
118
Returns the weight scale of the point associated with the given [param id].
119
</description>
120
</method>
121
<method name="is_dirty" qualifiers="const">
122
<return type="bool" />
123
<description>
124
Indicates that the grid parameters were changed and [method update] needs to be called.
125
</description>
126
</method>
127
<method name="is_in_bounds" qualifiers="const">
128
<return type="bool" />
129
<param index="0" name="x" type="int" />
130
<param index="1" name="y" type="int" />
131
<description>
132
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].
133
</description>
134
</method>
135
<method name="is_in_boundsv" qualifiers="const">
136
<return type="bool" />
137
<param index="0" name="id" type="Vector2i" />
138
<description>
139
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].
140
</description>
141
</method>
142
<method name="is_point_solid" qualifiers="const">
143
<return type="bool" />
144
<param index="0" name="id" type="Vector2i" />
145
<description>
146
Returns [code]true[/code] if a point is disabled for pathfinding. By default, all points are enabled.
147
</description>
148
</method>
149
<method name="set_point_solid">
150
<return type="void" />
151
<param index="0" name="id" type="Vector2i" />
152
<param index="1" name="solid" type="bool" default="true" />
153
<description>
154
Disables or enables the specified point for pathfinding. Useful for making an obstacle. By default, all points are enabled.
155
[b]Note:[/b] Calling [method update] is not needed after the call of this function.
156
</description>
157
</method>
158
<method name="set_point_weight_scale">
159
<return type="void" />
160
<param index="0" name="id" type="Vector2i" />
161
<param index="1" name="weight_scale" type="float" />
162
<description>
163
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.
164
[b]Note:[/b] Calling [method update] is not needed after the call of this function.
165
</description>
166
</method>
167
<method name="update">
168
<return type="void" />
169
<description>
170
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.
171
[b]Note:[/b] All point data (solidity and weight scale) will be cleared.
172
</description>
173
</method>
174
</methods>
175
<members>
176
<member name="cell_shape" type="int" setter="set_cell_shape" getter="get_cell_shape" enum="AStarGrid2D.CellShape" default="0">
177
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.
178
</member>
179
<member name="cell_size" type="Vector2" setter="set_cell_size" getter="get_cell_size" default="Vector2(1, 1)">
180
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.
181
</member>
182
<member name="default_compute_heuristic" type="int" setter="set_default_compute_heuristic" getter="get_default_compute_heuristic" enum="AStarGrid2D.Heuristic" default="0">
183
The default [enum Heuristic] which will be used to calculate the cost between two points if [method _compute_cost] was not overridden.
184
</member>
185
<member name="default_estimate_heuristic" type="int" setter="set_default_estimate_heuristic" getter="get_default_estimate_heuristic" enum="AStarGrid2D.Heuristic" default="0">
186
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.
187
</member>
188
<member name="diagonal_mode" type="int" setter="set_diagonal_mode" getter="get_diagonal_mode" enum="AStarGrid2D.DiagonalMode" default="0">
189
A specific [enum DiagonalMode] mode which will force the path to avoid or accept the specified diagonals.
190
</member>
191
<member name="jumping_enabled" type="bool" setter="set_jumping_enabled" getter="is_jumping_enabled" default="false">
192
Enables or disables jumping to skip up the intermediate points and speeds up the searching algorithm.
193
[b]Note:[/b] Currently, toggling it on disables the consideration of weight scaling in pathfinding.
194
</member>
195
<member name="offset" type="Vector2" setter="set_offset" getter="get_offset" default="Vector2(0, 0)">
196
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.
197
</member>
198
<member name="region" type="Rect2i" setter="set_region" getter="get_region" default="Rect2i(0, 0, 0, 0)">
199
The region of grid cells available for pathfinding. If changed, [method update] needs to be called before finding the next path.
200
</member>
201
<member name="size" type="Vector2i" setter="set_size" getter="get_size" default="Vector2i(0, 0)" deprecated="Use [member region] instead.">
202
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.
203
</member>
204
</members>
205
<constants>
206
<constant name="HEURISTIC_EUCLIDEAN" value="0" enum="Heuristic">
207
The [url=https://en.wikipedia.org/wiki/Euclidean_distance]Euclidean heuristic[/url] to be used for the pathfinding using the following formula:
208
[codeblock]
209
dx = abs(to_id.x - from_id.x)
210
dy = abs(to_id.y - from_id.y)
211
result = sqrt(dx * dx + dy * dy)
212
[/codeblock]
213
[b]Note:[/b] This is also the internal heuristic used in [AStar3D] and [AStar2D] by default (with the inclusion of possible z-axis coordinate).
214
</constant>
215
<constant name="HEURISTIC_MANHATTAN" value="1" enum="Heuristic">
216
The [url=https://en.wikipedia.org/wiki/Taxicab_geometry]Manhattan heuristic[/url] to be used for the pathfinding using the following formula:
217
[codeblock]
218
dx = abs(to_id.x - from_id.x)
219
dy = abs(to_id.y - from_id.y)
220
result = dx + dy
221
[/codeblock]
222
[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].
223
</constant>
224
<constant name="HEURISTIC_OCTILE" value="2" enum="Heuristic">
225
The Octile heuristic to be used for the pathfinding using the following formula:
226
[codeblock]
227
dx = abs(to_id.x - from_id.x)
228
dy = abs(to_id.y - from_id.y)
229
f = sqrt(2) - 1
230
result = (dx &lt; dy) ? f * dx + dy : f * dy + dx;
231
[/codeblock]
232
</constant>
233
<constant name="HEURISTIC_CHEBYSHEV" value="3" enum="Heuristic">
234
The [url=https://en.wikipedia.org/wiki/Chebyshev_distance]Chebyshev heuristic[/url] to be used for the pathfinding using the following formula:
235
[codeblock]
236
dx = abs(to_id.x - from_id.x)
237
dy = abs(to_id.y - from_id.y)
238
result = max(dx, dy)
239
[/codeblock]
240
</constant>
241
<constant name="HEURISTIC_MAX" value="4" enum="Heuristic">
242
Represents the size of the [enum Heuristic] enum.
243
</constant>
244
<constant name="DIAGONAL_MODE_ALWAYS" value="0" enum="DiagonalMode">
245
The pathfinding algorithm will ignore solid neighbors around the target cell and allow passing using diagonals.
246
</constant>
247
<constant name="DIAGONAL_MODE_NEVER" value="1" enum="DiagonalMode">
248
The pathfinding algorithm will ignore all diagonals and the way will be always orthogonal.
249
</constant>
250
<constant name="DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE" value="2" enum="DiagonalMode">
251
The pathfinding algorithm will avoid using diagonals if at least two obstacles have been placed around the neighboring cells of the specific path segment.
252
</constant>
253
<constant name="DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES" value="3" enum="DiagonalMode">
254
The pathfinding algorithm will avoid using diagonals if any obstacle has been placed around the neighboring cells of the specific path segment.
255
</constant>
256
<constant name="DIAGONAL_MODE_MAX" value="4" enum="DiagonalMode">
257
Represents the size of the [enum DiagonalMode] enum.
258
</constant>
259
<constant name="CELL_SHAPE_SQUARE" value="0" enum="CellShape">
260
Rectangular cell shape.
261
</constant>
262
<constant name="CELL_SHAPE_ISOMETRIC_RIGHT" value="1" enum="CellShape">
263
Diamond cell shape (for isometric look). Cell coordinates layout where the horizontal axis goes up-right, and the vertical one goes down-right.
264
</constant>
265
<constant name="CELL_SHAPE_ISOMETRIC_DOWN" value="2" enum="CellShape">
266
Diamond cell shape (for isometric look). Cell coordinates layout where the horizontal axis goes down-right, and the vertical one goes down-left.
267
</constant>
268
<constant name="CELL_SHAPE_MAX" value="3" enum="CellShape">
269
Represents the size of the [enum CellShape] enum.
270
</constant>
271
</constants>
272
</class>
273
274