The algorithm of simple tracking projectile in flash

Source: Internet
Author: User
Algorithm I see some of the methods of tracking bombs, are to use the conversion of the limit, but also consider the bullet and the aircraft in a certain direction to make some adjustments in a parallel, personally think a bit annoying. And I'm using the direction vector to calculate the tracking bomb, no matter how the coordinate system changes, the relevant calculation is only one, the speed in the direction of the X,y "unit direction vector" calculated, you can. A friend who has learned vectors should know that the unit direction vector (x,y), where x is the cosine of the angle between this vector and the x axis, and y is the sine, and so what, now act. The core code is as follows:
function Getdistance (MC1, MC2) {//The distance between the two ——— bullets and my machine is computed.

Return Math.sqrt ((mc1._x-mc2._x) * (mc1._x-mc2._x) + (mc1._y-mc2._y) * (mc1._y-mc2._y));

}

function Stardandlizeangle (MC1, MC2) {//the direction vector that calculates the velocity of a bullet.

var L = getdistance (MC1, MC2);

MC1. vdirectionx= (mc2._x-mc1._x)/l;//The x MC1 in the unit direction vector. Vdirectiony= (mc2._y-mc1._y)/l;//y in the unit direction vector

}

function Domove (MC) {//figure out Vx,vy, through MC._X+=MC.VX//and Mc._y+=mc.vy; implement dynamic effect



MC.VX=MC.V*MC. Vdirectionx;

MC.VY=MC.V*MC. Vdirectiony;

MC._X+=MC.VX;

Mc._y+=mc.vy;

} _root.onenterframe=function () {

Stardandlizeangle (Bullet,plane);

Domove (bullet);

}
PS: The properties of the object I am another in a section as script with Mc.prototype. Property (method) =function () {[} Personally prefer this approach, especially when some physical properties are frequently annoying to use, this is like a small physical property library, very convenient.

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.