AStarGrid2D
An implementation of A* for finding the shortest path between two points on a partial 2D grid.
AStarGrid2D.swift:22class AStarGrid2D
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 cellSize
, and then call the update
method:
To remove a point from the pathfinding grid, it must be set as “solid” with setPointSolid(_:solid:)
.
Superclasses
class RefCounted
Base class for reference-counted objects.
Citizens in SwiftGodot
Conformances
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Equatable
A type that can be compared for value equality.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol Identifiable<ID>
A class of types whose instances hold the value of an entity with stable identity.
protocol VariantRepresentable
Types that conform to VariantRepresentable can be stored directly in
Variant
with no conversion. These include all of the Variant types from Godot (for exampleGString
,Rect
,Plane
), Godot objects (those that subclass SwiftGodot.Object) as well as the built-in Swift types UInt8, Int64 and Double.protocol VariantStorable
Types that conform to VariantStorable can be stored in a Variant and can be extracted back out of a Variant.
Types
Type members
Instance members
var cellShape: AStarGrid2D.CellShape
The cell shape. Affects how the positions are placed in the grid. If changed,
update
needs to be called before finding the next path.var cellSize: Vector2
The size of the point cell which will be applied to calculate the resulting point position returned by
getPointPath(fromId:toId:allowPartialPath:)
. If changed,update
needs to be called before finding the next path.var defaultComputeHeuristic: AStarGrid2D.Heuristic
The default
Heuristic
which will be used to calculate the cost between two points if_computeCost(fromId:toId:)
was not overridden.var defaultEstimateHeuristic: AStarGrid2D.Heuristic
The default
Heuristic
which will be used to calculate the cost between the point and the end point if_estimateCost(fromId:toId:)
was not overridden.var diagonalMode: AStarGrid2D.DiagonalMode
A specific
DiagonalMode
mode which will force the path to avoid or accept the specified diagonals.var jumpingEnabled: Bool
Enables or disables jumping to skip up the intermediate points and speeds up the searching algorithm.
var offset: Vector2
The offset of the grid which will be applied to calculate the resulting point position returned by
getPointPath(fromId:toId:allowPartialPath:)
. If changed,update
needs to be called before finding the next path.var region: Rect2i
The region of grid cells available for pathfinding. If changed,
update
needs to be called before finding the next path.var size: Vector2i
The size of the grid (number of cells of size
cellSize
on each axis). If changed,update
needs to be called before finding the next path.func clear(
) Clears the grid and sets the
region
toRect2i(0, 0, 0, 0)
.func fillSolidRegion(Rect2i, solid: Bool
) Fills the given
region
on the grid with the specified value for the solid flag.func fillWeightScaleRegion(Rect2i, weightScale: Double
) Fills the given
region
on the grid with the specified value for the weight scale.func getIdPath(fromId: Vector2i, toId: Vector2i, allowPartialPath: Bool
) -> VariantCollection<Vector2i> 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.
func getPointPath(fromId: Vector2i, toId: Vector2i, allowPartialPath: Bool
) -> PackedVector2Array 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.func getPointPosition(id: Vector2i
) -> Vector2 Returns the position of the point associated with the given
id
.func getPointWeightScale(id: Vector2i
) -> Double Returns the weight scale of the point associated with the given
id
.func isDirty(
) -> Bool Indicates that the grid parameters were changed and
update
needs to be called.func isInBounds(x: Int32, y: Int32
) -> Bool Returns
true
if thex
andy
is a valid grid coordinate (id), i.e. if it is insideregion
. Equivalent toregion.has_point(Vector2i(x, y))
.func isInBoundsv(id: Vector2i
) -> Bool Returns
true
if theid
vector is a valid grid coordinate, i.e. if it is insideregion
. Equivalent toregion.has_point(id)
.func isPointSolid(id: Vector2i
) -> Bool Returns
true
if a point is disabled for pathfinding. By default, all points are enabled.func setPointSolid(id: Vector2i, solid: Bool
) Disables or enables the specified point for pathfinding. Useful for making an obstacle. By default, all points are enabled.
func setPointWeightScale(id: Vector2i, weightScale: Double
) Sets the
weightScale
for the point with the givenid
. TheweightScale
is multiplied by the result of_computeCost(fromId:toId:)
when determining the overall cost of traveling across a segment from a neighboring point to this point.func 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
region
,cellSize
oroffset
are changed.isDirty
will returntrue
if this is the case and this needs to be called.
Show implementation details (2)
Hide implementation details
func _computeCost(fromId: Vector2i, toId: Vector2i
) -> Double Called when computing the cost between two connected points.
func _estimateCost(fromId: Vector2i, toId: Vector2i
) -> Double Called when estimating the cost between a point and the path’s ending point.