Many friends who have played flying shooters are impressed by the ability to track missiles, which are often invincible, but the enemy's missiles may be a nightmare for the player. In fact, the implementation of the missile tracking method is not complicated, only need some simple plane analytic geometry knowledge can be done.
The algorithm analysis assumes that the rotational angular velocity of the missile is Omega and the motion speed is v. Shows the initial state of the missile and the target in the axis.
At the initial moment, the target and the direction of the missile is Y, the position is (x1,y1), (x2,y2), the coordinates of the missile and the target, to get a line segment, the angle between the line and the y-axis is C; The angle of the missile with the-y direction is b,b, which is 0 The angle between the missile direction and the line segment is a,a that the missile still needs to be rotated. At this time
C=90-math.atan2 (y2-y1,x2-x1) *180/math.pi; b=0; A=c-b;
To conveniently calculate a positive value that converts C to 360 degrees:
C= (270+math.atan2 (y2-y1,x2-x1) *180/math.pi)%360;
After these values are obtained, the rotation angle and position of the missile at the next moment can be calculated. If a is less than the angular velocity Omega, the missile rotation angle A, just can point to the target, otherwise it will rotate Omega degree, so b=a<omega?a:omega; Copy code missile position becomes more
X2=x2+math.sin (b*math.pi/180) *v; Y2=y2+math.cos (b*math.pi/180) *v;
At the new moment, the target moves to a new position, while the missile rotates at a angle of B, as
The above calculation is then corrected to correct the position and rotation of the missile, so repeated until the missile hits the target or is destroyed due to a time limit.
Missile tracking algorithm