# Angle to intersect two projectiles

This topic is 3825 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've been working on the following problem and can't figure it out, so any help appreciated. This is for a 2D game. I have a gun I want to fire a projectile at speed u at a moving target with a constant velocity (speed s). Essentially, I want to figure out the angle the gun should point at, to ensure that the projectile and the target intersect exactly. Both will be travelling at constant speeds along a straight line. So if we let s = speed of target Px, Py = start position of target (on projectile firing) q = angle target is moving at u = speed of projectile Gx, Gy = start position of projectile (position of gun) a = angle to point gun at I did some maths and came up with a = acos( (s / u) . (((Gx - Px) sin q - (Gy + Py) cos q) / ((Gx - Px) - (Gy - Py))) Unfortunately, it doesn't work - when you stop the gun always points in the same direction regardless of where you are, and at some positions the result becomes undefined. I'm finding it tricky because you have to eliminate the unknown time t to the intersection, which I tried, but I must've stuffed something up... Can anyone help?

##### Share on other sites
Ah, I found a mistake in my original method. I essentially now just need to solve for a:

q cos a - r sin a = w

Not sure how to solve this one... anyone know the identities for this?

##### Share on other sites
I had to solve this exact problem when I was once writing AI code for a robot simulation. Unfortunately, I've somewhat forgotten my exact solution. I'll try to recall the algorithm I used (which worked very well):

Let P = <Px, Py> and G = <Gx, Gy>. Let v be the velocity of the target and let currentAngle be initially set to zero.

1) Compute the amount of time it would take for the bullet to reach the target if it remained stationary. Call this dt1.

2) Now advance the target forward dt1 units of time.

3) Compute how long it would take a bullet to reach the target if it were at the new projected position (firing directly at this location). Call this dt2.

4) Starting at its initial location P, advance the target forward dt2 units of time.

5) Compute how long it would take a bullet to reach the target if it were at the new projected position. Call this dt3.

6) Repeat this process with as many iterations as you like. Eventually the predicted position will coincide with exact point you should fire at in order that the bullet hit the target (because the difference dti+1 - dti tends to zero). You'll just need to play around with the algorithm to determine how many iterations you want. I used around 40 if I remember correctly.

Here's the same algorithm in pseudocode:

Vector2 targetVelocity = new Vector2(s * cos(q), s * sin(q));float ComputeFiringAngle(){     float timeToReachTarget = 0;     for (int i = 0; i < numberOfIterations; ++i)     {          Vector2 destination = PredictTargetPosition(timeToReachTarget);          float distance = Vector2.Length(destination - G);          timeToReachTarget = distance / u;     }     return Vector2.Angle(destination - G);}Vector2 PredictTargetPosition(float dt){     return P + dt * targetVelocity;}

##### Share on other sites
On the other hand, it seems to me that it should be possible to calculate this without iterative methods...

Unfortunately, I don't seem to be having much luck in seeing it tonight (perhaps due to being rather short on sleep ^^; ). >_<

However, it seems that you should be able to find a way to use the point-vector form of the line equations for the trajectories of the projectile and target to find the desired vector direction for the projectile, and from that get an angle.

This isn't as simple as normal line intersection, however, since we don't know both the time to intersection and the direction of the projectile. I think that one ought to be able to equate out the time variable, since it's equal in both cases, but I don't seem to be getting it at the moment.

All of that said, I haven't tried it, but a search turned up this thread, which has what might be a working solution. ^_^

##### Share on other sites
This problem comes up frequently on the forums. In fact, there was a thread on the topic fairly recently.

##### Share on other sites
That's interesting - I actually managed to come up with my own solution which works, and looks different to the one obtained in the other thread. I didn't use any vector math.

I came up with

Let m = Py - Gy
Let n = Px - Gx
Let w = (s sin q (Gx - Px) - s cos q (Gy - Py)) / u

then

a = asin(w / sqrt(m^2 + n^2)) - atan(m / -n)

If you replace atan with atan2, it gets the quadrants and works fine. I guess this boils down to the same thing as done with vector math but I did it all via simultaneous equations etc. If anyones interested, I could post the steps...

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 9
• 9
• 34
• 16
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015656
×