Skip to content

Commit c6cfed4

Browse files
committedNov 10, 2024·
Docs: Update AStar3D examples
1 parent e65a237 commit c6cfed4

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed
 

‎doc/classes/AStar3D.xml

+23-10
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,42 @@
66
<description>
77
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.
88
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 class that extends [AStar3D] and override methods [method _compute_cost] and [method _estimate_cost]. Both take two indices and return a length, as is shown in the following example.
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:
1011
[codeblocks]
1112
[gdscript]
12-
class MyAStar:
13-
extends AStar3D
13+
class_name MyAStar3D
14+
extends AStar3D
1415

15-
func _compute_cost(u, v):
16-
return abs(u - v)
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)
1720

18-
func _estimate_cost(u, v):
19-
return min(0, abs(u - v) - 1)
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)
2025
[/gdscript]
2126
[csharp]
22-
public partial class MyAStar : AStar3D
27+
using Godot;
28+
29+
[GlobalClass]
30+
public partial class MyAStar3D : AStar3D
2331
{
2432
public override float _ComputeCost(long fromId, long toId)
2533
{
26-
return Mathf.Abs((int)(fromId - toId));
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);
2738
}
2839

2940
public override float _EstimateCost(long fromId, long toId)
3041
{
31-
return Mathf.Min(0, Mathf.Abs((int)(fromId - toId)) - 1);
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);
3245
}
3346
}
3447
[/csharp]

0 commit comments

Comments
 (0)
Please sign in to comment.