Class AStar2D
- Namespace
- Godot
- Assembly
- GodotSharp.dll
An implementation of the A* algorithm, used to find the shortest path between two vertices on a connected graph in 2D space.
See AStar3D for a more thorough explanation on how to use this class. AStar2D is a wrapper for AStar3D that enforces 2D coordinates.
public class AStar2D : RefCounted, IDisposable
- Inheritance
-
AStar2D
- Implements
- Inherited Members
Constructors
AStar2D()
public AStar2D()
Methods
AddPoint(long, Vector2, 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 AStar2D();
astar.AddPoint(1, new Vector2(1, 0), 4); // Adds the point (1, 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, Vector2 position, float weightScale = 1)
Parameters
ArePointsConnected(long, long, bool)
Returns whether there is a connection/segment between the given points. 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 AStar2D();
astar.AddPoint(1, new Vector2(1, 1));
astar.AddPoint(2, new Vector2(0, 5));
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(Vector2, 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(Vector2 toPosition, bool includeDisabled = false)
Parameters
Returns
GetClosestPositionInSegment(Vector2)
Returns the closest position to toPosition
that resides inside a segment between two connected points.
var astar = new AStar2D();
astar.AddPoint(1, new Vector2(0, 0));
astar.AddPoint(2, new Vector2(0, 5));
astar.ConnectPoints(1, 2);
Vector2 res = astar.GetClosestPositionInSegment(new Vector2(3, 3)); // Returns (0, 3)
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 Vector2 GetClosestPositionInSegment(Vector2 toPosition)
Parameters
toPosition
Vector2
Returns
GetIdPath(long, long)
Returns an array with the IDs of the points that form the path found by AStar2D between the given points. The array is ordered from the starting point to the ending point of the path.
var astar = new AStar2D();
astar.AddPoint(1, new Vector2(0, 0));
astar.AddPoint(2, new Vector2(0, 1), 1); // Default weight is 1
astar.AddPoint(3, new Vector2(1, 1));
astar.AddPoint(4, new Vector2(2, 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 AStar2D();
astar.AddPoint(1, new Vector2(0, 0));
astar.AddPoint(2, new Vector2(0, 1));
astar.AddPoint(3, new Vector2(1, 1));
astar.AddPoint(4, new Vector2(2, 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 AStar2D 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 Vector2[] and will print an error message.
public Vector2[] GetPointPath(long fromId, long toId)
Parameters
Returns
- Vector2[]
GetPointPosition(long)
Returns the position of the point associated with the given id
.
public Vector2 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, Vector2)
Sets the position
for the point with the given id
.
public void SetPointPosition(long id, Vector2 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 AStar2D 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 AStar2D class.
public virtual float _EstimateCost(long fromId, long toId)