API AStar - shmellyorc/Box GitHub Wiki

AStar

Namespace: Box.Pathfind

Description: Provides a flexible and efficient A* pathfinding system designed for real-time applications. It supports customizable weighted edges with optional dynamic cost modifiers, multiple heuristic functions (Euclidean, Manhattan, Diagonal), and directional graph traversal. Under the hood, it leverages object pooling for vertex lists, path reconstruction maps, and visited sets to minimize garbage collection overhead. Additionally, it features an internal heuristic cache for faster repeated distance calculations and returns detailed path results, including total traversal cost, to facilitate performance-sensitive scenarios.


Constructors

public AStar()

Initializes a new instance of the AStar pathfinding graph.


Properties

Property Type Description
Count int Gets the number of vertices currently in the graph.

Methods

Method Description
AddPoint(int id, Vect2 position) Adds a new vertex to the graph with a specified ID and position. Returns false if the ID already exists.
RemovePoint(int id) Removes a vertex and all its associated edges from the graph. Returns false if the vertex is not found.
ConnectPoints(int id, int toId, float cost = 1f, bool bidirectional = true) Connects two vertices with a weighted edge. Optionally bidirectional. Returns false if vertices are not found or already connected.
DisconnectPoints(int id, int toId) Disconnects two vertices by removing edges between them. Returns false if vertices not found.
ClearPools() Clears all internal object pools and cached heuristic values.
FindPath(int startId, int goalId, HeuristicType heuristicType = HeuristicType.Euclidean, Func<Vertex, Vertex, float> costModifier = null) Finds the shortest path between two vertices using the A* algorithm. Returns a PathResult (path and total cost), or null if no path is found.
ShortestPath(int startId, int goalId, HeuristicType heuristicType = HeuristicType.Euclidean, Func<Vertex, Vertex, float> costModifier = null) Retrieves the shortest path as a list of vertex IDs, or null if none found. Accepts an optional costModifier function to dynamically adjust the cost of traversing each edge, where the function takes the current and target Vertex and returns a cost multiplier.
ShortestPathAsPositions(int startId, int goalId, HeuristicType heuristicType = HeuristicType.Euclidean, Func<Vertex, Vertex, float> costModifier = null) Retrieves the shortest path as a list of vertex positions (Vect2), or null if none found. Accepts an optional costModifier function to dynamically adjust the cost of traversing each edge, where the function takes the current and target Vertex and returns a cost multiplier.
ArePointsConnected(int fromId, int toId) Checks if a direct edge exists between two vertices. Returns false if either vertex is not found.
SetEdgeEnabled(int fromId, int toId, bool enabled) Enables or disables the edge between two vertices. Returns false if vertices are not found.
SetNodeActive(int id, bool active) Activates or deactivates a vertex by its ID. Returns false if the vertex is not found.
TryGetPosition(int id, out Vect2 position) Gets the position of a vertex by its ID. Returns false if the vertex is not found.

Examples

// Create a new A* graph instance
var astar = new AStar();

// Add vertices with unique IDs and positions
astar.AddPoint(1, new Vect2(0, 0));
astar.AddPoint(2, new Vect2(5, 5));

// Connect the vertices with default cost and bidirectional edge
astar.ConnectPoints(1, 2);

// Retrieve the shortest path as positions
List<Vect2> path = astar.ShortestPathAsPositions(1, 2);

if (path != null)
{
    foreach (var position in path)
    {
        Console.WriteLine(position);
    }
}
⚠️ **GitHub.com Fallback** ⚠️