Reading Notes-ai in Game Development-Chapter 2-Chase and dodge

Source: Internet
Author: User

1. Composition of Chase/Dodge

Complete Chase/Dodge consists of three parts:
· Make a decision to catch up or escape (discussed later when talking about state machines and Neural Networks)
· Start to catch up or escape (the focus of this chapter)
· Avoid obstacles (Chapter 5 and chapter 6, further discussion)
2. Basic pursuit and dodge

Assume that the tracker coordinates (predatorX, predatorY) and the Dodge coordinates (preyX, preyY)
Basic chase code in a continuous Environment

If (predatorx> preyx) predatorx --;
Else if (predatorx <preyx) predatorx ++
If (predatory> preyy) Predatory --;
Else if (predatory <preyy) Predatory ++

Basic Dodge code in a continuous Environment

If (preyx> predatorx) preyx ++;
Else if (preyx <predatorx) preyx --;
If (preyy> predatory) preyy ++;
Else if (preyy <predatory) preyy --;

The basic Chase/Dodge code of the brick environment is very similar to that of the continuous environment, which is ignored here.

3. Line of sight chase
Why do we need to keep track of sight in the 3.1 brick environment?


Figure-simple Chase and line-of-sight chase in the brick Environment

The image on the left is simple pursuit, and the image on the right is the image on the line of sight.
Simple pursuit is certainly the shortest path, but it is not necessarily possible to obtain a visual straight line, while straight line pursuit will be much better visually.
In addition, when a group of attackers gather together to players, simple pursuit will make them go along the diagonal lines to the nearest coordinate axis in the coordinate system with the target as a distant point, then, go along the axis to the target. This is equivalent to arranging them into a column to initiate an attack. It is more reasonable to approach the target from different directions.
3.2 How to Achieve line-of-sight chase in the brick Environment
The book uses the so-called bresenham algorithm, which is one of the most effective ways to draw lines in a graphic environment. The original bresenham requires division to calculate the slope, but the simplified bresenham version avoids the application of Subtraction by multiplication.
First, bresenham is indeed a good algorithm, because it can ensure that the entire path is similar to a straight line. Instead, it is not easy to make the two steps even. breseham is more obvious.
For more information about Bresenham algorithms, see worker. Multiply the formula by Dx to obtain the derivation process without division.
3.2 line of sight chase in a continuous Environment
In the continuous environment discussed in this section, line-of-sight Chase is the simplest Chase algorithm, but it takes into account the tracker's moving speed, not just wired speed, but also angular speed. The algorithm is to first redirect the direction to the line of sight based on the angular velocity, and then chase the target. This section describes the global coordinate system and local coordinate system.


Figure-Coordinate System

How to construct a local coordinate system, that is, how to determine the X axis and Y axis of the local coordinate system. In fact, I think that the current direction of the chaser is the positive direction of the Y axis (What should I do if the current chaser is still? This is unknown), the positive direction of the X axis is the positive direction of the Y axis to rotate 90 degrees counterclockwise. The key to coordinate conversion is the angle. Based on the above formula, we can bring the global coordinates (x', y') and local coordinates () of the chaser to calculate the cosine and sine of the angle. In fact, the local coordinate system is used because there are ready-made functions that can help coordinate system transformation. After coordinate conversion, it is more convenient to use. The following code also shows that when you determine whether to turn left or right, you only need to determine the positive and negative values of the x coordinate of the line of sight vector, of course, the convenience here is closely related to the parameters of local coordinate conversion, that is,-Predator. fOrientation. It is not clear yet. I have never touched the VRotate2D function.

Void dolineofsightchase (void)
{
Vector U, V; // U pursuit vector, V prey Vector
Bool left = false; // whether to turn left
Bool right = false; // whether to turn right
U = vrotate2d (-predator. forientation,
Prey. vPosition-Predator.vPosition) // vector of the line of sight in the Local Coordinate System
U. Normalize (); // Normalize
If (u. x <-_ TOL) // you can specify the rotation direction.
Left = true;
Else if (u. x> _ TOL)
Right = true;
Predator. SetThrusters (left, right); // rotate
}

4. Interception

The basic principle of the interception algorithm is that it can predict the future position of prey and then directly go to that position, so that it can reach the same position as the prey at the same time. In order to find out the points at the same time that tracing and prey can reach, we should not only consider their moving direction, but also their speed. In fact, it is very simple. First, we can predict the shortest time for tracking the prey, that is, the close time = relative displacement/relative speed. If the tracking cannot change the direction, the close time may not exist, for example, if the relative displacement is the opposite of the relative velocity, it is never possible to move closer. This section briefly describes the situation. With close time, we can predict the position of prey after the close time based on the speed and initial displacement of prey. In this way, only this position can be used for tracking, just use the line of sight method to chase this position.
Is this Interception Algorithm correct? There is still a problem with the feeling. According to the book, the velocity vectors and displacement vectors of prey and chaser are fixed because the calculation of close time requires relative displacement and relative velocity. However, if these are fixed, it is easy to get the problem that the two may not meet. A more reasonable explanation should be that the velocity vector of prey is fixed with the initial position vector, while the initial position and speed of the chaser are fixed, but the speed direction is not fixed, in this way, the direction can be adjusted to achieve interception. The calculation can adopt the method of moving closer to time, but it is just an equation.
5. Summary
This chapter provides some basic methods. Chapter 5th also describes the use of potential functions to chase or dodge.

6. Practice

The appendix shows the line-of-sight chase in my self-implemented brick environment. Three algorithms are available, the first is the basic method, and the second is the bresenham method, the third method is to use division and modulo to calculate the line of sight. The last two methods are very similar in some cases. Of course they are also different. I personally think bresenham is better. After all, there is theoretical support.
Chase demo in brick environment: http://files.cnblogs.com/pangxiaodong/chasing.swf
Program:

Figure-line-of-sight chase demonstration in brick Environment

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.