# Calculating a lead on a target

Recently, Quadgnim, on the Unity 3d forums, asked me if I could help him figure out how the code for leading a target would work.  I thought about it for a minute, and then thought about it for a few more minutes.  And now after several hours, I’ve decided it was one of the more fun derivations I’ve done in a while.  So I’m going to share.

First, I’ll lay out the problem.  Consider a sniper, “A” and a target, “B”.  Sniper A is stationary and trying to track a target who is moving, for simplicity at some fixed speed in some arbitrary 3-dimensional direction.  Sniper A wants to fire on B, but if he fires where he sees B, then by the time the bullet arrives, B may have moved out of the way.  So the Sniper must aim at where B will be when the bullet get there.

Here’s a picture of the situation.

A fires on B with the solid red line. But B has moved to the dotted position B prime. A should have fired with the dotted red line.

This would be very simple if B were moving directly toward A or directly away from A.  Then we could either subtract or add that speed to our projectile speed and solve.  But we must concern ourselves with the perpendicular speed as well.  So where do we start?

In order to solve this, we must find a missing variable.  We know all the variables except time, that is the time at which our projectile will hit the target if we aimed properly.  Technically we don’t know where to aim, but if we knew the time, then because B is moving at a steady velocity we would just aim at B’s position plus the distance he travelled over that time, that looks like this

$P_b + V_b \Delta t$, where $P_b$ is B’s position and $V_b$ is B’s velocity.

So all we need to do is find the $\Delta t$ and plug it into that equation and that’s where we know B will be and thus where Sniper A should aim.

But how do we actually solve this?  Well, let’s start with the naive approach: Lets just fire directly at B and solve for that $\Delta t$.  That’s pretty straight forward, we just need to find the distance from Sniper A to Target B and then divide by the speed of the projectile.  That looks like this:

$\Delta t_{naive} = \frac{||P_b - P_a||}{s}$ where $P_a$ and $P_b$ are A and B’s positions and $s$ is the speed of the projectile.

But the problem here is that we’ve missed the target by $V_b \Delta t_{naive}$.   Now we could take the approach that we find an initial $\Delta t$ this way, call it $\Delta t_1$ and then use that to compute a new $P_b$ and find a $\Delta t_2$.  That would look like this:

$\Delta t_2 = \frac{||P_b + V_b \Delta t_1 - P_a||}{s}$

We can continue to iterate this recurrence relation this way, finding $\Delta t_3, \Delta t_4 \ldots$ and so on.  This is a pretty good way to solve a non-linear problem like this, if we don’t know of a closed form solution.  We just keep iterating until, if we’re lucky, we get a converged value, a $\Delta t^*$.

First find the time it takes to reach B0, then compute how far B has moved to B1. Re run the algorithm using B1 to find B2 and so on.

Page 1 of 3 | Next page