Several common tracing algorithms in games

Source: Internet
Author: User

Compared with another role, tracing is a process of escape. First, you must make a decision to track and escape.

1. Coordinate tracking
It is also the most basic method of tracing. he modifies the tracker coordinates based on the coordinates of the object to be tracked, and gradually shortens the distance between the two.
A simple example:
Point m_pprey; // Tracker
Point m_patta; // Tracker
For Tracker: New Position = old position + XY speed;

1if (m_patta.x <m_pprey.x)
2 m_patta.x ++;
3 else if (m_patta.x> m_pprey.x)
4 m_patta.x --;
5
6
7if (m_patta.y <m_pprey.y)
8 m_patta.y ++;
9 else if (m_patta.y> m_pprey.y)
10 m_patta.y --;

 
2. Line of sight tracking
The line-of-sight tracing method is used to describe that the tracker will move along a straight line between the chaser at every moment. :

The graph can better describe this problem. The key to solving this problem is to find a straight line connecting the tracker and prey. You can know through the vector: two vectors to be reduced.
Two vectors can be constructed using the coordinates of the tracker and prey respectively. Assume that B Represents the tracker's position vector, and a represents the prey's position vector. After vector subtraction a-B, vector C is obtained, and the starting point of C is placed at the tracker's position. Then, a vector C pointing to the prey is obtained:

Tracker X-Direction Speed/tracker y-Direction Speed = C vector x-axis component/C vector Y-axis component.

3. Interception tracking
The so-called interception and tracking, if you consider the target to be pursued too far, if the two are the same speed, or the difference is not big, it may be difficult to catch up, all those who have played live football know, if you use the above 2 pursuit method, you may miss the best defensive position. The following is an example of interception tracing:


For a tracker, he only needs to know the position, direction, and speed of the tracker, and will calculate an optimal interception position. Then you will find that this is just a simple tracing problem. The minimum time t is required.

The source code and demo of the entire three types of tracing are shared:
/Files/expter/chase.rar

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.