A* Pathfinding Project
3.1.4
The A* Pathfinding Project for Unity 3D
|
Generates a grid of nodes. More...
Classes | |
class | TextureData |
Used for using a texture as a source for a grid graph. More... | |
Public Member Functions | |
void | AddPortal (Node n1, Node n2, List< Vector3 > left, List< Vector3 > right) |
void | AddPortal (GridNode n1, GridNode n2, List< Vector3 > left, List< Vector3 > right) |
void | BuildFunnelCorridor (List< Node > path, int sIndex, int eIndex, List< Vector3 > left, List< Vector3 > right) |
virtual void | CalculateConnections (Node[] nodes, int x, int z, GridNode node) |
Calculates the grid connections for a single node. | |
bool | CheckConnection (GridNode node, int dir) |
Returns if node is connected to it's neighbour in the specified direction. | |
override Node[] | CreateNodes (int number) |
Creates a number of nodes with the correct type for the graph. | |
void | DeSerializeNodes (Node[] nodes, AstarSerializer serializer) |
Deserializes grid graph specific node stuff from the serializer. | |
void | DeSerializeSettings (AstarSerializer serializer) |
virtual void | ErodeWalkableArea () |
Erodes the walkable area. | |
virtual void | ErodeWalkableArea (int xmin, int zmin, int xmax, int zmax) |
Erodes the walkable area. | |
void | GenerateMatrix () |
Generates the matrix used for translating nodes from grid coordinates to world coordintes. | |
void | GetBoundsMinMax (Bounds b, Matrix4x4 matrix, out Vector3 min, out Vector3 max) |
Calculates minimum and maximum points for bounds b when multiplied with the matrix. | |
override NNInfo | GetNearest (Vector3 position, NNConstraint constraint, Node hint) |
override NNInfo | GetNearestForce (Vector3 position, NNConstraint constraint) |
List< Node > | GetNodesInArea (Bounds b) |
All nodes inside the bounding box. | |
List< Node > | GetNodesInArea (GraphUpdateShape shape) |
All nodes inside the shape. | |
virtual bool | IsValidConnection (GridNode n1, GridNode n2) |
Returns true if a connection between the adjacent nodes n1 and n2 is valid. | |
bool | Linecast (Vector3 _a, Vector3 _b) |
Returns if _b is visible from _a on the graph. | |
bool | Linecast (Vector3 _a, Vector3 _b, Node hint) |
Returns if _b is visible from _a on the graph. | |
bool | Linecast (Vector3 _a, Vector3 _b, Node hint, out GraphHitInfo hit) |
Returns if _b is visible from _a on the graph. | |
override void | OnDestroy () |
This function will be called when this graph is destroyed. | |
override void | OnDrawGizmos (bool drawNodes) |
void | OnPostScan (AstarPath script) |
Auto links grid graphs together. | |
override void | PostDeserialization () |
Called after all deserialization has been done for all graphs. | |
void | RemoveGridGraphFromStatic () |
override void | Scan () |
Scans the graph, called from AstarPath.Scan Override this function to implement custom scanning logic | |
void | SerializeNodes (Node[] nodes, AstarSerializer serializer) |
Serializes grid graph specific node stuff to the serializer. | |
void | SerializeSettings (AstarSerializer serializer) |
Serialize Settings. | |
virtual void | SetUpOffsetsAndCosts () |
Sets up neighbourOffsets with the current settings. | |
bool | SnappedLinecast (Vector3 _a, Vector3 _b, Node hint, out GraphHitInfo hit) |
Returns if _b is visible from _a on the graph. | |
void | UpdateArea (GraphUpdateObject o) |
Internal function to update an area of the graph. | |
void | UpdateNodePositionCollision (Node node, int x, int z) |
Updates position, walkability and penalty for the node. | |
void | UpdateSizeFromWidthDepth () |
Updates size from width, depth and nodeSize values. | |
Public Member Functions inherited from NavGraph | |
virtual void | Awake () |
This will be called on the same time as Awake on the gameObject which the AstarPath script is attached to. | |
virtual void | DeserializeExtraInfo (byte[] bytes) |
Deserializes graph type specific node data. | |
NNInfo | GetNearest (Vector3 position) |
Returns the nearest node to a position using the default NNConstraint. | |
NNInfo | GetNearest (Vector3 position, NNConstraint constraint) |
Returns the nearest node to a position using the specified NNConstraint. | |
bool | InSearchTree (Node node, Path path) |
Returns if the node is in the search tree of the path. | |
virtual Color | NodeColor (Node node, NodeRunData data) |
virtual void | RelocateNodes (Matrix4x4 oldMatrix, Matrix4x4 newMatrix) |
Relocates the nodes in this graph. | |
void | SafeOnDestroy () |
SafeOnDestroy should be used when there is a risk that the pathfinding is searching through this graph when called | |
void | ScanGraph () |
Consider using AstarPath.Scan () instead since this function might screw things up if there is more than one graph. | |
virtual byte[] | SerializeExtraInfo () |
Serializes graph type specific node data. | |
Public Member Functions inherited from ISerializableGraph | |
Node[] | CreateNodes (int num) |
void | DeSerializeNodes (Node[] nodes, AstarSerializer serializer) |
void | SerializeNodes (Node[] nodes, AstarSerializer serializer) |
Public Member Functions inherited from ISerializableObject | |
void | DeSerializeSettings (AstarSerializer serializer) |
Called to deserialize the object. | |
void | SerializeSettings (AstarSerializer serializer) |
Called to serialize the object. | |
Public Member Functions inherited from IUpdatableGraph | |
void | UpdateArea (GraphUpdateObject o) |
Updates an area using the specified GraphUpdateObject. | |
Public Member Functions inherited from IFunnelGraph | |
void | AddPortal (Node n1, Node n2, List< Vector3 > left, List< Vector3 > right) |
Add the portal between node n1 and n2 to the funnel corridor. | |
void | BuildFunnelCorridor (List< Node > path, int sIndex, int eIndex, List< Vector3 > left, List< Vector3 > right) |
Public Member Functions inherited from IRaycastableGraph | |
bool | Linecast (Vector3 start, Vector3 end) |
bool | Linecast (Vector3 start, Vector3 end, Node hint) |
bool | Linecast (Vector3 start, Vector3 end, Node hint, out GraphHitInfo hit) |
Static Public Member Functions | |
static void | CalculateConnections (GridNode node) |
Calculates the grid connections for a single node. | |
Public Attributes | |
Matrix4x4 | boundsMatrix |
Matrix4x4 | boundsMatrix2 |
int | getNearestForceOverlap = 2 |
In GetNearestForce, determines how far to search after a valid node has been found. | |
int[] | neighbourCosts |
Costs to neighbour nodes. | |
int[] | neighbourOffsets |
Index offset to get neighbour nodes. | |
int[] | neighbourXOffsets |
Offsets in the X direction for neighbour nodes. | |
int[] | neighbourZOffsets |
Offsets in the Z direction for neighbour nodes. | |
int | scans = 0 |
Vector2 | size |
Size of the grid. | |
Public Attributes inherited from NavGraph | |
byte[] | _sguid |
Used to store the guid value. | |
AstarPath | active |
Reference to the AstarPath object in the scene. | |
bool | drawGizmos = true |
bool | infoScreenOpen |
Used in the editor to check if the info screen is open. | |
uint | initialPenalty = 0 |
Matrix4x4 | matrix |
A matrix for translating/rotating/scaling the graph. | |
string | name |
Node[] | nodes |
All nodes this graph contains. | |
bool | open |
Is the graph open in the editor | |
Protected Attributes | |
int[] | corners |
To reduce memory allocations this array is reused. | |
Properties | |
virtual bool | uniformWidhtDepthGrid [get] |
This is placed here so generators inheriting from this one can override it and set it to false. | |
Properties inherited from NavGraph | |
Guid | guid [get, set] |
Used as an ID of the graph, considered to be unique. | |
Matrix4x4 | inverseMatrix [get] |
Private Member Functions | |
List< Node > | GetNodesInArea (Bounds b, GraphUpdateShape shape) |
All nodes inside the shape or if null, the bounding box. | |
Inspector - Settings | |
bool | useRaycastNormal [get] |
Use heigh raycasting normal for max slope calculation. | |
int | width |
Width of the grid in nodes. | |
int | depth |
Depth (height) of the grid in nodes. | |
float | aspectRatio = 1F |
Scaling of the graph along the X axis. | |
Vector3 | rotation |
Rotation of the grid in degrees. | |
Bounds | bounds |
Vector3 | center |
Center point of the grid. | |
Vector2 | unclampedSize |
Size of the grid. | |
float | nodeSize = 1 |
Size of one node in world units. | |
GraphCollision | collision |
Settings on how to check for walkability and height. | |
float | maxClimb = 0.4F |
The max position difference between two nodes to enable a connection. | |
int | maxClimbAxis = 1 |
The axis to use for maxClimb. | |
float | maxSlope = 90 |
The max slope in degrees for a node to be walkable. | |
int | erodeIterations = 0 |
Erosion of the graph. | |
bool | erosionUseTags = false |
Use tags instead of walkability for erosion. | |
int | erosionFirstTag = 1 |
Tag to start from when using tags for erosion. | |
bool | autoLinkGrids = false |
Auto link the graph's edge nodes together with other GridGraphs in the scene on Scan. | |
float | autoLinkDistLimit = 10F |
Distance limit for grid graphs to be auto linked. | |
NumNeighbours | neighbours = NumNeighbours.Eight |
Number of neighbours for each node. | |
bool | cutCorners = true |
If disabled, will not cut corners on obstacles. | |
float | penaltyPositionOffset = 0 |
Offset for the position when calculating penalty. | |
bool | penaltyPosition = false |
Use position (y-coordinate) to calculate penalty. | |
float | penaltyPositionFactor = 1F |
Scale factor for penalty when calculating from position. | |
bool | penaltyAngle = false |
float | penaltyAngleFactor = 100F |
TextureData | textureData = new TextureData () |
Holds settings for using a texture as source for a grid graph. | |
Generates a grid of nodes.
The GridGraph does exactly what the name implies, generates nodes in a grid pattern.
Grid graphs suit well to when you already have a grid based world. Features:
The The Snap Size button snaps the internal size of the graph to exactly contain the current number of nodes, i.e not contain 100.3 nodes but exactly 100 nodes.
This will make the "center" coordinate more accurate.
The Graph During Runtime Any graph which implements the IUpdatableGraph interface can be updated during runtime.
For grid graphs this is a great feature since you can update only a small part of the grid without causing any lag like a complete rescan would.
If you for example just have instantiated a sphere obstacle in the scene and you want to update the grid where that sphere was instantiated, you can do this:
Where ob is the obstacle you just instantiated (a GameObject).
As you can see, the UpdateGraphs function takes a Bounds parameter and it will send an update call to all updateable graphs.
A grid graph will update that area and a small margin around it equal to collision testing diameter/2
|
static |
Calculates the grid connections for a single node.
Convenience function, it's faster to use CalculateConnections(GridNode[],int,int,node) but that will only show when calculating for a large number of nodes
bool CheckConnection | ( | GridNode | node, |
int | dir | ||
) |
Returns if node is connected to it's neighbour in the specified direction.
This will also return true if neighbours = NumNeighbours.Four, the direction is diagonal and one can move through one of the adjacent nodes to the targeted node.
|
virtual |
Creates a number of nodes with the correct type for the graph.
This should not set the nodes array, only return the nodes. Called by graph generators and when deserializing a graph with nodes. Override this function if you do not use the default Pathfinding::Node class.
Reimplemented from NavGraph.
Reimplemented in LayerGridGraph.
void DeSerializeNodes | ( | Node[] | nodes, |
AstarSerializer | serializer | ||
) |
Deserializes grid graph specific node stuff from the serializer.
|
virtual |
|
virtual |
Erodes the walkable area.
xmin, zmin (inclusive)
xmax, zmax (exclusive)
Reimplemented in LayerGridGraph.
void GenerateMatrix | ( | ) |
Generates the matrix used for translating nodes from grid coordinates to world coordintes.
|
virtual |
Reimplemented from NavGraph.
Reimplemented in LayerGridGraph.
|
virtual |
Reimplemented from NavGraph.
Reimplemented in LayerGridGraph.
List<Node> GetNodesInArea | ( | Bounds | b | ) |
All nodes inside the bounding box.
List<Node> GetNodesInArea | ( | GraphUpdateShape | shape | ) |
All nodes inside the shape.
|
private |
All nodes inside the shape or if null, the bounding box.
If a shape is supplied, it is assumed to be contained inside the bounding box.
Returns true if a connection between the adjacent nodes n1 and n2 is valid.
Also takes into account if the nodes are walkable
bool Linecast | ( | Vector3 | _a, |
Vector3 | _b | ||
) |
Returns if _b is visible from _a on the graph.
This is not the same as Physics.Linecast, this function traverses the graph and looks for collisions.
bool Linecast | ( | Vector3 | _a, |
Vector3 | _b, | ||
Node | hint | ||
) |
Returns if _b is visible from _a on the graph.
[in] | _a | Point to linecast from |
[in] | _b | Point to linecast to |
[in] | hint | If you have some idea of what the start node might be (the one close to _a), pass it to hint since it can enable faster lookups This is not the same as Physics.Linecast, this function traverses the graph and looks for collisions.
|
bool Linecast | ( | Vector3 | _a, |
Vector3 | _b, | ||
Node | hint, | ||
out GraphHitInfo | hit | ||
) |
Returns if _b is visible from _a on the graph.
[in] | _a | Point to linecast from |
[in] | _b | Point to linecast to |
[out] | hit | Contains info on what was hit, see GraphHitInfo |
[in] | hint | If you have some idea of what the start node might be (the one close to _a), pass it to hint since it can enable faster lookups This is not the same as Physics.Linecast, this function traverses the graph and looks for collisions.
|
void OnPostScan | ( | AstarPath | script | ) |
Auto links grid graphs together.
Called after all graphs have been scanned.
|
virtual |
Called after all deserialization has been done for all graphs.
Can be used to set up more graph data which is not serialized
Reimplemented from NavGraph.
|
virtual |
Scans the graph, called from AstarPath.Scan Override this function to implement custom scanning logic
Implements NavGraph.
Reimplemented in LayerGridGraph.
void SerializeNodes | ( | Node[] | nodes, |
AstarSerializer | serializer | ||
) |
Serializes grid graph specific node stuff to the serializer.
void SerializeSettings | ( | AstarSerializer | serializer | ) |
Serialize Settings.
|
virtual |
Sets up neighbourOffsets with the current settings.
neighbourOffsets, neighbourCosts, neighbourXOffsets and neighbourZOffsets are set up.
The cost for a non-diagonal movement between two adjacent nodes is RoundToInt (nodeSize * Int3::Precision)
The cost for a diagonal movement between two adjacent nodes is RoundToInt (nodeSize * Sqrt (2) * Int3::Precision)
bool SnappedLinecast | ( | Vector3 | _a, |
Vector3 | _b, | ||
Node | hint, | ||
out GraphHitInfo | hit | ||
) |
Returns if _b is visible from _a on the graph.
This function is different from the other Linecast functions since it 1) snaps the start and end positions directly to the graph and it uses Bresenham's line drawing algorithm as opposed to the others which use sampling at fixed intervals. If you only care about if one node can see another node, then this function is great, but if you need more precision than one node, use the normal linecast functions
[in] | _a | Point to linecast from |
[in] | _b | Point to linecast to |
[out] | hit | Contains info on what was hit, see GraphHitInfo |
[in] | hint | If you have some idea of what the start node might be (the one close to _a), pass it to hint since it can enable faster lookups |
This is not the same as Physics.Linecast, this function traverses the graph and looks for collisions.
void UpdateNodePositionCollision | ( | Node | node, |
int | x, | ||
int | z | ||
) |
Updates position, walkability and penalty for the node.
Assumes that collision.Initialize (...) has been called before this function
void UpdateSizeFromWidthDepth | ( | ) |
Updates size from width, depth and nodeSize values.
Also generates a new matrix .
float aspectRatio = 1F |
Scaling of the graph along the X axis.
This should be used if you want different scales on the X and Y axis of the grid
float autoLinkDistLimit = 10F |
Distance limit for grid graphs to be auto linked.
bool autoLinkGrids = false |
Auto link the graph's edge nodes together with other GridGraphs in the scene on Scan.
|
protected |
To reduce memory allocations this array is reused.
Used in the CalculateConnections function
bool cutCorners = true |
If disabled, will not cut corners on obstacles.
If connections is Eight, obstacle corners might be cut by a connection, setting this to false disables that.
int erodeIterations = 0 |
Erosion of the graph.
The graph can be eroded after calculation. This means a margin is put around unwalkable nodes or other unwalkable connections. It is really good if your graph contains ledges where the nodes without erosion are walkable too close to the edge.
Below is an image showing a graph with erode iterations 0, 1 and 2
int erosionFirstTag = 1 |
Tag to start from when using tags for erosion.
bool erosionUseTags = false |
Use tags instead of walkability for erosion.
Tags will be used for erosion instead of marking nodes as unwalkable. The nodes will be marked with tags in an increasing order starting with the tag erosionFirstTag. Debug with the Tags mode to see the effect. With this enabled you can in effect set how close different AIs are allowed to get to walls using the Valid Tags field on the Seeker component.
float maxClimb = 0.4F |
The max position difference between two nodes to enable a connection.
Set to 0 to ignore the value.
int maxClimbAxis = 1 |
The axis to use for maxClimb.
X = 0, Y = 1, Z = 2.
float maxSlope = 90 |
The max slope in degrees for a node to be walkable.
int [] neighbourOffsets |
Index offset to get neighbour nodes.
Added to a node's index to get a neighbour node index
NumNeighbours neighbours = NumNeighbours.Eight |
Number of neighbours for each node.
Either four directional or eight directional
int [] neighbourXOffsets |
Offsets in the X direction for neighbour nodes.
Only 1, 0 or -1
int [] neighbourZOffsets |
Offsets in the Z direction for neighbour nodes.
Only 1, 0 or -1
float penaltyPositionFactor = 1F |
Scale factor for penalty when calculating from position.
float penaltyPositionOffset = 0 |
Offset for the position when calculating penalty.
Vector2 size |
TextureData textureData = new TextureData () |
Holds settings for using a texture as source for a grid graph.
Texure data can be used for fine grained control over how the graph will look. It can be used for positioning, penalty and walkability control.
Below is a screenshot of a grid graph with a penalty map applied. It has the effect of the AI taking the longer path along the green (low penalty) areas.
Vector2 unclampedSize |
Size of the grid.
Might be negative or smaller than nodeSize
|
get |
This is placed here so generators inheriting from this one can override it and set it to false.
If it is true, it means that the nodes array's length will always be equal to width*depth It is used mainly in the editor to do auto-scanning calls, setting it to false for a non-uniform grid will reduce the number of scans
|
get |
Use heigh raycasting normal for max slope calculation.
True if maxSlope is less than 90 degrees.