I am trying to make an enemy node follow the player node in C# with A* algorithm. I have read the tutorials and downloaded some C# exmaples. I have got my A* algorithm working to a degree now. It will follow the player in an open space but hits a snag when trying to trace around an object.
So when my algorithm is checking and moving in a direction of lowest F value, it might come across a dead end, and at this point it needs to retrace its steps backwards, but it can't because my code tells it that a previously checked node is closed and can't be moved to, and therefore it gets stuck.
How do I recalculate a closed node to tell my algorithm that it is ok to go back that way.
Also, if I do tell my algorithm to go back on its self, what is to stop it from going back AGAIN to the better node it just came from; effectively going back between two nodes repeatedly.
I see that it should be able to check a node in the closed list and determine if it is better on this particular path, but I'm not sure how that is done.
The heuristics I'm using.
G = Math.Abs(StartNodeX - TargetNodeX) + Math.Abs(StartNodeY - TargetNodeY)
H = Math.Abs(CurrentNodeX - TargetNodeX) + Math.Abs(CurrentNodeY - CurrentNodeY)
F = G + H
Psuedocode.
- Add all adjacent nodes to the Open List
- Check all those nodes for lowest F score and add that node to the Best Path List
- Add all checked nodes to the Closed List (they've all been checked, don't want to check them again)
- Repeat until target is reached
- Move enemy 1 node in the direction of the best path
- Repeat all over again