Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/doc/classes/AStar3D.xml
10277 views
1
<?xml version="1.0" encoding="UTF-8" ?>
2
<class name="AStar3D" 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 3D space.
5
</brief_description>
6
<description>
7
A* (A star) is a computer algorithm used in pathfinding and graph traversal, the process of plotting short paths among vertices (points), passing through a given set of edges (segments). It enjoys widespread use due to its performance and accuracy. Godot's A* implementation uses points in 3D space and Euclidean distances by default.
8
You must add points manually with [method add_point] and create segments manually with [method connect_points]. Once done, you can test if there is a path between two points with the [method are_points_connected] function, get a path containing indices by [method get_id_path], or one containing actual coordinates with [method get_point_path].
9
It is also possible to use non-Euclidean distances. To do so, create a script that extends [AStar3D] and override the methods [method _compute_cost] and [method _estimate_cost]. Both should take two point IDs and return the distance between the corresponding points.
10
[b]Example:[/b] Use Manhattan distance instead of Euclidean distance:
11
[codeblocks]
12
[gdscript]
13
class_name MyAStar3D
14
extends AStar3D
15
16
func _compute_cost(u, v):
17
var u_pos = get_point_position(u)
18
var v_pos = get_point_position(v)
19
return abs(u_pos.x - v_pos.x) + abs(u_pos.y - v_pos.y) + abs(u_pos.z - v_pos.z)
20
21
func _estimate_cost(u, v):
22
var u_pos = get_point_position(u)
23
var v_pos = get_point_position(v)
24
return abs(u_pos.x - v_pos.x) + abs(u_pos.y - v_pos.y) + abs(u_pos.z - v_pos.z)
25
[/gdscript]
26
[csharp]
27
using Godot;
28
29
[GlobalClass]
30
public partial class MyAStar3D : AStar3D
31
{
32
public override float _ComputeCost(long fromId, long toId)
33
{
34
Vector3 fromPoint = GetPointPosition(fromId);
35
Vector3 toPoint = GetPointPosition(toId);
36
37
return Mathf.Abs(fromPoint.X - toPoint.X) + Mathf.Abs(fromPoint.Y - toPoint.Y) + Mathf.Abs(fromPoint.Z - toPoint.Z);
38
}
39
40
public override float _EstimateCost(long fromId, long toId)
41
{
42
Vector3 fromPoint = GetPointPosition(fromId);
43
Vector3 toPoint = GetPointPosition(toId);
44
return Mathf.Abs(fromPoint.X - toPoint.X) + Mathf.Abs(fromPoint.Y - toPoint.Y) + Mathf.Abs(fromPoint.Z - toPoint.Z);
45
}
46
}
47
[/csharp]
48
[/codeblocks]
49
[method _estimate_cost] should return a lower bound of the distance, i.e. [code]_estimate_cost(u, v) &lt;= _compute_cost(u, v)[/code]. This serves as a hint to the algorithm because the custom [method _compute_cost] might be computation-heavy. If this is not the case, make [method _estimate_cost] return the same value as [method _compute_cost] to provide the algorithm with the most accurate information.
50
If the default [method _estimate_cost] and [method _compute_cost] methods are used, or if the supplied [method _estimate_cost] method returns a lower bound of the cost, then the paths returned by A* will be the lowest-cost paths. Here, the cost of a path equals the sum of the [method _compute_cost] results of all segments in the path multiplied by the [code]weight_scale[/code]s of the endpoints of the respective segments. If the default methods are used and the [code]weight_scale[/code]s of all points are set to [code]1.0[/code], then this equals the sum of Euclidean distances of all segments in the path.
51
</description>
52
<tutorials>
53
</tutorials>
54
<methods>
55
<method name="_compute_cost" qualifiers="virtual const">
56
<return type="float" />
57
<param index="0" name="from_id" type="int" />
58
<param index="1" name="to_id" type="int" />
59
<description>
60
Called when computing the cost between two connected points.
61
Note that this function is hidden in the default [AStar3D] class.
62
</description>
63
</method>
64
<method name="_estimate_cost" qualifiers="virtual const">
65
<return type="float" />
66
<param index="0" name="from_id" type="int" />
67
<param index="1" name="end_id" type="int" />
68
<description>
69
Called when estimating the cost between a point and the path's ending point.
70
Note that this function is hidden in the default [AStar3D] class.
71
</description>
72
</method>
73
<method name="_filter_neighbor" qualifiers="virtual const">
74
<return type="bool" />
75
<param index="0" name="from_id" type="int" />
76
<param index="1" name="neighbor_id" type="int" />
77
<description>
78
Called when neighboring point enters processing and if [member neighbor_filter_enabled] is [code]true[/code]. If [code]true[/code] is returned the point will not be processed.
79
Note that this function is hidden in the default [AStar3D] class.
80
</description>
81
</method>
82
<method name="add_point">
83
<return type="void" />
84
<param index="0" name="id" type="int" />
85
<param index="1" name="position" type="Vector3" />
86
<param index="2" name="weight_scale" type="float" default="1.0" />
87
<description>
88
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.
89
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.
90
[codeblocks]
91
[gdscript]
92
var astar = AStar3D.new()
93
astar.add_point(1, Vector3(1, 0, 0), 4) # Adds the point (1, 0, 0) with weight_scale 4 and id 1
94
[/gdscript]
95
[csharp]
96
var astar = new AStar3D();
97
astar.AddPoint(1, new Vector3(1, 0, 0), 4); // Adds the point (1, 0, 0) with weight_scale 4 and id 1
98
[/csharp]
99
[/codeblocks]
100
If there already exists a point for the given [param id], its position and weight scale are updated to the given values.
101
</description>
102
</method>
103
<method name="are_points_connected" qualifiers="const">
104
<return type="bool" />
105
<param index="0" name="id" type="int" />
106
<param index="1" name="to_id" type="int" />
107
<param index="2" name="bidirectional" type="bool" default="true" />
108
<description>
109
Returns whether the two given points are directly connected by a segment. If [param bidirectional] is [code]false[/code], returns whether movement from [param id] to [param to_id] is possible through this segment.
110
</description>
111
</method>
112
<method name="clear">
113
<return type="void" />
114
<description>
115
Clears all the points and segments.
116
</description>
117
</method>
118
<method name="connect_points">
119
<return type="void" />
120
<param index="0" name="id" type="int" />
121
<param index="1" name="to_id" type="int" />
122
<param index="2" name="bidirectional" type="bool" default="true" />
123
<description>
124
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.
125
[codeblocks]
126
[gdscript]
127
var astar = AStar3D.new()
128
astar.add_point(1, Vector3(1, 1, 0))
129
astar.add_point(2, Vector3(0, 5, 0))
130
astar.connect_points(1, 2, false)
131
[/gdscript]
132
[csharp]
133
var astar = new AStar3D();
134
astar.AddPoint(1, new Vector3(1, 1, 0));
135
astar.AddPoint(2, new Vector3(0, 5, 0));
136
astar.ConnectPoints(1, 2, false);
137
[/csharp]
138
[/codeblocks]
139
</description>
140
</method>
141
<method name="disconnect_points">
142
<return type="void" />
143
<param index="0" name="id" type="int" />
144
<param index="1" name="to_id" type="int" />
145
<param index="2" name="bidirectional" type="bool" default="true" />
146
<description>
147
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.
148
</description>
149
</method>
150
<method name="get_available_point_id" qualifiers="const">
151
<return type="int" />
152
<description>
153
Returns the next available point ID with no point associated to it.
154
</description>
155
</method>
156
<method name="get_closest_point" qualifiers="const">
157
<return type="int" />
158
<param index="0" name="to_position" type="Vector3" />
159
<param index="1" name="include_disabled" type="bool" default="false" />
160
<description>
161
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.
162
[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.
163
</description>
164
</method>
165
<method name="get_closest_position_in_segment" qualifiers="const">
166
<return type="Vector3" />
167
<param index="0" name="to_position" type="Vector3" />
168
<description>
169
Returns the closest position to [param to_position] that resides inside a segment between two connected points.
170
[codeblocks]
171
[gdscript]
172
var astar = AStar3D.new()
173
astar.add_point(1, Vector3(0, 0, 0))
174
astar.add_point(2, Vector3(0, 5, 0))
175
astar.connect_points(1, 2)
176
var res = astar.get_closest_position_in_segment(Vector3(3, 3, 0)) # Returns (0, 3, 0)
177
[/gdscript]
178
[csharp]
179
var astar = new AStar3D();
180
astar.AddPoint(1, new Vector3(0, 0, 0));
181
astar.AddPoint(2, new Vector3(0, 5, 0));
182
astar.ConnectPoints(1, 2);
183
Vector3 res = astar.GetClosestPositionInSegment(new Vector3(3, 3, 0)); // Returns (0, 3, 0)
184
[/csharp]
185
[/codeblocks]
186
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.
187
</description>
188
</method>
189
<method name="get_id_path">
190
<return type="PackedInt64Array" />
191
<param index="0" name="from_id" type="int" />
192
<param index="1" name="to_id" type="int" />
193
<param index="2" name="allow_partial_path" type="bool" default="false" />
194
<description>
195
Returns an array with the IDs of the points that form the path found by AStar3D between the given points. The array is ordered from the starting point to the ending point of the path.
196
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.
197
[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.
198
[codeblocks]
199
[gdscript]
200
var astar = AStar3D.new()
201
astar.add_point(1, Vector3(0, 0, 0))
202
astar.add_point(2, Vector3(0, 1, 0), 1) # Default weight is 1
203
astar.add_point(3, Vector3(1, 1, 0))
204
astar.add_point(4, Vector3(2, 0, 0))
205
206
astar.connect_points(1, 2, false)
207
astar.connect_points(2, 3, false)
208
astar.connect_points(4, 3, false)
209
astar.connect_points(1, 4, false)
210
211
var res = astar.get_id_path(1, 3) # Returns [1, 2, 3]
212
[/gdscript]
213
[csharp]
214
var astar = new AStar3D();
215
astar.AddPoint(1, new Vector3(0, 0, 0));
216
astar.AddPoint(2, new Vector3(0, 1, 0), 1); // Default weight is 1
217
astar.AddPoint(3, new Vector3(1, 1, 0));
218
astar.AddPoint(4, new Vector3(2, 0, 0));
219
astar.ConnectPoints(1, 2, false);
220
astar.ConnectPoints(2, 3, false);
221
astar.ConnectPoints(4, 3, false);
222
astar.ConnectPoints(1, 4, false);
223
long[] res = astar.GetIdPath(1, 3); // Returns [1, 2, 3]
224
[/csharp]
225
[/codeblocks]
226
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.
227
</description>
228
</method>
229
<method name="get_point_capacity" qualifiers="const">
230
<return type="int" />
231
<description>
232
Returns the capacity of the structure backing the points, useful in conjunction with [method reserve_space].
233
</description>
234
</method>
235
<method name="get_point_connections">
236
<return type="PackedInt64Array" />
237
<param index="0" name="id" type="int" />
238
<description>
239
Returns an array with the IDs of the points that form the connection with the given point.
240
[codeblocks]
241
[gdscript]
242
var astar = AStar3D.new()
243
astar.add_point(1, Vector3(0, 0, 0))
244
astar.add_point(2, Vector3(0, 1, 0))
245
astar.add_point(3, Vector3(1, 1, 0))
246
astar.add_point(4, Vector3(2, 0, 0))
247
248
astar.connect_points(1, 2, true)
249
astar.connect_points(1, 3, true)
250
251
var neighbors = astar.get_point_connections(1) # Returns [2, 3]
252
[/gdscript]
253
[csharp]
254
var astar = new AStar3D();
255
astar.AddPoint(1, new Vector3(0, 0, 0));
256
astar.AddPoint(2, new Vector3(0, 1, 0));
257
astar.AddPoint(3, new Vector3(1, 1, 0));
258
astar.AddPoint(4, new Vector3(2, 0, 0));
259
astar.ConnectPoints(1, 2, true);
260
astar.ConnectPoints(1, 3, true);
261
262
long[] neighbors = astar.GetPointConnections(1); // Returns [2, 3]
263
[/csharp]
264
[/codeblocks]
265
</description>
266
</method>
267
<method name="get_point_count" qualifiers="const">
268
<return type="int" />
269
<description>
270
Returns the number of points currently in the points pool.
271
</description>
272
</method>
273
<method name="get_point_ids">
274
<return type="PackedInt64Array" />
275
<description>
276
Returns an array of all point IDs.
277
</description>
278
</method>
279
<method name="get_point_path">
280
<return type="PackedVector3Array" />
281
<param index="0" name="from_id" type="int" />
282
<param index="1" name="to_id" type="int" />
283
<param index="2" name="allow_partial_path" type="bool" default="false" />
284
<description>
285
Returns an array with the points that are in the path found by AStar3D between the given points. The array is ordered from the starting point to the ending point of the path.
286
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.
287
[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.
288
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.
289
</description>
290
</method>
291
<method name="get_point_position" qualifiers="const">
292
<return type="Vector3" />
293
<param index="0" name="id" type="int" />
294
<description>
295
Returns the position of the point associated with the given [param id].
296
</description>
297
</method>
298
<method name="get_point_weight_scale" qualifiers="const">
299
<return type="float" />
300
<param index="0" name="id" type="int" />
301
<description>
302
Returns the weight scale of the point associated with the given [param id].
303
</description>
304
</method>
305
<method name="has_point" qualifiers="const">
306
<return type="bool" />
307
<param index="0" name="id" type="int" />
308
<description>
309
Returns whether a point associated with the given [param id] exists.
310
</description>
311
</method>
312
<method name="is_point_disabled" qualifiers="const">
313
<return type="bool" />
314
<param index="0" name="id" type="int" />
315
<description>
316
Returns whether a point is disabled or not for pathfinding. By default, all points are enabled.
317
</description>
318
</method>
319
<method name="remove_point">
320
<return type="void" />
321
<param index="0" name="id" type="int" />
322
<description>
323
Removes the point associated with the given [param id] from the points pool.
324
</description>
325
</method>
326
<method name="reserve_space">
327
<return type="void" />
328
<param index="0" name="num_nodes" type="int" />
329
<description>
330
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.
331
</description>
332
</method>
333
<method name="set_point_disabled">
334
<return type="void" />
335
<param index="0" name="id" type="int" />
336
<param index="1" name="disabled" type="bool" default="true" />
337
<description>
338
Disables or enables the specified point for pathfinding. Useful for making a temporary obstacle.
339
</description>
340
</method>
341
<method name="set_point_position">
342
<return type="void" />
343
<param index="0" name="id" type="int" />
344
<param index="1" name="position" type="Vector3" />
345
<description>
346
Sets the [param position] for the point with the given [param id].
347
</description>
348
</method>
349
<method name="set_point_weight_scale">
350
<return type="void" />
351
<param index="0" name="id" type="int" />
352
<param index="1" name="weight_scale" type="float" />
353
<description>
354
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.
355
</description>
356
</method>
357
</methods>
358
<members>
359
<member name="neighbor_filter_enabled" type="bool" setter="set_neighbor_filter_enabled" getter="is_neighbor_filter_enabled" default="false">
360
If [code]true[/code] enables the filtering of neighbors via [method _filter_neighbor].
361
</member>
362
</members>
363
</class>
364
365