# How to calculate the vector of an interception?

Given are a twodimensional space, and 1 friendly spaceship standing still, one foe is moving NOT directly to the friendly ship with known actual position, speed and direction.

The friendly ship wants to get itself into firing range to battle the foe.

Actually I am setting just a direct vector to the actual position moving ship, and recalculate it every frame, resulting in some kind of "round" flightpath.

What I want is to set a direct and straight path to the position that the foe will (presumably) will have when firing distance will be reached, assuming that foe will not change course until then.

As a first and "simple" implementation it would be enough if we assume the friend can speed up from 0 to max in no time.

Preffered implemantation would be one that considers the acceleration capabilities of the friend, and knows when interception is impossible because of the speed. It should work for every starting speed, not only from stand still. A plus would be if it even considers braking (battling at lightspeed is very energy inefficient in the given universe)

-

If I understand your question, you do not want the ship to steer to the target, but rather to fly in a straight line that happens to intercept the target. I'm making a tower defense game that basically has the same need for a tower's bullet, a tower wants to fire a gun such that the bullet will intercept a moving target as long as it doesn't change speed/direction. The way I solved it was by using a quadratic equation. Here is some pseudo code:

``````Vector totarget =  target.position - tower.position;

float a = Vector.Dot(target.velocity, target.velocity) - (bullet.velocity * bullet.velocity);
float b = 2 * Vector.Dot(target.velocity, totarget);
float c = Vector.Dot(totarget, totarget);

float p = -b / (2 * a);
float q = (float)Math.Sqrt((b * b) - 4 * a * c) / (2 * a);

float t1 = p - q;
float t2 = p + q;
float t;

if (t1 > t2 && t2 > 0)
{
t = t2;
}
else
{
t = t1;
}

Vector aimSpot = target.position + target.velocity * t;
Vector bulletPath = aimSpot - tower.position;
float timeToImpact = bulletPath.Length() / bullet.speed;//speed must be in units per second
``````

I found this to work so well I did not need collision detection for the shot... I was able to count on every shot hitting a bulls eye regardless of distance/direction/speed of target as long as those factors remained steady.

-
From your description this seems to be what I am searching for, at least the easy way assuming instant acceleration to max speed. I'll take a closer look at this in the evening. Am I assuming right that Vector.Dot returns the dotproduct of the to vectors? –  NobbZ Jul 7 '11 at 12:33
Hmmm... I have done this in ruby now, but there seems something wrong. Everytime I try, there is an exception thrown, because the expression in the sqrt evaluates to something negativ and is therefore out of bounds. How can I handle this. Sorry for the question, but I can only use this, but dont understand the conepts here until someone give me an advice. –  NobbZ Jul 7 '11 at 16:09
The example was from this book:amazon.com/… –  Steve H Jul 7 '11 at 17:43
don't know if this helps but here is some python code that accomplishes the same thing. moddb.com/mods/wicmw/tutorials/… –  Steve H Jul 7 '11 at 17:44
OK, I still does not understand the hole mathematics behind, but thx for the python code, the documentation told me, that if there is a negativ value inside the sqrt, then my friend is to slow to catch up. After tweaking my test values I get some results. Thx for your help. –  NobbZ Jul 7 '11 at 19:31

I suggest you look into steering behaviors. Especially pursuit. Source-code can be found in the OpenSteer implementation or look up a book like "Programming game AI by example" (ISBN 13: 978-1556220784)

-
pursuit seems to need knowledge about the target and steers towards that, but I dont know the target really. I know where foe is now, I know its speed and its direction. Now I want to know which direction it has to go to intercept foe on its way to its target as soon and fast as possible. As mentioned before acceleration can be ignored at first, this would even save much of processing time compared to the actual version... With the new model I have to recalculate only when the foe fires a "coursechange"-event, not for every "hasmoved"-event as I do it now. –  NobbZ Jul 6 '11 at 19:03
Yes, what you describe is pursuit. It doesn't know the target.. it makes a prediction based on the "foes" current location, speed and direction –  bummzack Jul 6 '11 at 20:18
Then I have missunderstood the description, I'll take a closer look at it tomorrow. –  NobbZ Jul 6 '11 at 20:45
+1 Great resources. –  TreDubZedd Jul 6 '11 at 21:44
+1 since I love that book –  Ray Dey Jul 6 '11 at 22:35