How do I remove the unnecessary nodes from the path to make it as short as possible? Idealy I'd like the result path to look like the green line. The red thingy is not walkable.
var newPath = new List<Vector3>();
var nodes = p.vectorPath;
int lastCp = 0;
List<int> toRemove = new List<int>();
for (int i = 2; i < nodes.Count; i++)
{
if (IsPassable(nodes[lastCp], nodes[i]))
{
toRemove.Add(i - 1);
}
else
{
lastCp = i - 1;
}
}
for (int i = 0; i < nodes.Count; i++)
{
if (!toRemove.Contains(i))
{
newPath.Add(nodes[i]);
}
}
Here's my attempt to solve the problem and it usualy works well but sometimes if I stand next to obstacle I get the orange line in the picture above.
I'm assuming the path has at least 3 nodes.
nodes
is a list of Vector3
(the positions of grey dots on picture), IsPassable
returns true if the path between two given Vector3
is passable.
newPath
idealy should contain two points in the case of example path above.
EDIT:
I know why the algorithm doesn't work in this case (look at @Draco18s answer below). What I don't know is how to fix it so it always returns the shortest path regardless of start and target location.