we assume that someone is going from Point a moves to point B, but the two points are separated by a wall. 1, Green is a, red is B, Middle blue is wall.
We divide the area we want to search into square squares to simplify the search area, and our search area is simplified for two - dimensional arrays . Each item of an array represents a lattice, and its state is to walk and not to walk , and the path is found by calculating which squares need to pass from A to B. Once the path is found, the character moves from the center of the Square to the center of the other square until it reaches the destination.
The center point of the grid we become "node (nodes)". If you've read other articles about a * pathfinding algorithm, you'll find that people are often talking about nodes. Why not just describe it as a square? Because it is possible for us to delimit the search area for other variants rather than squares, such as hexagonal, rectangular, or even arbitrary multiple deformations. The nodes can be placed in any polygon, can be placed in the center of the multi-deformation, can also be placed on the edge of the polygon. We use this system because it is the simplest.
Start searching (starting the search)
Once we have simplified the search area to a set of quantifiable nodes, as we did above, the next step is to find the shortest path . In a *, we start with the starting point, check its adjacent squares, and then expand around until we find the target.
here's how to find it :
1. start from Start A, and add it to an open list of squares. Now there is only one item in the open list, which is the starting point a, which will slowly add more items. The grid in the Open list is the path that might pass along the way, or it might not. Basically open list is a list of squares to check.
2. Viewing and starting point a adjacent square. (Ignoring squares in which the walls occupy, and other obstacles ), add the squares that can be taken to the open list. set the starting point A to the parent of these squares. objects . The content of these parent nodes is important when we are tracking paths. (based on the parent of the current node, the reverse path)
3. Remove A from the open list and add it to the close list , where each square in the list is now no longer in need of attention.
as shown, the dark green Square is the starting point and its outer frame is bright blue, indicating that the square is added to the close list. The black squares adjacent to it need to be checked, and their outer frames are bright green. Each black square has a gray pointer pointing to their parent node, and here is the starting point A.
Next, we need to select a square adjacent to start a from the open list and choose the one with the minimum F value.
Path Ordering (path sorting)
The key to calculating the squares that make up the path is the following equation:
F = G + H
Over here
G = The moving cost of moving from start A to the specified square.
H = Estimated cost to move from the specified square to the end point B. This is often called a heuristic, why is it so called, because it's a guess. Until we find the path we will know the true distance, because there are all kinds of things (such as walls, water, etc.) on the way.
Our path is this: Iterate through the open list and select the square with the lowest F value.
as mentioned above, G is the moving cost of moving from start a to the specified square. In this case, the horizontal and vertical movement cost is 10, and the diagonal movement cost is 14. This data is used because the actual diagonal moving distance is the square root of 2, or is approximately 1.414 times times the cost of horizontal or vertical movement. Using 10 and 14 is for simplicity.
Now that we're going along the path to the specified squares, we calculate G value, then the method of calculating the G value of the square is to find out the G value of its father and then add 10 or 14 according to whether the father is in a straight or diagonal direction. As we leave the starting point and get more squares, this approach becomes clearer.
There are many ways to estimate H value. Here we use the Manhattan (Manhattan Block algorithm) method to calculate the number of squares that move from the current grid to the target in the horizontal or vertical direction, ignoring the diagonal movement and multiplying the total by 10. It's called the Manhattan method because it's like counting the number of blocks that have been passed from one location to another, and you can't cross the block diagonally. It is important to calculate that H is to ignore obstructions in the path. This is an estimate of the remaining distance, not the actual value, and is therefore called heuristics.
Add G and H to get F. The result of our first step is as shown. Each square is marked with a value of F, G, H, as in the right square of the starting point, the upper- left corner is F, the lower-left corner is G, and the lower-right corner is H .
Now let's look at some of those squares. In a square labeled with letters, G = 10. This is because the horizontal direction from the beginning to the distance there is only one square. Directly above the starting point, below, the left square of the G value is 10, the diagonal square g value is 14.
The H value is obtained by estimating the Manhattan distance from the end point (Red Square), moving only horizontally and vertically, and ignoring the walls along the way. In this way, the square to the right of the starting point has a distance of 3 squares, so H = 30. The squares above this square have a distance of 4 squares to the end point (note that only horizontal and vertical distances are calculated), so H = 40. For other squares, you can use the same method to know how the H value is derived.
every square. F value, again, the G-value and H-value can be added directly.
Continue searching (continuing the search)
To continue the search, we select the lowest F-value (square) node from the open list, and then do the following for the selected squares:
1 . Take it out of the open list and put it in the close list.
2 check all squares adjacent to it, ignoring any squares that are in the close list or not (such as walls, water, or other illegal terrain) if the squares are not open l is T, they are added to the open list.
3. Set our selected squares as the father of these newly added squares.
4 If an adjacent square is already in the open list, check to see if the path is better, which means whether the square has a smaller G-value is reached through the current square (our checked squares). if not, do nothing.
Conversely, if The G value is small, then the father of that Square is set as the current square (our checked squares), and then the F and G values of that square are recalculated. If you are still confused, please refer to.
Ok, let's see how it works. of our first 9 squares, there are 8 in the open list, where the starting point is placed in the close list. In these squares, the square to the right of the beginning of the grid has the lowest F value of 40, so we select this grid as the next square to be processed. Its outer frame is lit with a blue line.
First, we move it from the open list to the close list (that's why the Blue Line is lit). Then we examine the squares adjacent to it. The square on the right is the wall, we ignore it. The square on the left is the starting point, and in the close list, we also ignore it. The other 4 adjacent squares are in the open list, and we need to check to see if the path through the square is better and use the G value to determine it. First Let's look at the squares above. It now has a G value of . If we get there through the current square, the G value will be 20 (where 10 is the G value to reach the current square, plus the G-value 10 that moves from the current square to the upper square). Obviously 20:14 large, so this is not the optimal path. It is better to move diagonally from the starting point to the square than to move it vertically.
after checking the 4 adjacent squares already in the open list, we did not find a better path through the current grid, so we did not make any changes. Now that we have checked all the adjacent squares of the current square and handled them, it is time to select the next box to be processed.
so traversing our open list again, now it's only 7 squares, and we need to choose the one with the lowest F value. Interestingly, this time there are two squares of F values(Figure 4, up and down 2), which one to choose? It doesn't matter. In terms of speed, it is faster to choose the last to add the open list to the grid. This leads to preference for the newly found squares when approaching the target in the pathfinding process. But it doesn't matter. (Different treatment of the same data, resulting in the two version of A * found a different path of equal length).
We select the square at the bottom right of the starting point as shown in.
This time, when we check the adjacent squares, we find that the square on the right is the wall, ignoring it. Right The same is true of the above .
We ignored a lattice below the wall . Why? Because if you don't cross the corner, you can't move directly from the current square to that square. You need to go down first and then move to that square to get around the corner.
This leaves 5 (9 Gongge left 3, and the current top and bottom 2) adjacent squares. The 2 squares below the current grid have not been added to the open list, so add them and set the current grid as their father. of the remaining 3 squares, 2 are already in the close list (one is the starting point, one is the square above the current square, the outer frame is highlighted), and we ignore them. the last square, the square to the left of the current square, checks to see if it has a smaller G value through the current square . No. So we're going to select the next pending box from the open list.
Repeat this process until you add the end point to the open list, as shown in.
So how do we determine the actual path? It's simple, starting at the end, moving the arrow toward the parent node, so you're taken back to the beginning, and that's your path. As shown in. Moving from start A to end B is simply moving from the center of one square on the path to the center of another square until the target.
A * Algorithm summary (Summary of the * method)
1. Add the starting point to the open list.
2. Repeat the following procedure:
A. Traverse the open list to find the node with the lowest F value, and use it as the node that is currently being processed.
B. Move the node to the close list.
C. For each square of the 8 adjacent squares of the current square, do the following:
if it is not reachable or it is in the close list, ignore it.
if it is not in the open list, add it to the open list and set the current square as its father, recording the square's F, G, and H values.
if it is already in the open list, check if the path is better and use the G value for reference. A smaller G-value indicates that this is a better path. If so, set its father to the current square and recalculate its G and F values. If your open list is sorted by the F value, you may need to reorder it after the change.
D. Stop, when you
The end is added to the open list, where the path has been found, or
The find endpoint failed, and The open list is empty and there is no path at this time.
3. Save the path. From the end point, each square moves along the parent node until it starts, and that's your path.
This article refer to https://www.gamedev.net/resources/_/technical/artificial-intelligence/a-pathfinding-for-beginners-r2003
Reference Links:
http://blog.csdn.net/aisajiajiao/article/details/17622063
Http://www.cnblogs.com/yangyxd/articles/5447889.html
Http://www.taidous.com/thread-58620-1-1.html
Http://edu.manew.com/course/44
Implementation of A * pathfinding algorithm