Hello everyone,
I have implemented an A* algorithm to find the shortest path between two static none moving points, and that works great. However if I use the same algorithm to find the shortest path between one point and another moving point(example of that is an enemy and a moving player) for multiple enemies, my FPS tanks from ~2000+ to 15 FPS or lower and the game starts to freeze every once and a while.
So what am I doing? and What is the problem?
Well, what i'm doing is I'm re-calculating the shortest path using A* every time the player moves from one node or cell to another. (I have a grid that contains 100X100 cells or nodes where each node or cell is 32x32 pixels). So each time the player moves by 32 pixels I re-calculate the path, which is very taxing and unnecessary.
So that's what I am doing.
I did find one possible solution which is, I would only calculate the path from the enemy to the player once, and then whenever the player moves one node or cell I just add that node or cell to the already existing way point list. However that solution has one problem. Imagine if the player is not walking in a straight line, imaging he is walking in a zig-zag motion, if I add the same way points to the enemy path, the enemy will do the same zig-zag motion and will not walk in the shortest possible path which would be a straight line.
Like so:-
So Here is my question, What is the best possible way to make an enemy follow a player using the shortest path possible? And does it work on more than one enemy? by that I mean will your solution work in case of 10 enemies that are all following the player? will there be no massive FPS drop?
Thank you.