One of my favorite games, and by far the one I used the most time creating content
for during my youth is Escape Velocity Nova
. The Escape Velocity series are 2D space RPGs, which means there is lots of 2D space combat. In these types of games you typically have three main types of weapons, non-turreted, turreted and homing. Non-turreted and homing weapons are not really that complicated. The non-turreted fire in one direction (typically forward), while the homing weapon typically turns as hard as it can towards the target at maximum speed. Turreted weapons were always a puzzle to me though, they somehow knew where you would be, and fired to intercept your ship. I always wondered how this was done.
Some years later during a visualization class (on how to find intersections between geometric primitives) I was wondering if I could use geometric equations to figure out where a spaceship needs to aim its torpedo, if it knows the position and velocity of the target. Below is the solution I came up with. It assumes constant speed for the target and the torpedo.
The positions of something moving at constant speed, can be described by this equation where
the current velocity and
is the starting position. This restricts the position of the target to a line, where the position on the line is determined by
For 2 dimensions this is:
The possible locations
of a constant speed (
) bullet fired from point
in any direction, are given by this equation, where again time is denoted by
. This restrict the possible positions to a circle which expands with time.
For 2 dimensions this is:
Now inserting the restrictions on
from 2 into 4 gives a polynomial that only depends on
Simplifying by setting constants
and moving towards standard form:
Rearranging to standard form:
This is a quadratic equation, with coefficients:
Solving the quadratic equation gives two solution for
, if an answer is positive and has no imaginary part, there is a solution to the problem.
Inserting a solution for
in equation 1 gives the
coordinates where the torpedo should be aimed, and where it will hit the target.
Below is an implementation of this, where the constant speed of the torpedo can be set. If there are two solutions, the smallest
is chosen. If there is no solution (typically because the target it too fast for the torpedo to catch) the torpedo is never launched.
As you can see in some cases, this does not take into account the size of the target, only the targets center. This can be remedied by using a representative selection of boundary points of the target instead, and firing if one of them would connect.
The solution does also not take into account how fast the target can change direction, or when the torpedo runs out of fuel. This can be handled by not firing if the time from the torpedo is fired to the impact is too long.
This method can also quite easily be expanded for an accelerating weapon or target. This will result in a higher degree polynomial though, which are mostly only possible to solve using numerical methods like Newtons's method