AStarGrid2D

package
v0.0.0-...-535787f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2025 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

graphics.gd/classdb/AStarGrid2D is a variant of graphics.gd/classdb/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 graphics.gd/classdb/AStarGrid2D, you only need to set the Instance.Region of the grid, optionally set the Instance.CellSize, and then call the Instance.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 Instance.SetPointSolid.

Index

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 Any

type Any interface {
	gd.IsClass
	AsAStarGrid2D() Instance
}

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 Expanded

type Expanded [1]gdclass.AStarGrid2D

func (Expanded) FillSolidRegion

func (self Expanded) FillSolidRegion(region Rect2i.PositionSize, solid bool)

Fills the given 'region' on the grid with the specified value for the solid flag.

Note: Calling Instance.Update is not needed after the call of this function.

func (Expanded) GetIdPath

func (self Expanded) GetIdPath(from_id Point, to_id Point, allow_partial_path bool) []Point

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 (Expanded) GetPointPath

func (self Expanded) GetPointPath(from_id Point, to_id Point, allow_partial_path bool) []Vector2.XY

Returns an array with the points that are in the path found by graphics.gd/classdb/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 graphics.gd/classdb/Thread at a given time. Consider using graphics.gd/classdb/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 (Expanded) SetPointSolid

func (self Expanded) SetPointSolid(id Point, solid bool)

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

Note: Calling Instance.Update is not needed after the call of this function.

type Extension

type Extension[T gdclass.Interface] struct{ gdclass.Extension[T, Instance] }

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 (self *Extension[T]) AsAStarGrid2D() Instance

func (*Extension[T]) AsObject

func (self *Extension[T]) AsObject() [1]gd.Object

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 [graphics.gd/classdb/AStar3D] and [graphics.gd/classdb/AStar2D] by default (with the inclusion of possible z-axis coordinate).
	//
	// [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 [Instance.DiagonalMode] to [DiagonalModeNever].
	//
	// [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

type ID Object.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.

func (ID) Instance

func (id ID) Instance() (Instance, bool)

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 New

func New() Instance

func (Instance) AsAStarGrid2D

func (self Instance) AsAStarGrid2D() Instance

func (Instance) AsObject

func (self Instance) AsObject() [1]gd.Object

func (Instance) AsRefCounted

func (self Instance) AsRefCounted() [1]gd.RefCounted

func (Instance) CellShape

func (self Instance) CellShape() CellShape

func (Instance) CellSize

func (self Instance) CellSize() Vector2.XY

func (Instance) Clear

func (self Instance) Clear()

Clears the grid and sets the Instance.Region to Rect2i(0, 0, 0, 0).

func (Instance) DefaultComputeHeuristic

func (self Instance) DefaultComputeHeuristic() Heuristic

func (Instance) DefaultEstimateHeuristic

func (self Instance) DefaultEstimateHeuristic() Heuristic

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 Instance.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 Instance.Update is not needed after the call of this function.

func (Instance) GetIdPath

func (self Instance) GetIdPath(from_id Point, to_id Point) []Point

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

func (self Instance) GetPointPath(from_id Point, to_id Point) []Vector2.XY

Returns an array with the points that are in the path found by graphics.gd/classdb/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 graphics.gd/classdb/Thread at a given time. Consider using graphics.gd/classdb/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

func (self Instance) GetPointPosition(id Point) Vector2.XY

Returns the position of the point associated with the given 'id'.

func (Instance) GetPointWeightScale

func (self Instance) GetPointWeightScale(id Point) Float.X

Returns the weight scale of the point associated with the given 'id'.

func (Instance) ID

func (self Instance) ID() ID

func (Instance) IsDirty

func (self Instance) IsDirty() bool

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

func (Instance) IsInBounds

func (self Instance) IsInBounds(x int, y int) bool

Returns true if the 'x' and 'y' is a valid grid coordinate (id), i.e. if it is inside Instance.Region. Equivalent to region.has_point(Vector2i(x, y)).

func (Instance) IsInBoundsv

func (self Instance) IsInBoundsv(id Point) bool

Returns true if the 'id' vector is a valid grid coordinate, i.e. if it is inside Instance.Region. Equivalent to region.has_point(id).

func (Instance) IsPointSolid

func (self Instance) IsPointSolid(id Point) bool

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

func (Instance) JumpingEnabled

func (self Instance) JumpingEnabled() bool

func (Instance) Offset

func (self Instance) Offset() Vector2.XY

func (Instance) Region

func (self Instance) Region() Rect2i.PositionSize

func (Instance) SetCellShape

func (self Instance) SetCellShape(value CellShape)

func (Instance) SetCellSize

func (self Instance) SetCellSize(value Vector2.XY)

func (Instance) SetDefaultComputeHeuristic

func (self Instance) SetDefaultComputeHeuristic(value Heuristic)

func (Instance) SetDefaultEstimateHeuristic

func (self Instance) SetDefaultEstimateHeuristic(value Heuristic)

func (Instance) SetDiagonalMode

func (self Instance) SetDiagonalMode(value DiagonalMode)

func (Instance) SetJumpingEnabled

func (self Instance) SetJumpingEnabled(value bool)

func (*Instance) SetObject

func (self *Instance) SetObject(obj [1]gd.Object) bool

func (Instance) SetOffset

func (self Instance) SetOffset(value Vector2.XY)

func (Instance) SetPointSolid

func (self Instance) SetPointSolid(id Point)

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

Note: Calling Instance.Update is not needed after the call of this function.

func (Instance) SetPointWeightScale

func (self Instance) SetPointWeightScale(id Point, weight_scale Float.X)

Sets the 'weight_scale' for the point with the given 'id'. The 'weight_scale' is multiplied by the result of [Interface.ComputeCost] when determining the overall cost of traveling across a segment from a neighboring point to this point.

Note: Calling Instance.Update is not needed after the call of this function.

func (Instance) SetRegion

func (self Instance) SetRegion(value Rect2i.PositionSize)

func (Instance) SetSize

func (self Instance) SetSize(value Vector2i.XY)

func (Instance) Size

func (self Instance) Size() Vector2i.XY

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 Instance.Region, Instance.CellSize or Instance.Offset are changed. Instance.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.

func (Instance) Virtual

func (self Instance) Virtual(name string) reflect.Value

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 [graphics.gd/classdb/AStarGrid2D] class.
	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 [graphics.gd/classdb/AStarGrid2D] class.
	ComputeCost(from_id Point, to_id Point) Float.X
}

type Point

type Point Vector2i.XY

type PointData

type PointData struct {
	ID struct {
		X int32
		Y int32
	} `gd:"id"`
	Position struct {
		X float32
		Y float32
	} `gd:"position"`
	Solid       bool    `gd:"solid"`
	WeightScale float32 `gd:"weight_scale"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL