Class AStar3D
- Namespace
- Godot
- Assembly
- GodotSharp.dll
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.
You must add points manually with AddPoint(long, Vector3, float) and create segments manually with ConnectPoints(long, long, bool). Once done, you can test if there is a path between two points with the ArePointsConnected(long, long, bool) function, get a path containing indices by GetIdPath(long, long), or one containing actual coordinates with GetPointPath(long, long).
It is also possible to use non-Euclidean distances. To do so, create a class that extends AStar3D and override methods _ComputeCost(long, long) and _EstimateCost(long, long). Both take two indices and return a length, as is shown in the following example.
public partial class MyAStar : AStar3D
{
public override float _ComputeCost(long fromId, long toId)
{
return Mathf.Abs((int)(fromId - toId));
}
public override float _EstimateCost(long fromId, long toId)
{
return Mathf.Min(0, Mathf.Abs((int)(fromId - toId)) - 1);
}
}
_EstimateCost(long, long) 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 _ComputeCost(long, long) might be computation-heavy. If this is not the case, make _EstimateCost(long, long) return the same value as _ComputeCost(long, long) to provide the algorithm with the most accurate information.
If the default _EstimateCost(long, long) and _ComputeCost(long, long) methods are used, or if the supplied _EstimateCost(long, long) 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 _ComputeCost(long, long) 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.
public class AStar3D : RefCounted, IDisposable
- Inheritance
-
AStar3D
- Implements
- Inherited Members
Constructors
AStar3D()
public AStar3D()
Methods
AddPoint(long, Vector3, float)
Adds a new point at the given position with the given identifier. The id
must be 0 or larger, and the weightScale
must be 0.0 or greater.
The weightScale
is multiplied by the result of _ComputeCost(long, long) 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 weightScale
s to form a path.
var astar = new AStar3D();
astar.AddPoint(1, new 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.
public void AddPoint(long id, Vector3 position, float weightScale = 1)
Parameters
ArePointsConnected(long, long, bool)
Returns whether the two given points are directly connected by a segment. If bidirectional
is false
, returns whether movement from id
to toId
is possible through this segment.
public bool ArePointsConnected(long id, long toId, bool bidirectional = true)
Parameters
Returns
Clear()
Clears all the points and segments.
public void Clear()
ConnectPoints(long, long, bool)
Creates a segment between the given points. If bidirectional
is false
, only movement from id
to toId
is allowed, not the reverse direction.
var astar = new AStar3D();
astar.AddPoint(1, new Vector3(1, 1, 0));
astar.AddPoint(2, new Vector3(0, 5, 0));
astar.ConnectPoints(1, 2, false);
public void ConnectPoints(long id, long toId, bool bidirectional = true)
Parameters
DisconnectPoints(long, long, bool)
Deletes the segment between the given points. If bidirectional
is false
, only movement from id
to toId
is prevented, and a unidirectional segment possibly remains.
public void DisconnectPoints(long id, long toId, bool bidirectional = true)
Parameters
GetAvailablePointId()
Returns the next available point ID with no point associated to it.
public long GetAvailablePointId()
Returns
GetClosestPoint(Vector3, bool)
Returns the ID of the closest point to toPosition
, 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 toPosition
, the one with the smallest ID will be returned, ensuring a deterministic result.
public long GetClosestPoint(Vector3 toPosition, bool includeDisabled = false)
Parameters
Returns
GetClosestPositionInSegment(Vector3)
Returns the closest position to toPosition
that resides inside a segment between two connected points.
var astar = new AStar3D();
astar.AddPoint(1, new Vector3(0, 0, 0));
astar.AddPoint(2, new Vector3(0, 5, 0));
astar.ConnectPoints(1, 2);
Vector3 res = astar.GetClosestPositionInSegment(new 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.
public Vector3 GetClosestPositionInSegment(Vector3 toPosition)
Parameters
toPosition
Vector3
Returns
GetIdPath(long, long)
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.
var astar = new AStar3D();
astar.AddPoint(1, new Vector3(0, 0, 0));
astar.AddPoint(2, new Vector3(0, 1, 0), 1); // Default weight is 1
astar.AddPoint(3, new Vector3(1, 1, 0));
astar.AddPoint(4, new Vector3(2, 0, 0));
astar.ConnectPoints(1, 2, false);
astar.ConnectPoints(2, 3, false);
astar.ConnectPoints(4, 3, false);
astar.ConnectPoints(1, 4, false);
int[] res = astar.GetIdPath(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.
public long[] GetIdPath(long fromId, long toId)
Parameters
Returns
- long[]
GetPointCapacity()
Returns the capacity of the structure backing the points, useful in conjunction with ReserveSpace(long).
public long GetPointCapacity()
Returns
GetPointConnections(long)
Returns an array with the IDs of the points that form the connection with the given point.
var astar = new AStar3D();
astar.AddPoint(1, new Vector3(0, 0, 0));
astar.AddPoint(2, new Vector3(0, 1, 0));
astar.AddPoint(3, new Vector3(1, 1, 0));
astar.AddPoint(4, new Vector3(2, 0, 0));
astar.ConnectPoints(1, 2, true);
astar.ConnectPoints(1, 3, true);
int[] neighbors = astar.GetPointConnections(1); // Returns [2, 3]
public long[] GetPointConnections(long id)
Parameters
id
long
Returns
- long[]
GetPointCount()
Returns the number of points currently in the points pool.
public long GetPointCount()
Returns
GetPointIds()
Returns an array of all point IDs.
public long[] GetPointIds()
Returns
- long[]
GetPointPath(long, long)
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.
Note: This method is not thread-safe. If called from a GodotThread, it will return an empty Vector3[] and will print an error message.
public Vector3[] GetPointPath(long fromId, long toId)
Parameters
Returns
- Vector3[]
GetPointPosition(long)
Returns the position of the point associated with the given id
.
public Vector3 GetPointPosition(long id)
Parameters
id
long
Returns
GetPointWeightScale(long)
Returns the weight scale of the point associated with the given id
.
public float GetPointWeightScale(long id)
Parameters
id
long
Returns
HasGodotClassMethod(in godot_string_name)
Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.
protected override bool HasGodotClassMethod(in godot_string_name method)
Parameters
method
godot_string_nameName of the method to check for.
Returns
HasGodotClassSignal(in godot_string_name)
Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.
protected override bool HasGodotClassSignal(in godot_string_name signal)
Parameters
signal
godot_string_nameName of the signal to check for.
Returns
HasPoint(long)
Returns whether a point associated with the given id
exists.
public bool HasPoint(long id)
Parameters
id
long
Returns
InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)
Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.
protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)
Parameters
method
godot_string_nameName of the method to invoke.
args
NativeVariantPtrArgsArguments to use with the invoked method.
ret
godot_variantValue returned by the invoked method.
Returns
IsPointDisabled(long)
Returns whether a point is disabled or not for pathfinding. By default, all points are enabled.
public bool IsPointDisabled(long id)
Parameters
id
long
Returns
RemovePoint(long)
Removes the point associated with the given id
from the points pool.
public void RemovePoint(long id)
Parameters
id
long
ReserveSpace(long)
Reserves space internally for numNodes
points. Useful if you're adding a known large number of points at once, such as points on a grid. New capacity must be greater or equals to old capacity.
public void ReserveSpace(long numNodes)
Parameters
numNodes
long
SetPointDisabled(long, bool)
Disables or enables the specified point for pathfinding. Useful for making a temporary obstacle.
public void SetPointDisabled(long id, bool disabled = true)
Parameters
SetPointPosition(long, Vector3)
Sets the position
for the point with the given id
.
public void SetPointPosition(long id, Vector3 position)
Parameters
SetPointWeightScale(long, float)
Sets the weightScale
for the point with the given id
. The weightScale
is multiplied by the result of _ComputeCost(long, long) when determining the overall cost of traveling across a segment from a neighboring point to this point.
public void SetPointWeightScale(long id, float weightScale)
Parameters
_ComputeCost(long, long)
Called when computing the cost between two connected points.
Note that this function is hidden in the default AStar3D class.
public virtual float _ComputeCost(long fromId, long toId)
Parameters
Returns
_EstimateCost(long, long)
Called when estimating the cost between a point and the path's ending point.
Note that this function is hidden in the default AStar3D class.
public virtual float _EstimateCost(long fromId, long toId)