Unity2D-monster AI heuristic pathfinding algorithm (multi-target, arbitrary monster size, attack range)

The complete project download is at the end, and the effect first:Please add a picture description

The red square is the number 1 of the monster with a size of 2x2.
Yellow is the target number 3.
White dots are obstacles.
The monster's attack range is 2x2 (its own size).
(Here, for the convenience of viewing, each step of the algorithm is delayed)

Please add a picture description

The size of the monster is 1x1, and the attack range of the monster is 1x1 (its own size).
The red path that appears during the search is the path, and the red path that is finally redrawn is the path that allows walking in an oblique direction.

Implementation steps

  1. Use BFS (breadth-first search)
    to add the positions around the current search position to the search queue each time.

The surrounding position is an abstract concept. If it is added as up, down, left, and right, and 4 directions, then the optimal path will only be selected in 4 directions each time.

To expand:
Q: What if I need to walk diagonally at any angle? What if I need to walk at a 45 degree angle?
A: Then you only need to change the definition of the surrounding positions.

Add priority

In fact, the BFS is prioritized, and each time the target is selected, it is searched from the optimal value we calculated. (In this way, the search is purposeful)
That is: BFS + priority queue = A*, (this is almost the core idea of ​​A*, the priority queue is just a way to choose the best)

How to find the optimal path

We can first assume that we have found the optimal path, so what are the properties of the optimal path?
For each point, I can always reach it, but the way of reaching it is different, resulting in a different number of steps to reach. Therefore, we can record the minimum number of steps used to reach this point in the map. If there is a move with a smaller number of steps, we overwrite it.
By recording the direction from which the smallest number of steps came from we can restore the entire path. (Of course, it is the same for any direction, such as 8 directions).

insert image description here

Implement the collision volume

There are actually only two parts that need to be dealt with to achieve volume.

  • Where is the center of the object?
  • How to represent the volume of an object
    When we determine the representation method of the volume, it is simple:
    we only need to use the collision check for each volume unit during the search process!

How to search multiple targets dynamically

  1. One of the simplest methods is to search multiple times, each time searching for a different target.
    The disadvantage of this method is obvious, that is, as the number of targets increases, the number of searches will continue to increase.
    And in the search of the first target, we already know the shortest source of many places, but we need to search again when searching for the second target, which is really a waste.

The demo image at the top uses this approach. Switching to multi-target simultaneous searches is easy.

  1. Simultaneously search for multiple targets
    Each search calculates and compares the distances of multiple targets, and only takes the shortest path.
    The advantage of this method is obvious, many targets can be searched, and the shortest one can be quickly selected.

The following is the path to use to search multiple targets at the same time and only find the closest target.
Please add a picture description

Configuration information used in the demonstration:
insert image description here

Project complete demo package:
https://download.csdn.net/download/qq_41709801/86911269

Guess you like

Origin blog.csdn.net/qq_41709801/article/details/127689213