Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/doc/classes/AStar2D.xml
10277 views
1
<?xml version="1.0" encoding="UTF-8" ?>
2
<class name="AStar2D" 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 vertices on a connected graph in 2D space.
5
</brief_description>
6
<description>
7
An implementation of the A* algorithm, used to find the shortest path between two vertices on a connected graph in 2D space.
8
See [AStar3D] for a more thorough explanation on how to use this class. [AStar2D] is a wrapper for [AStar3D] that enforces 2D coordinates.
9
</description>
10
<tutorials>
11
<link title="Grid-based Navigation with AStarGrid2D Demo">https://godotengine.org/asset-library/asset/2723</link>
12
</tutorials>
13
<methods>
14
<method name="_compute_cost" qualifiers="virtual const">
15
<return type="float" />
16
<param index="0" name="from_id" type="int" />
17
<param index="1" name="to_id" type="int" />
18
<description>
19
Called when computing the cost between two connected points.
20
Note that this function is hidden in the default [AStar2D] class.
21
</description>
22
</method>
23
<method name="_estimate_cost" qualifiers="virtual const">
24
<return type="float" />
25
<param index="0" name="from_id" type="int" />
26
<param index="1" name="end_id" type="int" />
27
<description>
28
Called when estimating the cost between a point and the path's ending point.
29
Note that this function is hidden in the default [AStar2D] class.
30
</description>
31
</method>
32
<method name="_filter_neighbor" qualifiers="virtual const">
33
<return type="bool" />
34
<param index="0" name="from_id" type="int" />
35
<param index="1" name="neighbor_id" type="int" />
36
<description>
37
Called when neighboring enters processing and if [member neighbor_filter_enabled] is [code]true[/code]. If [code]true[/code] is returned the point will not be processed.
38
Note that this function is hidden in the default [AStar2D] class.
39
</description>
40
</method>
41
<method name="add_point">
42
<return type="void" />
43
<param index="0" name="id" type="int" />
44
<param index="1" name="position" type="Vector2" />
45
<param index="2" name="weight_scale" type="float" default="1.0" />
46
<description>
47
Adds a new point at the given position with the given identifier. The [param id] must be 0 or larger, and the [param weight_scale] must be 0.0 or greater.
48
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. Thus, all else being equal, the algorithm prefers points with lower [param weight_scale]s to form a path.
49
[codeblocks]
50
[gdscript]
51
var astar = AStar2D.new()
52
astar.add_point(1, Vector2(1, 0), 4) # Adds the point (1, 0) with weight_scale 4 and id 1
53
[/gdscript]
54
[csharp]
55
var astar = new AStar2D();
56
astar.AddPoint(1, new Vector2(1, 0), 4); // Adds the point (1, 0) with weight_scale 4 and id 1
57
[/csharp]
58
[/codeblocks]
59
If there already exists a point for the given [param id], its position and weight scale are updated to the given values.
60
</description>
61
</method>
62
<method name="are_points_connected" qualifiers="const">
63
<return type="bool" />
64
<param index="0" name="id" type="int" />
65
<param index="1" name="to_id" type="int" />
66
<param index="2" name="bidirectional" type="bool" default="true" />
67
<description>
68
Returns whether there is a connection/segment between the given points. If [param bidirectional] is [code]false[/code], returns whether movement from [param id] to [param to_id] is possible through this segment.
69
</description>
70
</method>
71
<method name="clear">
72
<return type="void" />
73
<description>
74
Clears all the points and segments.
75
</description>
76
</method>
77
<method name="connect_points">
78
<return type="void" />
79
<param index="0" name="id" type="int" />
80
<param index="1" name="to_id" type="int" />
81
<param index="2" name="bidirectional" type="bool" default="true" />
82
<description>
83
Creates a segment between the given points. If [param bidirectional] is [code]false[/code], only movement from [param id] to [param to_id] is allowed, not the reverse direction.
84
[codeblocks]
85
[gdscript]
86
var astar = AStar2D.new()
87
astar.add_point(1, Vector2(1, 1))
88
astar.add_point(2, Vector2(0, 5))
89
astar.connect_points(1, 2, false)
90
[/gdscript]
91
[csharp]
92
var astar = new AStar2D();
93
astar.AddPoint(1, new Vector2(1, 1));
94
astar.AddPoint(2, new Vector2(0, 5));
95
astar.ConnectPoints(1, 2, false);
96
[/csharp]
97
[/codeblocks]
98
</description>
99
</method>
100
<method name="disconnect_points">
101
<return type="void" />
102
<param index="0" name="id" type="int" />
103
<param index="1" name="to_id" type="int" />
104
<param index="2" name="bidirectional" type="bool" default="true" />
105
<description>
106
Deletes the segment between the given points. If [param bidirectional] is [code]false[/code], only movement from [param id] to [param to_id] is prevented, and a unidirectional segment possibly remains.
107
</description>
108
</method>
109
<method name="get_available_point_id" qualifiers="const">
110
<return type="int" />
111
<description>
112
Returns the next available point ID with no point associated to it.
113
</description>
114
</method>
115
<method name="get_closest_point" qualifiers="const">
116
<return type="int" />
117
<param index="0" name="to_position" type="Vector2" />
118
<param index="1" name="include_disabled" type="bool" default="false" />
119
<description>
120
Returns the ID of the closest point to [param to_position], optionally taking disabled points into account. Returns [code]-1[/code] if there are no points in the points pool.
121
[b]Note:[/b] If several points are the closest to [param to_position], the one with the smallest ID will be returned, ensuring a deterministic result.
122
</description>
123
</method>
124
<method name="get_closest_position_in_segment" qualifiers="const">
125
<return type="Vector2" />
126
<param index="0" name="to_position" type="Vector2" />
127
<description>
128
Returns the closest position to [param to_position] that resides inside a segment between two connected points.
129
[codeblocks]
130
[gdscript]
131
var astar = AStar2D.new()
132
astar.add_point(1, Vector2(0, 0))
133
astar.add_point(2, Vector2(0, 5))
134
astar.connect_points(1, 2)
135
var res = astar.get_closest_position_in_segment(Vector2(3, 3)) # Returns (0, 3)
136
[/gdscript]
137
[csharp]
138
var astar = new AStar2D();
139
astar.AddPoint(1, new Vector2(0, 0));
140
astar.AddPoint(2, new Vector2(0, 5));
141
astar.ConnectPoints(1, 2);
142
Vector2 res = astar.GetClosestPositionInSegment(new Vector2(3, 3)); // Returns (0, 3)
143
[/csharp]
144
[/codeblocks]
145
The result is in the segment that goes from [code]y = 0[/code] to [code]y = 5[/code]. It's the closest position in the segment to the given point.
146
</description>
147
</method>
148
<method name="get_id_path">
149
<return type="PackedInt64Array" />
150
<param index="0" name="from_id" type="int" />
151
<param index="1" name="to_id" type="int" />
152
<param index="2" name="allow_partial_path" type="bool" default="false" />
153
<description>
154
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.
155
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.
156
[b]Note:[/b] When [param allow_partial_path] is [code]true[/code] and [param to_id] is disabled the search may take an unusually long time to finish.
157
[codeblocks]
158
[gdscript]
159
var astar = AStar2D.new()
160
astar.add_point(1, Vector2(0, 0))
161
astar.add_point(2, Vector2(0, 1), 1) # Default weight is 1
162
astar.add_point(3, Vector2(1, 1))
163
astar.add_point(4, Vector2(2, 0))
164
165
astar.connect_points(1, 2, false)
166
astar.connect_points(2, 3, false)
167
astar.connect_points(4, 3, false)
168
astar.connect_points(1, 4, false)
169
170
var res = astar.get_id_path(1, 3) # Returns [1, 2, 3]
171
[/gdscript]
172
[csharp]
173
var astar = new AStar2D();
174
astar.AddPoint(1, new Vector2(0, 0));
175
astar.AddPoint(2, new Vector2(0, 1), 1); // Default weight is 1
176
astar.AddPoint(3, new Vector2(1, 1));
177
astar.AddPoint(4, new Vector2(2, 0));
178
179
astar.ConnectPoints(1, 2, false);
180
astar.ConnectPoints(2, 3, false);
181
astar.ConnectPoints(4, 3, false);
182
astar.ConnectPoints(1, 4, false);
183
long[] res = astar.GetIdPath(1, 3); // Returns [1, 2, 3]
184
[/csharp]
185
[/codeblocks]
186
If you change the 2nd point's weight to 3, then the result will be [code][1, 4, 3][/code] instead, because now even though the distance is longer, it's "easier" to get through point 4 than through point 2.
187
</description>
188
</method>
189
<method name="get_point_capacity" qualifiers="const">
190
<return type="int" />
191
<description>
192
Returns the capacity of the structure backing the points, useful in conjunction with [method reserve_space].
193
</description>
194
</method>
195
<method name="get_point_connections">
196
<return type="PackedInt64Array" />
197
<param index="0" name="id" type="int" />
198
<description>
199
Returns an array with the IDs of the points that form the connection with the given point.
200
[codeblocks]
201
[gdscript]
202
var astar = AStar2D.new()
203
astar.add_point(1, Vector2(0, 0))
204
astar.add_point(2, Vector2(0, 1))
205
astar.add_point(3, Vector2(1, 1))
206
astar.add_point(4, Vector2(2, 0))
207
208
astar.connect_points(1, 2, true)
209
astar.connect_points(1, 3, true)
210
211
var neighbors = astar.get_point_connections(1) # Returns [2, 3]
212
[/gdscript]
213
[csharp]
214
var astar = new AStar2D();
215
astar.AddPoint(1, new Vector2(0, 0));
216
astar.AddPoint(2, new Vector2(0, 1));
217
astar.AddPoint(3, new Vector2(1, 1));
218
astar.AddPoint(4, new Vector2(2, 0));
219
220
astar.ConnectPoints(1, 2, true);
221
astar.ConnectPoints(1, 3, true);
222
223
long[] neighbors = astar.GetPointConnections(1); // Returns [2, 3]
224
[/csharp]
225
[/codeblocks]
226
</description>
227
</method>
228
<method name="get_point_count" qualifiers="const">
229
<return type="int" />
230
<description>
231
Returns the number of points currently in the points pool.
232
</description>
233
</method>
234
<method name="get_point_ids">
235
<return type="PackedInt64Array" />
236
<description>
237
Returns an array of all point IDs.
238
</description>
239
</method>
240
<method name="get_point_path">
241
<return type="PackedVector2Array" />
242
<param index="0" name="from_id" type="int" />
243
<param index="1" name="to_id" type="int" />
244
<param index="2" name="allow_partial_path" type="bool" default="false" />
245
<description>
246
Returns an array with the points that are in the path found by AStar2D between the given points. The array is ordered from the starting point to the ending point of the path.
247
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.
248
[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.
249
Additionally, when [param allow_partial_path] is [code]true[/code] and [param to_id] is disabled the search may take an unusually long time to finish.
250
</description>
251
</method>
252
<method name="get_point_position" qualifiers="const">
253
<return type="Vector2" />
254
<param index="0" name="id" type="int" />
255
<description>
256
Returns the position of the point associated with the given [param id].
257
</description>
258
</method>
259
<method name="get_point_weight_scale" qualifiers="const">
260
<return type="float" />
261
<param index="0" name="id" type="int" />
262
<description>
263
Returns the weight scale of the point associated with the given [param id].
264
</description>
265
</method>
266
<method name="has_point" qualifiers="const">
267
<return type="bool" />
268
<param index="0" name="id" type="int" />
269
<description>
270
Returns whether a point associated with the given [param id] exists.
271
</description>
272
</method>
273
<method name="is_point_disabled" qualifiers="const">
274
<return type="bool" />
275
<param index="0" name="id" type="int" />
276
<description>
277
Returns whether a point is disabled or not for pathfinding. By default, all points are enabled.
278
</description>
279
</method>
280
<method name="remove_point">
281
<return type="void" />
282
<param index="0" name="id" type="int" />
283
<description>
284
Removes the point associated with the given [param id] from the points pool.
285
</description>
286
</method>
287
<method name="reserve_space">
288
<return type="void" />
289
<param index="0" name="num_nodes" type="int" />
290
<description>
291
Reserves space internally for [param num_nodes] points. Useful if you're adding a known large number of points at once, such as points on a grid.
292
</description>
293
</method>
294
<method name="set_point_disabled">
295
<return type="void" />
296
<param index="0" name="id" type="int" />
297
<param index="1" name="disabled" type="bool" default="true" />
298
<description>
299
Disables or enables the specified point for pathfinding. Useful for making a temporary obstacle.
300
</description>
301
</method>
302
<method name="set_point_position">
303
<return type="void" />
304
<param index="0" name="id" type="int" />
305
<param index="1" name="position" type="Vector2" />
306
<description>
307
Sets the [param position] for the point with the given [param id].
308
</description>
309
</method>
310
<method name="set_point_weight_scale">
311
<return type="void" />
312
<param index="0" name="id" type="int" />
313
<param index="1" name="weight_scale" type="float" />
314
<description>
315
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.
316
</description>
317
</method>
318
</methods>
319
<members>
320
<member name="neighbor_filter_enabled" type="bool" setter="set_neighbor_filter_enabled" getter="is_neighbor_filter_enabled" default="false">
321
If [code]true[/code] enables the filtering of neighbors via [method _filter_neighbor].
322
</member>
323
</members>
324
</class>
325
326