1
\$\begingroup\$

I'm having an issue with calculating the predicted linear angle a projectile needs to move in to intersect a moving enemy ship for my 2D game.

I've tried following the document here, but what I've have come up with is simply awful.

protected Vector2 GetPredictedPosition(float angleToEnemy, ShipCompartment origin, ShipCompartment target)
{
    // Below obviously won't compile (document wants a Vector, not sure how to get that from a single float?)
    Vector2 velocity = target.Thrust - 25f; // Closing velocity (25 is example projectile velocity)
    Vector2 distance = target.Position - origin.Position; // Range to close
    double time = distance.Length() / velocity.Length(); // Time

    // Garbage code, doesn't compile, this method is incorrect
    return target.Position + (target.Thrust * time);
}

I would be grateful if the community can help point out how this is done correctly.

EDIT

Here is my (working) method following the advice given:

protected Vector2 GetPredictedPosition(ShipCompartment origin, ShipCompartment target, Ship enemyShip, Ship firingShip)
{
    Vector2 targetVelocity = enemyShip.Direction * enemyShip.Thrust;
    Vector2 projectileVelocity = firingShip.Direction * 25f;

    Vector2 velocity = targetVelocity - projectileVelocity; // Closing velocity
    Vector2 distance = target.Position - origin.Position; // Range to close
    double time = distance.Length() / velocity.Length(); // Time

    return target.Position + new Vector2(Convert.ToSingle(targetVelocity.X * time), Convert.ToSingle(targetVelocity.Y * time));
}
\$\endgroup\$
2
  • \$\begingroup\$ "I'm having an issue with calculating the predicted linear angle..." But the algorithm in the article doesn't return an angle, and if it wants a velocity in components, it's implied you already know the angle of the velocity. What exactly are you trying to calculate? \$\endgroup\$
    – Chaosed0
    Jun 12 '14 at 20:49
  • \$\begingroup\$ My apologies, I should explain better. I want to return the predicted position that the target will be in. With this I can get the appropriate angle in which to send my projectile so it will intersect with the target (that is, if the target maintains velocity and direction). \$\endgroup\$ Jun 12 '14 at 20:55
1
\$\begingroup\$

Speed == a single value (like your 25).

Direction == a normalized vector pointing in the direction an object is traveling in.

Velocity == Direction * Speed. The result here is a vector.

The author of your guide is refering to a velocity vector, not a speed value.

For instance, Vector2(0,1) and Vector2(0,2) can both be thought of as velocity vectors that are going in the same direction but the 2nd one is going there twice as fast.

If you are accustom to thinking of direction as an angle as opposed to a vector, then you need to convert your direction angles to direction vectors before using this method.

float targetDirectionAngle = someArbirtaryAngle;
Vector2 targetDirection = new Vector2((float)Math.Cos(targetDirectionAngle ), (float)Math.Sin(targetDirectionAngle ));
targetDirection.Normalize();
Vector2 targetVelocity = targetDirection * speed;//usually speed is in pixels(units) per second

now targetVelocity is ready to plug into your method.

\$\endgroup\$
1
  • \$\begingroup\$ Brilliant answer, you made it very easy to understand. Thank you. \$\endgroup\$ Jun 15 '14 at 18:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.