God-level programmers teach you to use Python for simple missile auto-tracking! This is the installation of force God skills!

Source: Internet
Author: User

Since we're going to demonstrate with Pygame, his coordinate system is the y-axis downward, so here we also use the Y-downward coordinate system.

Calculated Sina and Cosa, sinusoidal contrast oblique, Yu Ying ratio oblique, the hypotenuse can be calculated using the two-point distance formula, namely:

So

The length of the AC is the speed of the missile multiplied by the time | AC|=VT, and then you can calculate the length of the ad and CD, so this one time slice past, the missile should appear in the new position C point, his coordinates is the old point a x increase ad and y minus the CD.

So, the new C-point coordinates are:

Just keep repeating this operation over and over, okay, for a better image, put the first time slice and the second time slice together to see:

The very simple code is as follows:

If the missile is only considered a particle, then the above algorithm is sufficient, I did not do the rotation of the missile, because a particle does not have to split the tail does not need to rotate, of course, this premise is that you load the missile picture is very small when the rotation does not seem to be a problem. But it's not easy to spin in Pygame (or maybe I'm ignorant), okay? Let's replace the picture with a rectangle, and then add the rotation function to see how it works.

Because the coordinate point of the picture is the point of its upper-left corner, so if we want the coordinates of the image to be fixed at the Arrowhead Point, then the actual print position of the picture is reduced by the length of the image, and y is reduced by half the width.

But the actual performance is not good:

The direction is the same, but the arrow point of the picture doesn't follow the mouse all the time. After my research (just because this problem has not been resolved has not been released),

I found out that this is the mechanism of the rotation of the diagram, we look at the rotation of the picture becomes what:

After the rotation of the picture into the blue range, depending on the angle of rotation, the size of the picture is different, we look at the situation of rotation 90

In this way, the points of the two missile heads are identical. Next we analyze the algorithm of the missile head after the rotation. Depending on the angle of rotation, the rotation angle differs in different quadrant parameters, so we are divided into these four cases

Quadrant

3,4 quadrant, which rotates only plus or minus 0-180, so the 3,4 quadrant is the negative angle

We'll move him when we show the picture.

    1. screen.blit(missiled,(x1-width+(x1-C[0]),y1-height/2+(y1-C[1])))

Here (X1-WIDTH,Y1-HEIGHT/2) is actually in the (X1,Y1)

So finally we add the relevant algorithm code, the effect is more perfect

Complete, and finally attach the entire algorithm code

Welcome everyone to join million people exchange, resource Sharing Group:: 125240963

God-level programmers teach you to use Python for simple missile auto-tracking! This is the installation of force God skills!

Related Article

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.