Struct NavMeshStraightLineMod
Encourages A* on a navmesh to follow a straight line from start to goal.
Depending on the structure of your navmesh, this may be useful to get better looking straight paths. This is especially true for meshes that resemble a grid like structure. Because A* can only operate on the triangles itself, there may be many optimal paths to the destination that share the same cost (at the triangle level). If you use SSFA, then it may become apparent that the path that was chosen was not the most optimal after SSFA was performed. See this article for an in depth explanation: https://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html#breaking-ties
This edge modifier mitigates this by dynamically adding a cost penalty to triangles that lie further away from a straight line between the start and the goal location. Causing A* to always prefer paths that are as close to a straight line as possible at the triangle level.
Note that when using this modifier, you must assign your pathfinding start and end locations manually before making the request! If you forget to do this, it will not work and produce unexpected paths.
finder.Stops.Add(from); // pathfinding start location
finder.Stops.Add(to); // pathfinding end location
finder.EdgeMod = new NavMeshStraightLineMod(from, to); // assign the straight line mod with our start+goal, obtained via a raycast
finder.Run(); // run the query
Implements
Namespace: AnyPath.Graphs.NavMesh
Assembly: AnyPath.dll
Syntax
public struct NavMeshStraightLineMod : IEdgeMod<NavMeshGraphLocation>
Constructors
| Improve this Doc View SourceNavMeshStraightLineMod(NavMeshGraphLocation, NavMeshGraphLocation)
Construct the mod using a navmesh start and end location
Declaration
public NavMeshStraightLineMod(NavMeshGraphLocation start, NavMeshGraphLocation end)
Parameters
Type | Name | Description |
---|---|---|
NavMeshGraphLocation | start | |
NavMeshGraphLocation | end |
NavMeshStraightLineMod(float3, float3)
Construct to mod using an arbitrary line in space.
Declaration
public NavMeshStraightLineMod(float3 a, float3 b)
Parameters
Type | Name | Description |
---|---|---|
float3 | a | |
float3 | b |
Methods
| Improve this Doc View SourceModifyCost(in NavMeshGraphLocation, in NavMeshGraphLocation, ref float)
Allows for modification of the cost of a path segment, or totally discarding it as a possibility. This is called during the A* algorithm for every segment between locations A* encounters.
Declaration
public bool ModifyCost(in NavMeshGraphLocation from, in NavMeshGraphLocation to, ref float cost)
Parameters
Type | Name | Description |
---|---|---|
NavMeshGraphLocation | from | The source node for this edge |
NavMeshGraphLocation | to | The other end of the edge |
float | cost | Modify this parameter to alter the cost of the segment. Be careful as to not make the cost lower than a value that the heuristic function of the graph would calculate, as this can result in sub optimal paths. For some graph types this may not be immediately noticable but for true graph structures, providing a lower cost than the heuristic may cause the path to contain detours that look strange. |
Returns
Type | Description |
---|---|
bool | When you return false, the edge is discarded as a possibility. This can be used for instance to simulate a closed door. |
Remarks
The default NoProcessing does not touch the cost and returns true. Keeping the original graph as is. The burst compiler is smart enough to detect this and totally discard this method