Play-tracking algorithm in Game

Source: Internet
Author: User

Shooting at flightGamesI'm afraid no game has to be trackedAlgorithm. For example, when playing arcade games, is it often stuck in the "tracking bullet" of Boss "?
How is this implemented? It's easy, as long as you have a higher level of mathematical knowledge!
First, recall several trigonometric functions.Sin, cos, Tan (TG), arctan (arctg)
Sin (X)-Opposite side/diagonal side. The values of 1 and 2 are positive, and those of 3 and 4 are negative.
Cos (X)-Adjacent side/oblique side. The limit of 1, 4 is positive, and that of 2 and 3 is negative.
Tan (X)-Peer or adjacent edge. The values of 1 and 3 are positive, and the values of 2 and 4 are negative.
ConsideringCoordinatesSystem:

Assume that the coordinates of the enemy bullet are slug. x, slug. y. The speed of the bullet is slug. the oblique side of the triangle above speed (all double type) represents the speed of the bullet. The coordinates of the bullet are changed to slug each time it moves. X + = slug. speed * Cos (theta); slug. Y + = slug. speed * sin (theta); when an enemy bullet is fired at you, you must first calculate the angle between the bullet position and the angle of the clip in your position. Theta is simply calculated as follows: double deltax = player. x-slug. x; // note, is the starting point of the protagonist in the x1-x0 double deltay = player. y-slug. y; // y1-y0 in order to prevent the denominator from being 0, make a judgment so that the denominator is approximately 0. Is it negative or positive? In this case, we need to compare the bullet with your Y coordinate, who is big and who is small. If (deltax = 0) {If (player. y> = slug. y) // The bullet needs to move down deltax = 0.0000001; else // The bullet needs to move up deltax =-0.0000001;} Similarly, if (deltay = 0) {If (player. x> = slug. x) // The bullet needs to be shifted to deltay = 0.0000001; else // The bullet needs to be shifted to deltay =-0.0000001 ;} now, we can determine the condition where the angle is located. If (deltax> 0 & deltay> 0) angle = atan (FABS (deltay/deltax )); // The first else if (deltax <0 & deltay <0) angle = π-atan (FABS (deltay/deltax) // The second else if (delt Ax <0 & deltay <0) angle = π + atan (FABS (deltay/deltax )) // else angle = 2 π-atan (FABS (deltay/deltax) for the third item. // For the fourth item, set π to 3.1415926 ............ (Oh, don't forget the approximation.) Well, now we get the correct direction. You can calculate the bullet coordinates! Slug. x + = slug. Speed * Cos (theta); slug. Y + = slug. Speed * sin (theta );

  

 

In this way, how do I make a judgment and re-calculate the angle before each bullet moves? Why did the tracking pop up?

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.