Documentation
¶
Overview ¶
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:
package main import ( "fmt" "graphics.gd/classdb/AStarGrid2D" "graphics.gd/variant/Rect2i" "graphics.gd/variant/Vector2" "graphics.gd/variant/Vector2i" ) func SetupGrid() { var astar_grid = AStarGrid2D.New() astar_grid.SetRegion(Rect2i.New(0, 0, 32, 32)) astar_grid.SetCellSize(Vector2.New(16, 16)) astar_grid.Update() fmt.Print(astar_grid.GetIdPath(Vector2i.New(0, 0), Vector2i.New(3, 4))) // Prints [(0, 0), (1, 1), (2, 2), (3, 3), (3, 4)] fmt.Print(astar_grid.GetPointPath(Vector2i.New(0, 0), Vector2i.New(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 SetPointSolid.
Index ¶
- type Advanced
- type Any
- type CellShape
- type DiagonalMode
- type Expanded
- type Extension
- type Heuristic
- type ID
- type Implementation
- type Instance
- func (self Instance) AsAStarGrid2D() Instance
- func (self Instance) AsObject() [1]gd.Object
- func (self Instance) AsRefCounted() [1]gd.RefCounted
- func (self Instance) CellShape() CellShape
- func (self Instance) CellSize() Vector2.XY
- func (self Instance) Clear()
- func (self Instance) DefaultComputeHeuristic() Heuristic
- func (self Instance) DefaultEstimateHeuristic() Heuristic
- func (self Instance) DiagonalMode() DiagonalMode
- func (self Instance) FillSolidRegion(region Rect2i.PositionSize)
- func (self Instance) FillWeightScaleRegion(region Rect2i.PositionSize, weight_scale Float.X)
- func (self Instance) GetIdPath(from_id Point, to_id Point) []Point
- func (self Instance) GetPointDataInRegion(region Rect2i.PositionSize) []PointData
- func (self Instance) GetPointPath(from_id Point, to_id Point) []Vector2.XY
- func (self Instance) GetPointPosition(id Point) Vector2.XY
- func (self Instance) GetPointWeightScale(id Point) Float.X
- func (self Instance) ID() ID
- func (self Instance) IsDirty() bool
- func (self Instance) IsInBounds(x int, y int) bool
- func (self Instance) IsInBoundsv(id Point) bool
- func (self Instance) IsPointSolid(id Point) bool
- func (self Instance) JumpingEnabled() bool
- func (self Instance) MoreArgs() MoreArgs
- func (self Instance) Offset() Vector2.XY
- func (self Instance) Region() Rect2i.PositionSize
- func (self Instance) SetCellShape(value CellShape)
- func (self Instance) SetCellSize(value Vector2.XY)
- func (self Instance) SetDefaultComputeHeuristic(value Heuristic)
- func (self Instance) SetDefaultEstimateHeuristic(value Heuristic)
- func (self Instance) SetDiagonalMode(value DiagonalMode)
- func (self Instance) SetJumpingEnabled(value bool)
- func (self *Instance) SetObject(obj [1]gd.Object) bool
- func (self Instance) SetOffset(value Vector2.XY)
- func (self Instance) SetPointSolid(id Point)
- func (self Instance) SetPointWeightScale(id Point, weight_scale Float.X)
- func (self Instance) SetRegion(value Rect2i.PositionSize)
- func (self Instance) SetSize(value Vector2i.XY)
- func (self Instance) Size() Vector2i.XY
- func (self Instance) Update()
- func (self Instance) Virtual(name string) reflect.Value
- type Interface
- type MoreArgs
- func (self MoreArgs) FillSolidRegion(region Rect2i.PositionSize, solid bool)
- func (self MoreArgs) GetIdPath(from_id Point, to_id Point, allow_partial_path bool) []Point
- func (self MoreArgs) GetPointPath(from_id Point, to_id Point, allow_partial_path bool) []Vector2.XY
- func (self MoreArgs) SetPointSolid(id Point, solid bool)
- type Point
- type PointData
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Advanced ¶
type Advanced = class
Advanced exposes a 1:1 low-level instance of the class, undocumented, for those who know what they are doing.
type CellShape ¶
type CellShape int //gd:AStarGrid2D.CellShape
const ( // Rectangular cell shape. CellShapeSquare CellShape = 0 // Diamond cell shape (for isometric look). Cell coordinates layout where the horizontal axis goes up-right, and the vertical one goes down-right. CellShapeIsometricRight CellShape = 1 // Diamond cell shape (for isometric look). Cell coordinates layout where the horizontal axis goes down-right, and the vertical one goes down-left. CellShapeIsometricDown CellShape = 2 // Represents the size of the [CellShape] enum. CellShapeMax CellShape = 3 )
type DiagonalMode ¶
type DiagonalMode int //gd:AStarGrid2D.DiagonalMode
const ( // The pathfinding algorithm will ignore solid neighbors around the target cell and allow passing using diagonals. DiagonalModeAlways DiagonalMode = 0 // The pathfinding algorithm will ignore all diagonals and the way will be always orthogonal. DiagonalModeNever DiagonalMode = 1 // The pathfinding algorithm will avoid using diagonals if at least two obstacles have been placed around the neighboring cells of the specific path segment. DiagonalModeAtLeastOneWalkable DiagonalMode = 2 // The pathfinding algorithm will avoid using diagonals if any obstacle has been placed around the neighboring cells of the specific path segment. DiagonalModeOnlyIfNoObstacles DiagonalMode = 3 // Represents the size of the [DiagonalMode] enum. DiagonalModeMax DiagonalMode = 4 )
type Extension ¶
Extension can be embedded in a new struct to create an extension of this class. T should be the type that is embedding this [Extension]See Interface for methods that can be overridden by T.
func (*Extension[T]) AsAStarGrid2D ¶
func (*Extension[T]) AsRefCounted ¶
func (self *Extension[T]) AsRefCounted() [1]gd.RefCounted
type Heuristic ¶
type Heuristic int //gd:AStarGrid2D.Heuristic
const ( // 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). // // [AStar2D]: https://pkg.go.dev/graphics.gd/classdb/AStar2D // [AStar3D]: https://pkg.go.dev/graphics.gd/classdb/AStar3D // [Euclidean heuristic]: https://en.wikipedia.org/wiki/Euclidean_distance HeuristicEuclidean Heuristic = 0 // 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 [DiagonalMode] to [DiagonalModeNever]. // // [DiagonalMode]: https://pkg.go.dev/graphics.gd/classdb/#Instance.DiagonalMode // [Manhattan heuristic]: https://en.wikipedia.org/wiki/Taxicab_geometry HeuristicManhattan Heuristic = 1 // 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; // // HeuristicOctile Heuristic = 2 // 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) // // // // [Chebyshev heuristic]: https://en.wikipedia.org/wiki/Chebyshev_distance HeuristicChebyshev Heuristic = 3 // Represents the size of the [Heuristic] enum. HeuristicMax Heuristic = 4 )
type ID ¶
ID is a typed object ID (reference) to an instance of this class, use it to store references to objects with unknown lifetimes, as an ID will not panic on use if the underlying object has been destroyed.
type Implementation ¶
type Implementation = implementation
Implementation implements Interface with empty methods.
type Instance ¶
type Instance [1]gdclass.AStarGrid2D
Instance of the class with convieniently typed arguments and results.
var Nil Instance
Nil is a nil/null instance of the class. Equivalent to the zero value.
func (Instance) AsAStarGrid2D ¶
func (Instance) AsRefCounted ¶
func (self Instance) AsRefCounted() [1]gd.RefCounted
func (Instance) Clear ¶
func (self Instance) Clear()
Clears the grid and sets the Region to Rect2i(0, 0, 0, 0).
func (Instance) DefaultComputeHeuristic ¶
func (Instance) DefaultEstimateHeuristic ¶
func (Instance) DiagonalMode ¶
func (self Instance) DiagonalMode() DiagonalMode
func (Instance) FillSolidRegion ¶
func (self Instance) FillSolidRegion(region Rect2i.PositionSize)
Fills the given 'region' on the grid with the specified value for the solid flag.
Note: Calling Update is not needed after the call of this function.
func (Instance) FillWeightScaleRegion ¶
func (self Instance) FillWeightScaleRegion(region Rect2i.PositionSize, weight_scale Float.X)
Fills the given 'region' on the grid with the specified value for the weight scale.
Note: Calling Update is not needed after the call of this function.
func (Instance) GetIdPath ¶
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.
If there is no valid path to the target, and 'allow_partial_path' is true, returns a path to the point closest to the target that can be reached.
Note: When 'allow_partial_path' is true and 'to_id' is solid the search may take an unusually long time to finish.
func (Instance) GetPointDataInRegion ¶
func (self Instance) GetPointDataInRegion(region Rect2i.PositionSize) []PointData
Returns an array of dictionaries with point data (id: Vector2i.XY, position: Vector2.XY, solid: bool, weight_scale: Float.X) within a 'region'.
func (Instance) GetPointPath ¶
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.
If there is no valid path to the target, and 'allow_partial_path' is true, returns a path to the point closest to the target that can be reached.
Note: This method is not thread-safe; it can only be used from a single Thread at a given time. Consider using Mutex to ensure exclusive access to one thread to avoid race conditions.
Additionally, when 'allow_partial_path' is true and 'to_id' is solid the search may take an unusually long time to finish.
func (Instance) GetPointPosition ¶
Returns the position of the point associated with the given 'id'.
func (Instance) GetPointWeightScale ¶
Returns the weight scale of the point associated with the given 'id'.
func (Instance) IsDirty ¶
Indicates that the grid parameters were changed and Update needs to be called.
func (Instance) IsInBounds ¶
Returns true if the 'x' and 'y' is a valid grid coordinate (id), i.e. if it is inside Region. Equivalent to region.has_point(Vector2i(x, y)).
func (Instance) IsInBoundsv ¶
Returns true if the 'id' vector is a valid grid coordinate, i.e. if it is inside Region. Equivalent to region.has_point(id).
func (Instance) IsPointSolid ¶
Returns true if a point is disabled for pathfinding. By default, all points are enabled.
func (Instance) JumpingEnabled ¶
func (Instance) MoreArgs ¶
MoreArgs enables certain functions to be called with additional 'optional' arguments.
func (Instance) Region ¶
func (self Instance) Region() Rect2i.PositionSize
func (Instance) SetCellShape ¶
func (Instance) SetCellSize ¶
func (Instance) SetDefaultComputeHeuristic ¶
func (Instance) SetDefaultEstimateHeuristic ¶
func (Instance) SetDiagonalMode ¶
func (self Instance) SetDiagonalMode(value DiagonalMode)
func (Instance) SetJumpingEnabled ¶
func (Instance) SetPointSolid ¶
Disables or enables the specified point for pathfinding. Useful for making an obstacle. By default, all points are enabled.
Note: Calling Update is not needed after the call of this function.
func (Instance) SetPointWeightScale ¶
Sets the 'weight_scale' for the point with the given 'id'. The 'weight_scale' is multiplied by the result of ComputeCost when determining the overall cost of traveling across a segment from a neighboring point to this point.
Note: Calling Update is not needed after the call of this function.
func (Instance) SetRegion ¶
func (self Instance) SetRegion(value Rect2i.PositionSize)
func (Instance) Update ¶
func (self Instance) 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 or Offset are changed. IsDirty will return true if this is the case and this needs to be called.
Note: All point data (solidity and weight scale) will be cleared.
type Interface ¶
type Interface interface { // 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. // // [AStarGrid2D]: https://pkg.go.dev/graphics.gd/classdb/AStarGrid2D EstimateCost(from_id Point, end_id Point) Float.X // Called when computing the cost between two connected points. // // Note that this function is hidden in the default [AStarGrid2D] class. // // [AStarGrid2D]: https://pkg.go.dev/graphics.gd/classdb/AStarGrid2D ComputeCost(from_id Point, to_id Point) Float.X }
type MoreArgs ¶
type MoreArgs [1]gdclass.AStarGrid2D
MoreArgs is a container for Instance functions with additional 'optional' arguments.
func (MoreArgs) FillSolidRegion ¶
func (self MoreArgs) FillSolidRegion(region Rect2i.PositionSize, solid bool)
Fills the given 'region' on the grid with the specified value for the solid flag.
Note: Calling Update is not needed after the call of this function.
func (MoreArgs) GetIdPath ¶
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.
If there is no valid path to the target, and 'allow_partial_path' is true, returns a path to the point closest to the target that can be reached.
Note: When 'allow_partial_path' is true and 'to_id' is solid the search may take an unusually long time to finish.
func (MoreArgs) GetPointPath ¶
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.
If there is no valid path to the target, and 'allow_partial_path' is true, returns a path to the point closest to the target that can be reached.
Note: This method is not thread-safe; it can only be used from a single Thread at a given time. Consider using Mutex to ensure exclusive access to one thread to avoid race conditions.
Additionally, when 'allow_partial_path' is true and 'to_id' is solid the search may take an unusually long time to finish.