Homing Missiles, How Do They Work?

2019-03-07 07:34发布

问题:

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.