# Calculating a lead on a target

However, this may not converge. I’ll return to this because it’ll be there in any solution we find, but intuitively we can guess that it’s a case where our Target is moving as fast or faster than our Sniper’s projectile. Of course, this wouldn’t happen when we’re talking about humans and bullets, but we may not always be talking about that. Luckily with a recurrence relation like this, it’s easy to check if we’re converging, we simply watch the change in value between each and and make sure the number is getting smaller. If it grows or stays the same, we can’t solve the problem.

public float projectileSpeed;

public float lastError = Mathf.Infinity;

Vector3 CalculateLeadIterative (Vector3 previousPrediction) {

Vector3 V = targetVelocity;

Vector3 D = previousPrediction - sniper.position;

float dt = D.magnitude / projectileSpeed;

Vector3 pos = target.position + V * dt;

float thisError = (pos - previousPrediction).magnitude;

if (thisError >= lastError) {

Debug.LogError ("No solution exists");

lastError = -1; // -1 is signaling its wrong, never should be negative

} else {

lastError = thisError; // outside code checks when this is close enough to 0.

}

return pos;

}

Here’s an example player showing this code working.

Okay, so we have a solution but it’s not really efficient. If we could find a closed for solution, we could just plug the numbers in and get the answer back. Maybe that’s possible. Well let’s start with that recurrence relation we were using, but just get rid of the steps. There’s only one that we want, so let’s plug that in to both sides of the equation.

So, now we can’t easily find a solution for because its on both sides of the equation, so we have to do the hard work of seeing whether or not we can isolate it. I’m out of practice; there’s probably a shortcut I’m missing. In the end I’ll show the work in case there’s a mistake I’ve made along the way.

So first, I’ll move the out of the way.

Square both sides because it’s a vector magnitude over there and I want to get rid of that square root so I can pull it apart.

Because I don’t know a shortcut for this, I’m going to pull the vectors apart. But for now I’m just going to look at the x component because y and z will look exactly the same and we’ll just add them back in (again because we’re inside a vector magnitude).

Doing the work of the full squaring, we get this.

And then pulling together the like terms, we see we’re approaching a quadratic equation on , which is promising because solving a quadratic equation is straight forward.

That’s pretty close, but we can do one more simplification on those last three terms , that’s just a binomial. I remembered one shortcut. Yay!

Okay now, we need to pull in the other components, y and z. I’ll do this for each term, so it’s not too messy.

The first term after adding in the other components looks like . But we recognize that’s the square magnitude of , so really we have . This also works for the third term, which becomes .

The second term,

,

if we look closely, is a dot product between the vector and the vector , which becomes .

So putting it all back together we’ve got,

And then pulling the over, we finally have a quadratic equation.

Page 2 of 3 | Previous page | Next page