|
6 | 6 | <description>
|
7 | 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 | 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 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: |
10 | 11 | [codeblocks]
|
11 | 12 | [gdscript]
|
12 |
| - class MyAStar: |
13 |
| - extends AStar3D |
| 13 | + class_name MyAStar3D |
| 14 | + extends AStar3D |
14 | 15 |
|
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) |
17 | 20 |
|
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) |
20 | 25 | [/gdscript]
|
21 | 26 | [csharp]
|
22 |
| - public partial class MyAStar : AStar3D |
| 27 | + using Godot; |
| 28 | + |
| 29 | + [GlobalClass] |
| 30 | + public partial class MyAStar3D : AStar3D |
23 | 31 | {
|
24 | 32 | public override float _ComputeCost(long fromId, long toId)
|
25 | 33 | {
|
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); |
27 | 38 | }
|
28 | 39 |
|
29 | 40 | public override float _EstimateCost(long fromId, long toId)
|
30 | 41 | {
|
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); |
32 | 45 | }
|
33 | 46 | }
|
34 | 47 | [/csharp]
|
|
0 commit comments