1. What problem does A* pathfinding solve?
Used to calculate the shortest path for the player to avoid obstacles during the calculation process to find the shortest path
2. The basic principle of A* pathfinding
The basic logic of the A* algorithm is to start from the starting point, search the surrounding nodes for sorting and comparison, get the best point, and use the best point as a new starting point to search and compare again, repeat this search logic until the end point is found, the search ends, and the shortest path is returned
3. The detailed principle of A* pathfinding
How to get the best point by expanding the detailed principle based on the principle?
The formula for calculating the cost of pathfinding: F=G+H, G represents the distance from the node to the starting point, and H represents the distance from the node to the end point obtained through the Manhattan algorithm, that is, the value of the horizontal distance + vertical distance; add G and H in the node to get F, which is the total cost
Every time you search, the surrounding nodes of the starting point will be put into the open list, and the optimal point with the smallest F value will be obtained by comparison. Every time you put the surrounding nodes into the open list, you need to make a judgment**
1. Whether to block or not to let go
2. Whether it is in the open list or closed list , it will not be released if it is in it, and it will be released if it is not in it
Search to get the best point, put the best point into the closed list, and judge whether the point is the end point every time it is stored, if it is, the pathfinding ends, if not, the pathfinding continues
Pathfinding ends, if the shortest path is obtained?
The nodes in the list contain the F value and the parent node pointer of the current node. The parent node is also the starting point of the search, and the shortest path is obtained by backtracking through the pointer.
Else, if it is a dead end, (obstacles surround the entire end point), then during the continuous search process, the number of nodes stored in the closed list will increase, and the open list will become less and less until it is empty, indicating that the end point cannot be reached , a dead end
1. Pathfinding consumption formula
f=g+h
f (pathfinding cost) = g (distance from the starting point) + h (distance from the end point)
g value: (the starting point distance has a fixed algorithm), assuming that the unit of a grid is 1, then the horizontal grid distance is 1, and the oblique grid is 1.4 (the hypotenuse of an equilateral triangle)
h value: (Manhattan block algorithm): x+y
2. Open the list
A container that stores the information consumed by the pathfinding of the grids around the starting point. Each grid information contains (f, g, h, the parent grid of the current grid)
After sorting, find the grid (best point) with the smallest f value, and store it in the closed list
Every time you put the surrounding points into the open list, you need to make a judgment
1. Whether to block or not to let go
2. Whether it is in the open list or closed list , put it if it is there, and don’t put it if it is not
3. Close the list
The container that stores the best point, every time the best point is put into the closed list, it needs to judge whether the point is the end point
If yes, pathfinding ends, if not pathfinding continues
4. The parent object of the grid object
This information can really find the search path. In special cases, if the f value of the surrounding points may be the same when searching the grid several times, because our search logic is executed based on the pathfinding consumption formula, when the end point is found At the end, returning to the search path is to trace back the parent node pointer from the end point, so as to obtain the shortest path
If the open list is empty, it is judged as a dead end