Stuck on an a* pathfinding algorithm [modified]
-
SOLVED For anyone interested here is the solution to my problem:
public IEnumerable<Tile> PathFind(Tile startNode, Tile endNode) { List<Tile> openNodes = new List<Tile>() { startNode }; List<Tile> closedNodes = new List<Tile>(); Tile lowestFScore = null; while (openNodes.Count > 0 && lowestFScore != endNode) { lowestFScore = GetLowestScoringTile(openNodes, endNode); openNodes.Remove(lowestFScore); if (lowestFScore == null) return null; SwitchFromOpenToClosedList(lowestFScore, openNodes, closedNodes); IEnumerable<Tile> neighbouringTile = GetNeighbouringTiles(lowestFScore); foreach (Tile n in neighbouringTile) { if (IgnoreTile(n)) { SwitchFromOpenToClosedList(n, openNodes, closedNodes); continue; } if (closedNodes.Contains(n)) continue; if (!openNodes.Contains(n)) { openNodes.Add(n); n.Parent = lowestFScore; } } } return RenderToGrid(endNode); }
I have done my research and attempted several times to create my version of the infamous A* path-finding algorithm. The algorithm I have made does seem to work though!...(unsurprisingly) If anyone can help me it would be great, I am sure it is just a logic problem in the main part of the method:
public void PathFind(Tile startNode, Tile endNode)
{
List<Tile> openNodes = new List<Tile>() { startNode };
List<Tile> closedNodes = new List<Tile>();Tile currentNode = startNode; while (true) { foreach (Tile n in GetNeighbouringTiles(currentNode)) { if (closedNodes.Contains(n)) continue; if (IgnoreTile(n)) { if (!closedNodes.Contains(n)) { closedNodes.Add(n); continue; } }
-
SOLVED For anyone interested here is the solution to my problem:
public IEnumerable<Tile> PathFind(Tile startNode, Tile endNode) { List<Tile> openNodes = new List<Tile>() { startNode }; List<Tile> closedNodes = new List<Tile>(); Tile lowestFScore = null; while (openNodes.Count > 0 && lowestFScore != endNode) { lowestFScore = GetLowestScoringTile(openNodes, endNode); openNodes.Remove(lowestFScore); if (lowestFScore == null) return null; SwitchFromOpenToClosedList(lowestFScore, openNodes, closedNodes); IEnumerable<Tile> neighbouringTile = GetNeighbouringTiles(lowestFScore); foreach (Tile n in neighbouringTile) { if (IgnoreTile(n)) { SwitchFromOpenToClosedList(n, openNodes, closedNodes); continue; } if (closedNodes.Contains(n)) continue; if (!openNodes.Contains(n)) { openNodes.Add(n); n.Parent = lowestFScore; } } } return RenderToGrid(endNode); }
I have done my research and attempted several times to create my version of the infamous A* path-finding algorithm. The algorithm I have made does seem to work though!...(unsurprisingly) If anyone can help me it would be great, I am sure it is just a logic problem in the main part of the method:
public void PathFind(Tile startNode, Tile endNode)
{
List<Tile> openNodes = new List<Tile>() { startNode };
List<Tile> closedNodes = new List<Tile>();Tile currentNode = startNode; while (true) { foreach (Tile n in GetNeighbouringTiles(currentNode)) { if (closedNodes.Contains(n)) continue; if (IgnoreTile(n)) { if (!closedNodes.Contains(n)) { closedNodes.Add(n); continue; } }
Here is something that works: WPF: A* search[^] Regards Espen Harlinn
-
Here is something that works: WPF: A* search[^] Regards Espen Harlinn
Thank Espen, I was trying to do it based on the concept, I didn't want to use other code and instead build from the concept :-D
-
Thank Espen, I was trying to do it based on the concept, I didn't want to use other code and instead build from the concept :-D
Good for you!
-
SOLVED For anyone interested here is the solution to my problem:
public IEnumerable<Tile> PathFind(Tile startNode, Tile endNode) { List<Tile> openNodes = new List<Tile>() { startNode }; List<Tile> closedNodes = new List<Tile>(); Tile lowestFScore = null; while (openNodes.Count > 0 && lowestFScore != endNode) { lowestFScore = GetLowestScoringTile(openNodes, endNode); openNodes.Remove(lowestFScore); if (lowestFScore == null) return null; SwitchFromOpenToClosedList(lowestFScore, openNodes, closedNodes); IEnumerable<Tile> neighbouringTile = GetNeighbouringTiles(lowestFScore); foreach (Tile n in neighbouringTile) { if (IgnoreTile(n)) { SwitchFromOpenToClosedList(n, openNodes, closedNodes); continue; } if (closedNodes.Contains(n)) continue; if (!openNodes.Contains(n)) { openNodes.Add(n); n.Parent = lowestFScore; } } } return RenderToGrid(endNode); }
I have done my research and attempted several times to create my version of the infamous A* path-finding algorithm. The algorithm I have made does seem to work though!...(unsurprisingly) If anyone can help me it would be great, I am sure it is just a logic problem in the main part of the method:
public void PathFind(Tile startNode, Tile endNode)
{
List<Tile> openNodes = new List<Tile>() { startNode };
List<Tile> closedNodes = new List<Tile>();Tile currentNode = startNode; while (true) { foreach (Tile n in GetNeighbouringTiles(currentNode)) { if (closedNodes.Contains(n)) continue; if (IgnoreTile(n)) { if (!closedNodes.Contains(n)) { closedNodes.Add(n); continue; } }
.. A few comments on this, if your using XNA and want to use this code on the XBOX, you should get rid of the Foreach loop, it is a performance killer due to GC on the XBOX.... Also you should look into the weighting Heuristics, because this approach albeit closed and complete is not as efficient as it could be.... Also your sucking up memory as you use this algo... have you tested it against a large number of bots representative to the normal in game load...., I'd wadger it won't run inside your game loop propperly for more then 125 or so bots (using a 256x256 tile map).... just trying to give you the heads up...
I'd blame it on the Brain farts.. But let's be honest, it really is more like a Methane factory between my ears some days then it is anything else...
-----
"The conversations he was having with himself were becoming ominous."-.. On the radio...