AStarGrid2D

Inherits: RefCounted < Object

A* (or "A-Star") pathfinding tailored to find the shortest paths on 2D grids.

Description

Compared to AStar2D you don't need to manually create points or connect them together. It also supports multiple type of heuristics and modes for diagonal movement. This class also provides a jumping mode which is faster to calculate than without it in the AStar2D class.

In contrast to AStar2D, you only need set the size of the grid, optionally set the cell_size and then call the update method:

var astar_grid = AStarGrid2D.new()
astar_grid.size = Vector2i(32, 32)
astar_grid.cell_size = Vector2(16, 16)
astar_grid.update()
print(astar_grid.get_id_path(Vector2i(0, 0), Vector2i(3, 4))) # prints (0, 0), (1, 1), (2, 2), (3, 3), (3, 4)
print(astar_grid.get_point_path(Vector2i(0, 0), Vector2i(3, 4))) # prints (0, 0), (16, 16), (32, 32), (48, 48), (48, 64)

Properties

Vector2

cell_size

Vector2(1, 1)

Heuristic

default_heuristic

0

DiagonalMode

diagonal_mode

0

bool

jumping_enabled

false

Vector2

offset

Vector2(0, 0)

Vector2i

size

Vector2i(0, 0)

Methods

float

_compute_cost ( Vector2i from_id, Vector2i to_id ) virtual const

float

_estimate_cost ( Vector2i from_id, Vector2i to_id ) virtual const

void

clear ( )

PackedVector2Array

get_id_path ( Vector2i from_id, Vector2i to_id )

PackedVector2Array

get_point_path ( Vector2i from_id, Vector2i to_id )

bool

is_dirty ( ) const

bool

is_in_bounds ( int x, int y ) const

bool

is_in_boundsv ( Vector2i id ) const

bool

is_point_solid ( Vector2i id ) const

void

set_point_solid ( Vector2i id, bool solid=true )

void

update ( )

Enumerations

enum Heuristic:

  • HEURISTIC_EUCLIDEAN = 0 --- The Euclidean heuristic to be used for the pathfinding using the following formula:

dx = abs(to_id.x - from_id.x)
dy = abs(to_id.y - from_id.y)
result = sqrt(dx * dx + dy * dy)
  • HEURISTIC_MANHATTAN = 1 --- The Manhattan heuristic to be used for the pathfinding using the following formula:

dx = abs(to_id.x - from_id.x)
dy = abs(to_id.y - from_id.y)
result = dx + dy
  • HEURISTIC_OCTILE = 2 --- The Octile heuristic to be used for the pathfinding using the following formula:

dx = abs(to_id.x - from_id.x)
dy = abs(to_id.y - from_id.y)
f = sqrt(2) - 1
result = (dx < dy) ? f * dx + dy : f * dy + dx;
  • HEURISTIC_CHEBYSHEV = 3 --- The Chebyshev heuristic to be used for the pathfinding using the following formula:

dx = abs(to_id.x - from_id.x)
dy = abs(to_id.y - from_id.y)
result = max(dx, dy)
  • HEURISTIC_MAX = 4 --- Represents the size of the Heuristic enum.


enum DiagonalMode:

  • DIAGONAL_MODE_ALWAYS = 0 --- The pathfinding algorithm will ignore solid neighbors around the target cell and allow passing using diagonals.

  • DIAGONAL_MODE_NEVER = 1 --- The pathfinding algorithm will ignore all diagonals and the way will be always orthogonal.

  • DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE = 2 --- The pathfinding algorithm will avoid using diagonals if at least two obstacles have been placed around the neighboring cells of the specific path segment.

  • DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES = 3 --- The pathfinding algorithm will avoid using diagonals if any obstacle has been placed around the neighboring cells of the specific path segment.

  • DIAGONAL_MODE_MAX = 4 --- Represents the size of the DiagonalMode enum.

Property Descriptions

Default

Vector2(1, 1)

Setter

set_cell_size(value)

Getter

get_cell_size()

The size of the point cell which will be applied to calculate the resulting point position returned by get_point_path. If changed, update needs to be called before finding the next path.


Default

0

Setter

set_default_heuristic(value)

Getter

get_default_heuristic()

The default Heuristic which will be used to calculate the path if _compute_cost and/or _estimate_cost were not overridden.


Default

0

Setter

set_diagonal_mode(value)

Getter

get_diagonal_mode()

A specific DiagonalMode mode which will force the path to avoid or accept the specified diagonals.


  • bool jumping_enabled

Default

false

Setter

set_jumping_enabled(value)

Getter

is_jumping_enabled()

Enables or disables jumping to skip up the intermediate points and speeds up the searching algorithm.


Default

Vector2(0, 0)

Setter

set_offset(value)

Getter

get_offset()

The offset of the grid which will be applied to calculate the resulting point position returned by get_point_path. If changed, update needs to be called before finding the next path.


Default

Vector2i(0, 0)

Setter

set_size(value)

Getter

get_size()

The size of the grid (number of cells of size cell_size on each axis). If changed, update needs to be called before finding the next path.

Method Descriptions

Called when computing the cost between two connected points.

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


Called when estimating the cost between a point and the path's ending point.

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


  • void clear ( )

Clears the grid and sets the size to Vector2i.ZERO.


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.


Returns an array with the points that are in the path found by AStarGrid2D 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 PackedVector3Array and will print an error message.


  • bool is_dirty ( ) const

Indicates that the grid parameters were changed and update needs to be called.


Returns true if the x and y is a valid grid coordinate (id).


Returns true if the id vector is a valid grid coordinate.


Returns true if a point is disabled for pathfinding. By default, all points are enabled.


Disables or enables the specified point for pathfinding. Useful for making an obstacle. By default, all points are enabled.


  • void update ( )

Updates the internal state of the grid according to the parameters to prepare it to search the path. Needs to be called if parameters like size, cell_size or offset are changed. is_dirty will return true if this is the case and this needs to be called.