The algorithm of game tracking projectile

Source: Internet
Author: User
Tags cos sin

Welcome to Unity Learning,Unity training ,UnityEnterprise TrainingEducation Area, there are manyUnity3d Resources,Unity3d Training Video,Unity3d Tutorials,Unity3d Frequently Asked questions,Unity3d Project Source code, the "Dog Planing Learning Network" Unity's Ultimate Academy, dedicated to building the industry unity3d Training , learning the first brand.
In the flying shooting game, I'm afraid there is no game without a tracking algorithm. For example, when playing the arcade, is not often hung in the boss's "tracking bomb"?
How is this achieved? Very simple, as long as there is a little math knowledge in high school!
 
First recall several trigonometric Sin,cos,tan (TG), arctan (ARCTG)
Sin (x) on the Edge/hypotenuse. In 1, 2 items are limited to positive, 3, 4 are negative
cos (x) adjacent edge/hypotenuse. In 1, 4 items are limited to positive, 2, 3 are negative
Tan (x) on the edge/adjacent edge. In 1, 3 items are limited to positive, 2, 4 are negative
  
Take into account that the coordinate system in the game is as follows:
  

  

Assuming that the enemy bullet coordinates are SLUG.X,SLUG.Y, the bullet's speed is slug.speed (all double type)
The hypotenuse of the triangle above represents the speed of the bullet, and the change in the coordinates of each time the bullet is moved is:
slug.x + = slug.speed * cos (theta);
SLUG.Y + = Slug.speed * sin (theta);
  
When an enemy bullet is fired at you, the first thing to do is to calculate the position of the bullet and the angle theta
The simple calculation is:
Double deltax = player.x-slug.x; Note that the position of the protagonist as the starting point is x1-x0
Double DeltaY = player.y-slug.y; Y1-y0
  
In order to prevent dividing the denominator is 0, make a judgment, so that the denominator is approximately 0, whether it is a negative approximation or positive approximation? This requires comparing bullets and your y-coordinates who are big or small.
if (DeltaX = = 0)
{
if (player.y = slug.y)//The bullet needs to be moved down
DeltaX = 0.0000001;
else//bullets need to move up
DeltaX =-0.0000001;
}

In the same vein, Judge DeltaY.
if (DeltaY = = 0)
{
if (player.x = slug.x)//bullets need to move right
DeltaY = 0.0000001;
else//bullets need to move left
DeltaY =-0.0000001;
}

Now judge the terms of the angle
if (deltax0 deltay0)
Angle = Atan (Fabs (deltay/deltax)); The first limit
else if (deltax0 deltay0)
Angle =-atan (Fabs (Deltay/deltax))//second limit
else if (deltax0 deltay0)
Angle = +atan (Fabs (Deltay/deltax))//third limit
Else
Angle = 2-atan (Fabs (Deltay/deltax))//Fourth limit

which takes 3.1415926 (hehe, don't forget the approximate OH)

Well, now that we've got the right direction, we can calculate the bullet coordinates!
slug.x + = slug.speed * cos (theta);
SLUG.Y + = Slug.speed * sin (theta);

So, before each bullet moves, make a judgment, recalculate the angle, how about? Is the "Trace" coming out?
  
For more information, please visit the "Dog Planing Learning Network" Unity Ultimate Academy Http://edu.gopedu.com

Statement: This document comes from the "Dog Planing Learning Network" Community-unity Extreme College, is a self-published Unity3d study articles, if anything violates your relevant interests, please communicate with the official, we will deal with the real-time.


The algorithm of game tracking projectile

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.