2D “Heat Seeker” Algorithm

conic sectionskinematicsvectors

I'm working on a 2D game where one of the weapons available to the player is the classic "heat seeker" projectile. Once fired, it finds a path to the target. But finding that path has turned out to be really difficult, which makes me think I must be overlooking something obvious.

The only inputs to the algorithm are:

  • $\vec v_0$ = the initial velocity of the projectile (the player's velocity)
  • $p_{player}$ = the player's position (which is the initial position of the projectile)
  • $p_{target}$ = the target's position, which is stationary

For simplicity, I assume the projectile just has a fixed acceleration vector, $\vec a$. There are infinitely many paths the projectile could take to hit the target, but using a fixed acceleration makes the path a parabola, which I thought would be easiest to solve.

So there's one additional input:

  • $\left \| \vec a \right \|$ = the scalar magnitude of the projectile's acceleration (i.e. how much "thrust" does it have, for example $10 m/s^2$)

Given these four inputs, I believe there is exactly one parabola that passes through both $p_{player}$ and $p_{target}$. The goal is to find $\vec a$, and I already know its length $\left \| \vec a \right \|$, so the only thing left to solve for is the angle of $\vec a$.

Can the parabola (and hence $\vec a$) be found by solving the plain old kinematic equations (below)? Maybe by plugging in $p_{target}$ and working back to find the required acceleration?
$$\vec a(t) = \vec a = constant$$
$$\vec v(t) = \vec v_0 + \vec a t$$
$$\vec p(t) = p_{player} + \vec v_0 t + \frac{1}{2}\vec a t^2$$

Best Answer

Let $v$ be the initial velocity of the projectile, $A$ its initial position, $B$ the target's position and $\alpha$ the angle between $v$ and ray $AB$ (see figure below). We must find the angle $\beta$ between the acceleration (of fixed magnitude $a$) and $AB$ so that the parabola passes through $B$. Note that $\beta$ is also the angle between $AB$ and the direction of the axis of the parabola.

We can decompose $v$ as the sum $v_x+v_y$ of vectors parallel to $AB$ and to $a$. The time taken by the projectile to go from $A$ to $H$ (which is the point on the parabola where the tangent is parallel to $AB$) can be computed as $v_y/a$ but also as $s/v_x$, where I set $s=AM={1\over2}AB$. That leads to the equality $$ v_xv_y=as. $$

From the sine law one also gets: $$ v_x=v{\sin(\alpha+\beta)\over\sin\beta},\quad v_y=v{\sin\alpha\over\sin\beta}, $$ and inserting these into the preceding equality we obtain the final equation: $$ \sin^2\alpha\cos\beta+\sin\alpha\cos\alpha\sin\beta={as\over v^2}\sin^2\beta. $$ One can extract $\cos\beta$ from there and insert the result into $\cos^2\beta+\sin^2\beta=1$ to find a quartic equation in $\sin\beta$. Explicit solutions can be found with the aid of a software, but they are too long to be of any help: in practice I think it's better to solve the equation numerically.

enter image description here