Homing Missiles, How Do They Work?

南楼画角 提交于 2019-12-20 07:58:38

问题


What I am trying to create

  • An object that will launch a missile towards the player, if it collides with player, player dies.

The Problem

  • How does the missile move towards the player.

  • How do I make the missile move so that it does not instantly move directly towards the player(to slowly move on an angle).

I have a formula for the mouse to be the "Player" and the missile to go towards it.

        mouse = Mouse.GetState();

        mousePosition = new Vector2(mouse.X, mouse.Y);

        A = (mouse.X - Position.X);
        B = (mouse.Y - Position.Y);
        C = (A * A) + (B * B);

        C = (float)Math.Sqrt(C);

        Angle1 = A / C;
        Angle2 = B / C;

(Drawing is the best!)

This only gets the distance from missile to player, now I need to find the angle or something to move the missile, what do I need to do?


回答1:


You can get the distance from one point to another, and turn that into a direction to go to.

//Find the delta time
float delta = (float)gameTime.ElapsedGameTime.TotalSeconds * 60;
//Find the direction
Vector2 direction = mousePosition - misslePosition;
direction.Normalize();
//Move towards it
currentPos += direction * delta;

It needs to be multiplied by the elapsed time so it appears the same no matter what FPS you are running at.

You may need to adjust for speed, but it should create a pattern like this:

If you would like the missle to slowly turn towards the target, you can experiment with MathHelper.Lerp to slowly change the angle.



来源:https://stackoverflow.com/questions/22690424/homing-missiles-how-do-they-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!