Game development commonly used algorithm series (1)

Pathfinding and Navigation Algorithm - Detailed Explanation of Flow Field Pathfinding Algorithm


Table of contents

foreword

1. The main steps of the algorithm based on flow field pathfinding

Two, step by step example

1. Generate Heatmap

2. Vector Field Generation

3. Character movement and navigation

Summarize



foreword

There are often many characters in RTS, and the group finds its way to the vicinity of the destination together. How is this kind of pathfinding realized? Today in this article, I will introduce the algorithm based on flow field pathfinding. This article will explain vector field pathfinding and Its advantages over traditional pathfinding algorithms such as Dijkstra. A basic understanding of Dijkstra's algorithm and potential fields will be helpful to understand this article, but is not required. Next, this article will introduce the core technology and steps of flow field pathfinding in detail. (This article explains the map based on the grid, you can also use it in other places, not limited to the grid).


提示:以下是本篇文章正文内容,下面案例可供参考

1. The main steps of the algorithm based on flow field pathfinding

(1) Traverse each block of the game map and calculate the distance from the current block to the target, which is called "Heatmap" heat map;

(2) For each map block, a vector field is generated according to the Heatmap, and the direction from each block to the target is specified as "Vector Field";

(3) Each character at the destination shares this vector field to move and navigate to the target point;

Two, step by step example

1. Generate Heatmap

Heatmap refers to the path distance from the target point to each tile on the map. Path distance differs from Euclidean distance in that it is calculated from the shortest distance between two points that can traverse the terrain. In the image below, you can see the difference between the path distance and the linear distance from the target point (marked in red) to any point on the map (marked in pink). Blocks that are not walkable are drawn in green. As shown, the path distance (shown in yellow) is 9, while the linear distance (shown in light blue) is about 4.12. The number in the upper left corner of each tile shows the path distance to the target calculated by the Heatmap generation algorithm. Note that there may be more than one path distance between two points, we only count the shortest one. After the first step of the algorithm, we calculated the shortest distance from each block on the map to the destination and generated a Heatmap. 

The Heatmap generation algorithm is a wavefront algorithm. It starts with a target with a value of 0 and flows outward to fill the entire traversable area.

The wavefront algorithm has two steps:

(1) Start with the goal and mark it with a path distance of 0.

(2) Get the unmarked neighbors of each marked tile and mark them with the path distance +1 from the previous tile.

(3) After marking all the blocks of the map, the algorithm ends.

Note: The wavefront algorithm is to perform a breadth-first search on the grid and store the steps needed to reach each block along the way. This algorithm is also sometimes called the brushfire algorithm.

2. Vector Field Generation

Now that the path distance from each block to the goal has been calculated, we can easily determine the path we need to take to get to the goal. Typically the Vector Filed is calculated once, and then all objects to be pathed refer to that Vector Filed at runtime.

A Vector Field simply stores a vector that points in the direction (towards the goal) for each map tile towards its destination. Here is a visualization of the vector field, where vectors follow the shortest path from the center of the tile to the target, forming a flow field. (The red line is the origin, the white line is the direction, and the general trend points to the target point).

Each vector in this vector field is calculated by Heatmap. The specific calculation method of Vector vector is as follows:

Vector.x = left_tile.distance - right_tile.distance, vector x is the block on the left - the block on the right;

Vector.y = up_tile.distance - down_tile.distance, the vector y is the upper block - the lower block;

The distance of each block is the value calculated by the above Heatmap.

If the (left/right/up/down) of the current block is not walkable (obstacles, etc.), the distance from the current block is used in place of the missing value. Once the path vector is roughly computed, it is normalized to avoid inconsistencies later.

3. Character movement and navigation

Now that the vector field has been calculated, calculating the pathfinder's motion is easy. Assuming that vector_field(x, y) returns the vector we calculated at tile(x, y) before, and the movement speed is constant, the pseudocode for calculating the movement speed at tile(x, y) is as follows:

velocity_vector = vector_field(x, y) * desired_velocity

When we are navigating, we only need to move along the direction of the vector, and it is easy to realize the movement of the flow field. When finding the path of multiple character targets at the same time, we only need to calculate it once.

As shown in the picture above, the purple point cannot be moved horizontally, and the distance between the upper and lower blocks is the same, which leads to non-uniqueness. In half of these cases, we will randomly choose a direction, and the result may be The path we choose is not necessarily the shortest, so the flow field pathfinding is based on the local optimal solution. To solve such a problem, there is another good way, which is to divide the blocks into smaller ones to reduce the probability of this.

Of course, if the blocks are divided into small blocks, the amount of calculation will also increase.


Summarize

There are many solutions to the pathfinding problem, such as AStar, etc. Each pathfinding solution has its own advantages and disadvantages. Most of the pathfinding is that the character needs to go from point A to point B, and then call the pathfinding algorithm to find Make a way out. Normally, this is not a problem, but for games like RTS that need to do group pathfinding, a group of characters walk from point A to point B, if each character finds the path independently, the performance overhead will be relatively high , so today we introduce pathfinding based on flow field vectors in order to solve this problem. Its principle is to calculate the flow field of the target point, and all moving targets share this flow field.

Guess you like

Origin blog.csdn.net/csDN_Smily/article/details/128665383