Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
AStarGrid2D¶
Inherits: RefCounted < Object
An implementation of A* for finding the shortest path between two points on a partial 2D grid.
描述¶
AStarGrid2D is a variant of AStar2D that is specialized for partial 2D grids. It is simpler to use because it doesn't require you to manually create points and connect them together. This class also supports multiple types of heuristics, modes for diagonal movement, and a jumping mode to speed up calculations.
To use AStarGrid2D, you only need to set the region of the grid, optionally set the cell_size, and then call the update method:
var astar_grid = AStarGrid2D.new()
astar_grid.region = Rect2i(0, 0, 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)
AStarGrid2D astarGrid = new AStarGrid2D();
astarGrid.Region = new Rect2I(0, 0, 32, 32);
astarGrid.CellSize = new Vector2I(16, 16);
astarGrid.Update();
GD.Print(astarGrid.GetIdPath(Vector2I.Zero, new Vector2I(3, 4))); // prints (0, 0), (1, 1), (2, 2), (3, 3), (3, 4)
GD.Print(astarGrid.GetPointPath(Vector2I.Zero, new Vector2I(3, 4))); // prints (0, 0), (16, 16), (32, 32), (48, 48), (48, 64)
To remove a point from the pathfinding grid, it must be set as "solid" with set_point_solid.
属性¶
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
Methods¶
_compute_cost ( Vector2i from_id, Vector2i to_id ) virtual const |
|
_estimate_cost ( Vector2i from_id, Vector2i to_id ) virtual const |
|
void |
clear ( ) |
void |
fill_solid_region ( Rect2i region, bool solid=true ) |
void |
fill_weight_scale_region ( Rect2i region, float weight_scale ) |
get_id_path ( Vector2i from_id, Vector2i to_id ) |
|
get_point_path ( Vector2i from_id, Vector2i to_id ) |
|
get_point_position ( Vector2i id ) const |
|
get_point_weight_scale ( Vector2i id ) const |
|
is_dirty ( ) const |
|
is_in_bounds ( int x, int y ) const |
|
is_in_boundsv ( Vector2i id ) const |
|
is_point_solid ( Vector2i id ) const |
|
void |
set_point_solid ( Vector2i id, bool solid=true ) |
void |
set_point_weight_scale ( Vector2i id, float weight_scale ) |
void |
update ( ) |
Enumerations¶
enum Heuristic:
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)
Note: This is also the internal heuristic used in AStar3D and AStar2D by default (with the inclusion of possible z-axis coordinate).
Heuristic 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
Note: This heuristic is intended to be used with 4-side orthogonal movements, provided by setting the diagonal_mode to DIAGONAL_MODE_NEVER.
Heuristic 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 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 HEURISTIC_MAX = 4
Represents the size of the Heuristic enum.
enum DiagonalMode:
DiagonalMode DIAGONAL_MODE_ALWAYS = 0
The pathfinding algorithm will ignore solid neighbors around the target cell and allow passing using diagonals.
DiagonalMode DIAGONAL_MODE_NEVER = 1
The pathfinding algorithm will ignore all diagonals and the way will be always orthogonal.
DiagonalMode 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.
DiagonalMode 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.
DiagonalMode DIAGONAL_MODE_MAX = 4
Represents the size of the DiagonalMode enum.
Property Descriptions¶
Vector2 cell_size = Vector2(1, 1)
void set_cell_size (