AStar2D
An implementation of A* for finding the shortest path between two vertices on a connected graph in 2D space.
AStar2D.swift:14class AStar2D
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.
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.
Type members
Instance members
func addPoint(id: Int, position: Vector2, weightScale: Double
) Adds a new point at the given position with the given identifier. The
id
must be 0 or larger, and theweightScale
must be 0.0 or greater.func arePointsConnected(id: Int, toId: Int, bidirectional: Bool
) -> Bool Returns whether there is a connection/segment between the given points. If
bidirectional
isfalse
, returns whether movement fromid
totoId
is possible through this segment.func clear(
) Clears all the points and segments.
func connectPoints(id: Int, toId: Int, bidirectional: Bool
) Creates a segment between the given points. If
bidirectional
isfalse
, only movement fromid
totoId
is allowed, not the reverse direction.func disconnectPoints(id: Int, toId: Int, bidirectional: Bool
) Deletes the segment between the given points. If
bidirectional
isfalse
, only movement fromid
totoId
is prevented, and a unidirectional segment possibly remains.func getAvailablePointId(
) -> Int Returns the next available point ID with no point associated to it.
func getClosestPoint(toPosition: Vector2, includeDisabled: Bool
) -> Int Returns the ID of the closest point to
toPosition
, optionally taking disabled points into account. Returns-1
if there are no points in the points pool.func getClosestPositionInSegment(toPosition: Vector2
) -> Vector2 Returns the closest position to
toPosition
that resides inside a segment between two connected points.func getIdPath(fromId: Int, toId: Int
) -> PackedInt64Array 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 getPointCapacity(
) -> Int Returns the capacity of the structure backing the points, useful in conjunction with
reserveSpace(numNodes:)
.func getPointConnections(id: Int
) -> PackedInt64Array Returns an array with the IDs of the points that form the connection with the given point.
func getPointCount(
) -> Int Returns the number of points currently in the points pool.
func getPointIds(
) -> PackedInt64Array Returns an array of all point IDs.
func getPointPath(fromId: Int, toId: Int
) -> PackedVector2Array Returns an array with the points that are in 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 getPointPosition(id: Int
) -> Vector2 Returns the position of the point associated with the given
id
.func getPointWeightScale(id: Int
) -> Double Returns the weight scale of the point associated with the given
id
.func hasPoint(id: Int
) -> Bool Returns whether a point associated with the given
id
exists.func isPointDisabled(id: Int
) -> Bool Returns whether a point is disabled or not for pathfinding. By default, all points are enabled.
func removePoint(id: Int
) Removes the point associated with the given
id
from the points pool.func reserveSpace(numNodes: Int
) Reserves space internally for
numNodes
points, useful if you’re adding a known large number of points at once, such as points on a grid. New capacity must be greater or equals to old capacity.func setPointDisabled(id: Int, disabled: Bool
) Disables or enables the specified point for pathfinding. Useful for making a temporary obstacle.
func setPointPosition(id: Int, position: Vector2
) Sets the
position
for the point with the givenid
.func setPointWeightScale(id: Int, 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.
Show implementation details (2)
Hide implementation details
func _computeCost(fromId: Int, toId: Int
) -> Double Called when computing the cost between two connected points.
func _estimateCost(fromId: Int, toId: Int
) -> Double Called when estimating the cost between a point and the path’s ending point.