Misaligned paths in grid graph

Hi. I’m creating a group of paths to work as a patrol route for an AI, but there’s a slight problem with the paths. My game world (including the graph) is procedurally generated at runtime, so a grid graph works easiest for me. This is how I create the graph:

var data = AstarPath.active.astarData;
var graph = data.AddGraph(typeof(GridGraph)) as GridGraph;

graph.center = new Vector3(genWidth / 2f, 0f, genHeight / 2f) * UnitSize;
graph.width = genWidth * UnitSize;
graph.depth = genHeight * UnitSize;
graph.cutCorners = false;
graph.collision = new GraphCollision() { heightMask = ~(1 << wallMask), mask = 1 << wallMask };

graph.UpdateSizeFromWidthDepth();

AstarPath.active.Scan();

genWidth and genHeight are the dimensions of the generated map in game-units, and UnitSize is the size of one game-unit in Unity units (in this case, 3f). This is how I create the paths:

for (var i = 0; i < patrolPoints.Length; i++)
{
    var point = patrolPoints[i];
    var nextPoint = patrolPoints[(i + 1) % patrolPoints.Length];

    var path = ABPath.Construct(point, nextPoint);
    AstarPath.StartPath(path);
    AstarPath.WaitForPath(path);

    paths.Insert(i, path);
}

patrolPoints is an array of Vector3s, which defines the points the AI should be patrolling through. paths is a list of the created paths. The previously mentioned problem is that the paths are slightly misaligned from the patrol points:

The vertical red line on the right is one of the patrol points, and the red and black lines are visualisations of the paths that use the patrol point. As you can see, the paths don’t actually go through the point, but through a node next to it. This happens with all the points, and the relative direction for the actual node the path uses varies from point to point, so a hacky solution of just offsetting the paths to cancel the misalignment wouldn’t work.

I’m using version 3.8.2 of the A* Pathfinding Project and Unity 5.3.4f1.

Thanks for any help!

Hi

I’m not quite sure I understand your screenshot. Do you think you could post another one from further away?
Also, do you think you could turn off 3D gizmos to make the lines clearer.

Here you go. I changed the path visualisation to be magenta for possibly better visibility.

Ah. Ok.
The confusion I think stems from that what is visible in the scene view are the connections between the nodes. So each node is centered on those points where 8 lines meet (since every node has 8 neighbors with your settings). Therefore I would suggest that you offset the graph by 0.5 node sizes in the X and Z directions.

Yup, the slight offsetting fixed the issue. Thanks!

1 Like