Reading Notes-ai in Game Development-Chapter 2-A * path Search Algorithm

Source: Internet
Author: User

1. Brief Introduction

A * is a path search algorithm commonly used in game software development. A * the algorithm is so attractive because it ensures the optimal path between any start point and any end point. We can try to use the * algorithm, except for some special scenarios. For example, if there is no obstacle between the start point and the end point and there is a clear line of sight, the line of sight movement algorithm is fast and effective, and there is no need to use the * algorithm. If the CPU function is not strong, the * algorithm consumes a lot of CPU computing power, especially when finding paths for many game roles at the same time, the * algorithm may not be the best choice. In the following sections, we will gradually discuss the steps for using the * algorithm.

2. Define a search domain

Whether in a continuous environment or a brick environment, if the number of vertices is too large, the * algorithm is impractical. However, by simplifying the search area, the * algorithm becomes feasible.
· Simplified search domains in a continuous Environment
Place nodes in the game environment to simplify the search domain. The smaller the number of nodes, the faster the path searching speed.
We need a data list of the connection relationships between nodes.

· Simplified search domains for Brick Environments
You can use a node to replace several bricks. When searching for a path, you can only search for a few or one large brick in the environment, rather than the entire environment. If a reasonable path cannot be found in some areas, it is considered that there is no reasonable path.
In the brick environment, each brick is adjacent to each other, so you do not need to connect to the data list.

3. A * general idea of the Algorithm
A * the algorithm is A bit similar to the idea of popping up from the ground up in A circle. For example:

However, it is noted that although the cost of moving bugs to eight locations around is 1, the cost of moving these eight locations to the human role is different. Therefore, we add these two costs as the total mobile costs. When calculating the moving cost from location to destination, the obstacle is not considered. In this way, each time we give priority to starting from the smallest position of the total movement cost, we will not consider the positions and obstacles that have been blown up. In this way, even if the total movement cost at some locations is small but obstacles are damaged, they may be blasted first. However, due to the existence of obstacles, we will not continue to blow up, select a better path.
Is a final form of explosion:

The last step is to find the shortest path based on the fried records. There are many fried locations. We will record all the parent locations of the fried locations and wait until they reach the target location, start from the target location to find its parent location in sequence, and gradually find the starting location. The path composed of these parent locations is the path with the lowest moving cost.

4. A * pseudo-code of the Algorithm

Add the Start Node to the open list
While (open list is not empty ){
Current node = lowest cost node in open list
If (current node = target node ){
Path complete
Find the parent node from the target node until the parent node is the Start Node.
} Else {Move the current node to the close list
View each adjacent node of the current node
For (each adjacent node)
If (this node is not in the open list and this node is not in the closed list and this node is not an obstacle ){
Move the node to the open list
Computing costs
The parent node of the node is recorded as the current node}
}
} If (path not found) {the destination cannot be reached from the starting point}

 

5. Terrain cost

Sometimes other factors need to be considered. The shortest path is not necessarily the fastest. For example, the moving costs of different terrain are different. Only when calculating the total moving costs, you can consider the terrain. Of course, for money, fuel, or other types of resources, the problem will become more complicated.

6. Influence

Terrain costs are often known in advance, but some factors cannot be known in advance. For example, nodes with sight of any enemy have a high cost. This cost cannot be created during the design of game software because the location of the game role changes. Influence mapping is A way to change the cost of A * node, depending on what happens in the game.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.