Debian Codegraph @

doc/classes/AStar3D.xml

raw symbols SCIP
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 [param from_id] point is disabled, returns an empty array (even if [code]from_id == to_id[/code]).
197 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.
198 [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.
199 [codeblocks]
200 [gdscript]
201 var astar = AStar3D.new()
202 astar.add_point(1, Vector3(0, 0, 0))
203 astar.add_point(2, Vector3(0, 1, 0), 1) # Default weight is 1
204 astar.add_point(3, Vector3(1, 1, 0))
205 astar.add_point(4, Vector3(2, 0, 0))
206
207 astar.connect_points(1, 2, false)
208 astar.connect_points(2, 3, false)
209 astar.connect_points(4, 3, false)
210 astar.connect_points(1, 4, false)
211
212 var res = astar.get_id_path(1, 3) # Returns [1, 2, 3]
213 [/gdscript]
214 [csharp]
215 var astar = new AStar3D();
216 astar.AddPoint(1, new Vector3(0, 0, 0));
217 astar.AddPoint(2, new Vector3(0, 1, 0), 1); // Default weight is 1
218 astar.AddPoint(3, new Vector3(1, 1, 0));
219 astar.AddPoint(4, new Vector3(2, 0, 0));
220 astar.ConnectPoints(1, 2, false);
221 astar.ConnectPoints(2, 3, false);
222 astar.ConnectPoints(4, 3, false);
223 astar.ConnectPoints(1, 4, false);
224 long[] res = astar.GetIdPath(1, 3); // Returns [1, 2, 3]
225 [/csharp]
226 [/codeblocks]
227 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.
228 </description>
229 </method>
230 <method name="get_point_capacity" qualifiers="const">
231 <return type="int" />
232 <description>
233 Returns the capacity of the structure backing the points, useful in conjunction with [method reserve_space].
234 </description>
235 </method>
236 <method name="get_point_connections">
237 <return type="PackedInt64Array" />
238 <param index="0" name="id" type="int" />
239 <description>
240 Returns an array with the IDs of the points that form the connection with the given point.
241 [codeblocks]
242 [gdscript]
243 var astar = AStar3D.new()
244 astar.add_point(1, Vector3(0, 0, 0))
245 astar.add_point(2, Vector3(0, 1, 0))
246 astar.add_point(3, Vector3(1, 1, 0))
247 astar.add_point(4, Vector3(2, 0, 0))
248
249 astar.connect_points(1, 2, true)
250 astar.connect_points(1, 3, true)
251
252 var neighbors = astar.get_point_connections(1) # Returns [2, 3]
253 [/gdscript]
254 [csharp]
255 var astar = new AStar3D();
256 astar.AddPoint(1, new Vector3(0, 0, 0));
257 astar.AddPoint(2, new Vector3(0, 1, 0));
258 astar.AddPoint(3, new Vector3(1, 1, 0));
259 astar.AddPoint(4, new Vector3(2, 0, 0));
260 astar.ConnectPoints(1, 2, true);
261 astar.ConnectPoints(1, 3, true);
262
263 long[] neighbors = astar.GetPointConnections(1); // Returns [2, 3]
264 [/csharp]
265 [/codeblocks]
266 </description>
267 </method>
268 <method name="get_point_count" qualifiers="const">
269 <return type="int" />
270 <description>
271 Returns the number of points currently in the points pool.
272 </description>
273 </method>
274 <method name="get_point_ids">
275 <return type="PackedInt64Array" />
276 <description>
277 Returns an array of all point IDs.
278 </description>
279 </method>
280 <method name="get_point_path">
281 <return type="PackedVector3Array" />
282 <param index="0" name="from_id" type="int" />
283 <param index="1" name="to_id" type="int" />
284 <param index="2" name="allow_partial_path" type="bool" default="false" />
285 <description>
286 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.
287 If [param from_id] point is disabled, returns an empty array (even if [code]from_id == to_id[/code]).
288 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.
289 [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.
290 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.
291 </description>
292 </method>
293 <method name="get_point_position" qualifiers="const">
294 <return type="Vector3" />
295 <param index="0" name="id" type="int" />
296 <description>
297 Returns the position of the point associated with the given [param id].
298 </description>
299 </method>
300 <method name="get_point_weight_scale" qualifiers="const">
301 <return type="float" />
302 <param index="0" name="id" type="int" />
303 <description>
304 Returns the weight scale of the point associated with the given [param id].
305 </description>
306 </method>
307 <method name="has_point" qualifiers="const">
308 <return type="bool" />
309 <param index="0" name="id" type="int" />
310 <description>
311 Returns whether a point associated with the given [param id] exists.
312 </description>
313 </method>
314 <method name="is_point_disabled" qualifiers="const">
315 <return type="bool" />
316 <param index="0" name="id" type="int" />
317 <description>
318 Returns whether a point is disabled or not for pathfinding. By default, all points are enabled.
319 </description>
320 </method>
321 <method name="remove_point">
322 <return type="void" />
323 <param index="0" name="id" type="int" />
324 <description>
325 Removes the point associated with the given [param id] from the points pool.
326 </description>
327 </method>
328 <method name="reserve_space">
329 <return type="void" />
330 <param index="0" name="num_nodes" type="int" />
331 <description>
332 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.
333 </description>
334 </method>
335 <method name="set_point_disabled">
336 <return type="void" />
337 <param index="0" name="id" type="int" />
338 <param index="1" name="disabled" type="bool" default="true" />
339 <description>
340 Disables or enables the specified point for pathfinding. Useful for making a temporary obstacle.
341 </description>
342 </method>
343 <method name="set_point_position">
344 <return type="void" />
345 <param index="0" name="id" type="int" />
346 <param index="1" name="position" type="Vector3" />
347 <description>
348 Sets the [param position] for the point with the given [param id].
349 </description>
350 </method>
351 <method name="set_point_weight_scale">
352 <return type="void" />
353 <param index="0" name="id" type="int" />
354 <param index="1" name="weight_scale" type="float" />
355 <description>
356 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.
357 </description>
358 </method>
359 </methods>
360 <members>
361 <member name="neighbor_filter_enabled" type="bool" setter="set_neighbor_filter_enabled" getter="is_neighbor_filter_enabled" default="false">
362 If [code]true[/code] enables the filtering of neighbors via [method _filter_neighbor].
363 </member>
364 </members>
365 </class>