Hey there
I try to generate paths that are NOT the shortest and feature some sort of random direction changes.
Please see the attached image to get an idea:
For my problem we assume that we only have walkable nodes and there are no obstacles. The black line shows an example of a typical shortest path without diagonals. However, for my project I’m more interested in paths like Zig-Zag 1 or Zig-Zag 2.
What I need help with:
I think that similar paths could be generated when a) the traversal costs of all nodes could be completely randomized for a single path request or b) when I could set the traversal costs of each node contingent on the previous path by e.g. increasing the traversal cost if the last nodes in the path pointed in the same direction.
However I tried the following to apply randomized cost but it failed to generate the desired results.
private Path CalculateStartingPath(Vector3 _startPos, Vector3 _targetPos)
{
ABPath path = ABPath.Construct(_startPos, _targetPos);
path.traversalProvider = new RandomZigZag();
AstarPath.StartPath(path, true);
path.BlockUntilCalculated();
return path;
}
class RandomZigZag : ITraversalProvider
{
public bool CanTraverse(Path path, GraphNode node)
{
return DefaultITraversalProvider.CanTraverse(path, node);
}
public uint GetTraversalCost(Path path, GraphNode node)
{
// Use the default costs and add random values
System.Random random = new System.Random();
return DefaultITraversalProvider.GetTraversalCost(path, node) + (uint) random.Next(0, 10000);
}
}
Is there another way to achieve what I try to do?
And: Is there a way to set the traversal costs of a single node contingent on the previous nodes to account to emphasize more directional changes?
Many thanks in advance