Many games, especially RTS and RPG games, need to find the path. Search algorithms include deep-first search (DFS), breadth-first search (BFS), and astar algorithms. The astar algorithm is an heuristic algorithm with the highest efficiency among several algorithms, therefore, it has become the most common pathfinding algorithm in the game.
Go straight to the question:
In game design, maps can be divided into several square areas (squares) of the same size, which are the basic unit for finding a path.
When the start point and end point of the seek are determined, assuming that each square has a f value, this value represents the cost of selecting the square under the current route.
The idea of finding a path for astar is very simple: From the start point, each step selects the grid with the lowest cost until the end point is reached.
The core formula of the astar algorithm is the calculation of the F value:
F = G + H
Total movement cost of F-Square
G-price for moving the start point to the current square
H-Estimated moving price from the current square to the end point
The formula is explained in detail below to help you better understand it.
How is the G value calculated?
Suppose we are now in a grid, and there are eight adjacent grids to go. When we move up, down, left, and right, the price is 10; when you move the four grids in the upper left, lower left, upper right, and lower right, the moving price is 14. That is, the moving price of the diagonal line is 1.4 times that of the straight line.
This is the most basic G value calculation method, applicable to most 2.5drpg browser games.
Basic formula:
G = Mobile price
Based on Game needs, G-value computing can be expanded. For example, add the influence of terrain factors on pathfinding. If the grid terrain is different, the Movement cost will certainly be different if different terrain grids are selected. Although both flat terrain and Hill terrain can be taken along the same road, flat terrain is obviously easier to walk.
We can assign different cost factors to different terrain to reflect the difference in G value. For example, if you set the price factor to 1 for the flat terrain and 2 for the hill terrain, the G value for the flat terrain is lower when the movement cost is the same, the algorithm tends to select a flat terrain with a smaller G value.
Expansion formula:
G = moving cost * cost factor
How is the H value estimated?
Obviously, when we only know the current vertex, the end vertex, and the path of the two, we cannot accurately determine the H value, so we can only estimate it.
There are multiple ways to estimate the H value, such as the Manhattan distance, European distance, and diagonal estimate. The most common and simplest method is to estimate the Manhattan distance:
H = horizontal distance from the current square to the end point + vertical distance from the current square to the end point
ASIDE: the reason why the astar algorithm is regarded as an heuristic algorithm is that it can reduce the possibility of detours by estimating the H value, making it easier to find a shorter path. Other algorithms that do not have any heuristic policies do not have estimation processing. They just give a list of all accessible paths and select a shortest path from them. This is why the astar algorithm is more efficient.
How are the G and H values of each square determined?
G value = G value of the parent node + price for moving the parent node to the current point
H = the Manhattan distance from the current point to the end point
Finally, the astar algorithm also requires two lists:
Open list-Used to record all the available grids
CLOSED list-Used to record all grids that are no longer considered
The above is what we need to complete the astar algorithm, and the process of the algorithm is not complex.
Astar algorithm pseudo code:
A. Record the start point as the current point P
B. Add the current vertex P to the closed list.
C. Search for all neighboring points of vertex P. If a neighboring point is neither in the open or closed list, calculate the F value of the neighboring point and set the parent node to P, and then put it in the open list
D. Check whether the open list is empty. If it does not indicate that all possible path points have been found before the end point is reached, the search fails and the algorithm ends; otherwise, continue.
E. Take a point with the smallest F value from the open list as the next step in path searching.
F. Determine whether the vertex is the end point. If yes, the path is successfully searched and the algorithm ends. Otherwise, the algorithm continues.
G. Set the vertex to the current vertex P and skip back to Step C.
Subsequent Optimization
The above is the most basic principle of the astar algorithm. It is not difficult to use lines of code to write an available astar algorithm. Of course, the astar algorithm can not only be used in practice, but also optimize the details:
1. Select a faster binary tree as the open list, which helps us to retrieve the point with the smallest F value from the open list more quickly;
2. determine under which conditions the diagonal line path can be used;
3. Use the blenham algorithm to determine whether two points can pass through directly. If the two points can pass through, directly return the two-point straight line path. If the two points cannot pass through directly, use the astar algorithm to find the path, which improves the efficiency of searching;
4. After the astar algorithm obtains the path finding path, the path can be smoothly processed using the freuch algorithm to make the movement of the person more natural.
Here we only explain the principle of the astar Algorithm in a language, and do not provide a diagram to explain the entire process. If you want to understand the entire process more intuitively, we recommend that you refer to the astar tutorial translated by the following two netizens.
Http://www.raywenderlich.com/zh-hans/21503/a%E6%98%9F%E5%AF%BB%E8%B7%AF%E7% AE %97%E6%B3%95%E4%BB%8B%E7%BB%8D
http://www.cppblog.com/mythit/archive/2009/04/19/80492.aspx