Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

AStar2D

Inherits: RefCounted < Object

An implementation of A* for finding the shortest path between two vertices on a connected graph in 2D space.

Description

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.

Methods

float

_compute_cost ( int from_id, int to_id ) virtual const

float

_estimate_cost ( int from_id, int to_id ) virtual const

void

add_point ( int id, Vector2 position, float weight_scale=1.0 )

bool

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 )

int

get_available_point_id ( ) const

int

get_closest_point ( Vector2 to_position, bool include_disabled=false ) const

Vector2

get_closest_position_in_segment ( Vector2 to_position ) const

PackedInt64Array

get_id_path ( int from_id, int to_id )

int

get_point_capacity ( ) const

PackedInt64Array

get_point_connections ( int id )

int

get_point_count ( ) const

PackedInt64Array

get_point_ids ( )

PackedVector2Array

get_point_path ( int from_id, int to_id )

Vector2

get_point_position ( int id ) const

float

get_point_weight_scale ( int id ) const

bool

has_point ( int id ) const

bool

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, Vector2 position )

void

set_point_weight_scale ( int id, float weight_scale )


Method Descriptions

float _compute_cost ( int from_id, int to_id ) virtual const

Called when computing the cost between two connected points.

Note that this function is hidden in the default AStar2D class.


float _estimate_cost ( int from_id, int to_id ) virtual const

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.


void add_point ( int id, Vector2 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_scales to form a path.

var astar = AStar2D.new()
astar.add_point(1, 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.


bool are_points_connected ( int id, int to_id, bool bidirectional=true ) const

Returns whether there is a connection/segment between the given points. 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 = AStar2D.new()
astar.add_point(1, Vector2(1, 1))
astar.add_point(2, Vector2(0, 5))
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 ( Vector2 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.


Vector2 get_closest_position_in_segment ( Vector2 to_position ) const

Returns the closest position to to_position that resides inside a segment between two connected points.