AStar¶
An implementation of A* to find the shortest paths among connected points in space.
Description¶
A* (A star) is a computer algorithm that is widely 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 three-dimensional space and Euclidean distances by default.
You must add points manually with add_point and create segments manually with connect_points. Then you can test if there is a path between two points with the are_points_connected function, get a path containing indices by get_id_path, or one containing actual coordinates with get_point_path.
It is also possible to use non-Euclidean distances. To do so, create a class that extends AStar
and override methods _compute_cost and _estimate_cost. Both take two indices and return a length, as is shown in the following example.
class MyAStar:
extends AStar
func _compute_cost(u, v):
return abs(u - v)
func _estimate_cost(u, v):
return min(0, abs(u - v) - 1)
_estimate_cost should return a lower bound of the distance, i.e. _estimate_cost(u, v) <= _compute_cost(u, v)
. This serves as a hint to the algorithm because the custom _compute_cost
might be computation-heavy. If this is not the case, make _estimate_cost return the same value as _compute_cost to provide the algorithm with the most accurate information.
If the default _estimate_cost and _compute_cost methods are used, or if the supplied _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 _compute_cost results of all segments in the path multiplied by the weight_scale
s of the endpoints of the respective segments. If the default methods are used and the weight_scale
s of all points are set to 1.0
, then this equals the sum of Euclidean distances of all segments in the path.
Methods¶
_compute_cost ( int from_id, int to_id ) virtual |
|
_estimate_cost ( int from_id, int to_id ) virtual |
|
void |
add_point ( int id, Vector3 position, float weight_scale=1.0 ) |
are_points_connected ( int id, int to_id, bool bidirectional=true ) const |
|
void |
clear ( ) |
void |
connect_points ( int id, int to_id, bool bidirectional=true ) |
void |
disconnect_points ( int id, int to_id, bool bidirectional=true ) |
get_available_point_id ( ) const |
|
get_closest_point ( Vector3 to_position, bool include_disabled=false ) const |
|
get_closest_position_in_segment ( Vector3 to_position ) const |
|
get_id_path ( int from_id, int to_id ) |
|
get_point_capacity ( ) const |
|
get_point_connections ( int id ) |
|
get_point_count ( ) const |
|
get_point_path ( int from_id, int to_id ) |
|
get_point_position ( int id ) const |
|
get_point_weight_scale ( int id ) const |
|
get_points ( ) |
|
is_point_disabled ( int id ) const |
|
void |
remove_point ( int id ) |
void |
reserve_space ( int num_nodes ) |
void |
set_point_disabled ( int id, bool disabled=true ) |
void |
set_point_position ( int id, Vector3 position ) |
void |
set_point_weight_scale ( int id, float weight_scale ) |
Method Descriptions¶
float _compute_cost ( int from_id, int to_id ) virtual
Called when computing the cost between two connected points.
Note that this function is hidden in the default AStar
class.
float _estimate_cost ( int from_id, int to_id ) virtual
Called when estimating the cost between a point and the path's ending point.
Note that this function is hidden in the default AStar
class.
void add_point ( int id, Vector3 position, float weight_scale=1.0 )
Adds a new point at the given position with the given identifier. The id
must be 0 or larger, and the weight_scale
must be 0.0 or greater.
The weight_scale
is multiplied by the result of _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 weight_scale
s to form a path.
var astar = AStar.new()
astar.add_point(1, Vector3(1, 0, 0), 4) # Adds the point (1, 0, 0) with weight_scale 4 and id 1
If there already exists a point for the given id
, its position and weight scale are updated to the given values.
bool are_points_connected ( int id, int to_id, bool bidirectional=true ) const
Returns whether the two given points are directly connected by a segment. If bidirectional
is false
, returns whether movement from id
to to_id
is possible through this segment.
void clear ( )
Clears all the points and segments.
void connect_points ( int id, int to_id, bool bidirectional=true )
Creates a segment between the given points. If bidirectional
is false
, only movement from id
to to_id
is allowed, not the reverse direction.
var astar = AStar.new()
astar.add_point(1, Vector3(1, 1, 0))
astar.add_point(2, Vector3(0, 5, 0))
astar.connect_points(1, 2, false)
void disconnect_points ( int id, int to_id, bool bidirectional=true )
Deletes the segment between the given points. If bidirectional
is false
, only movement from id
to to_id
is prevented, and a unidirectional segment possibly remains.
int get_available_point_id ( ) const
Returns the next available point ID with no point associated to it.
int get_closest_point ( Vector3 to_position, bool include_disabled=false ) const
Returns the ID of the closest point to to_position
, optionally taking disabled points into account. Returns -1
if there are no points in the points pool.
Note: If several points are the closest to to_position
, the one with the smallest ID will be returned, ensuring a deterministic result.
Vector3 get_closest_position_in_segment ( Vector3 to_position ) const
Returns the closest position to to_position
that resides inside a segment between two connected points.
var astar = AStar.new()
astar.add_point(1, Vector3(0, 0, 0))
astar.add_point(2, Vector3(0, 5, 0))
astar.connect_points(1, 2)
var res = astar.get_closest_position_in_segment(Vector3(3, 3, 0)) # Returns (0, 3, 0)
The result is in the segment that goes from y = 0
to y = 5
. It's the closest position in the segment to the given point.
PoolIntArray get_id_path ( int from_id, int to_id )
Returns an array with the IDs of the points that form the path found by AStar between the given points. The array is ordered from the starting point to the ending point of the path.
var astar = AStar.new()
astar.add_point(1, Vector3(0, 0, 0))
astar.add_point(2, Vector3(0, 1, 0), 1) # Default weight is 1
astar.add_point(3, Vector3(1, 1, 0))
astar.add_point(4, Vector3(2, 0, 0))
astar.connect_points(1, 2, false)
astar.connect_points(2, 3, false)
astar.connect_points(4, 3, false)
astar.connect_points(1, 4, false)
var res = astar.get_id_path(1, 3) # Returns [1, 2, 3]
If you change the 2nd point's weight to 3, then the result will be [1, 4, 3]
instead, because now even though the distance is longer, it's "easier" to get through point 4 than through point 2.
int get_point_capacity ( ) const
Returns the capacity of the structure backing the points, useful in conjunction with reserve_space
.
PoolIntArray get_point_connections ( int id )
Returns an array with the IDs of the points that form the connection with the given point.
var astar = AStar.new()
astar.add_point(1, Vector3(0, 0, 0))
astar.add_point(2, Vector3(0, 1, 0))
astar.add_point(3, Vector3(1, 1, 0))
astar.add_point(4, Vector3(2, 0, 0))
astar.connect_points(1, 2, true)
astar.connect_points(1, 3, true)
var neighbors = astar.get_point_connections(1) # Returns [2, 3]
int get_point_count ( ) const
Returns the number of points currently in the points pool.
PoolVector3Array get_point_path ( int from_id, int to_id )
Returns an array with the points that are in the path found by AStar between the given points. The array is ordered from the starting point to the ending point of the path.
Note: This method is not thread-safe. If called from a Thread, it will return an empty PoolVector3Array and will print an error message.
Vector3 get_point_position ( int id ) const
Returns the position of the point associated with the given id
.
float get_point_weight_scale ( int id ) const
Returns the weight scale of the point associated with the given id
.
Array get_points ( )
Returns an array of all points.
bool has_point ( int id ) const
Returns whether a point associated with the given id
exists.
bool is_point_disabled ( int id ) const
Returns whether a point is disabled or not for pathfinding. By default, all points are enabled.
void remove_point ( int id )
Removes the point associated with the given id
from the points pool.
void reserve_space ( int num_nodes )
Reserves space internally for num_nodes
points, useful if you're adding a known large number of points at once, for a grid for instance. New capacity must be greater or equals to old capacity.
void set_point_disabled ( int id, bool disabled=true )
Disables or enables the specified point for pathfinding. Useful for making a temporary obstacle.
void set_point_position ( int id, Vector3 position )
Sets the position
for the point with the given id
.
void set_point_weight_scale ( int id, float weight_scale )
Sets the weight_scale
for the point with the given id
. The weight_scale
is multiplied by the result of _compute_cost when determining the overall cost of traveling across a segment from a neighboring point to this point.