# Calculate intersect point / aim point

I'm working on a simple game, and I need to calculate the position to aim.

Since my math days are a bit behind me, I put together a simple example to illustrate:

So given that I know the position of the two objects, and the velocity, how do I calculate the point at which they intersect (i.e. the point to aim for)?

EDIT: To clarify, the cannon is fired at time 0, there is no waiting.

• how do you know for sure that they intersect ? – dimitris93 Mar 25 '15 at 0:35
• @Shiro, because I decide the angle of the cannon. – Sarke Mar 25 '15 at 0:36
• so you have manually placed those objects in a way that when they move with those specific speed values, they intersect ? Answering this question is not possible without the canon ball moving angle – dimitris93 Mar 25 '15 at 0:38
• @Shiro, I don't think you understand the problem. The cannon, given the right angle, will always be able to hit the car (the question is where). Their starting points don't matter. – Sarke Mar 25 '15 at 0:41
• The question is not very clear. The way you put the question in the comments, the canon ball moving angle is a variable. That means that you are not only looking for the intersection point. You are looking for a (canon ball moving angle, intersection point) pair. And there is an infinite number of those pairs. And the canon and car starting points do matter – dimitris93 Mar 25 '15 at 0:45

I was able to solve this using trig instead of vector math. Here's how it looks as a triangle.

Notice that since this is meant for a computer coordinate system, the y+ axis is down. Also the angles are as such: x+ = 0°, y+ = 90°, y- = -90°, and x- = ±180°

Additionally, we know that line B's angle is ∠B = -10°.

The speeds don't matter since they can be expressed as a ratio r = 120 / 25. So A can be expressed as B * r, as shown.

We can calculate the length and angle of C as:

C = sqrt((b.x - a.x)² + (b.y - a.y)²) ≈ 10.05

∠C = atan2(b.y - a.y, b.x - a.x) ≈ 84.29°

therefore:

∠a = ∠C - ∠B ≈ 94.29°

Now we have all we need to use the Law of Cosine, which states:

A² = B² + C² - 2*B*C*cos(a)

Since we know that A is a ratio of B, we can express it like so:

(B*r)² = B² + C² - 2*B*C*cos(a)

Solve for B and we get:

B = ± ( sqrt( C² * (cos(a)² + r² -1 ) ) - cos(a)*C ) / (r² - 1) ≈ 2.18

Since time is not a factor in this equation, we get two results, depending on if we're forward in time or backwards. We'd have to check that the B we get is in the right direction.

Finally, we now know enough to calculate what we want:

c.x = a.x + B * cos(∠B) ≈ 4.14
c.y = a.y + B * sin(∠B) ≈ 1.62


For bonus points, if we wanted the shooting angle ∠A, we can get it like so:

∠A = atan2(c.y - b.y, c.x - b.x) ≈ -83.72°

I tried to solve it following @david van brink comment.

P: intersection point (unknown)
S1: car's starting point(2,2 here)
S2: canon's starting point(3,12 here)
|v1|: car's velocity length (25 here)
|v2|: canon ball's velocity length (120 here)
a: angle between the car's velocity and the X-axis (10 degrees here... or Pi/18)
b: angle between the canon ball's velocity and the X-axis (unknown)

(1): P.y = tan(a) * P.x + S1.x (This is true because P should be a point on the car's path, so it is on a straight line y = a*x + b)

(2): Distance(P,S1) / Distance(P,S2) = 25/120 (This is true because, for example, if the canon ball had double the speed of the car, then, at any given time, the distance covered from the car is half the distance covered from the ball)

(3): b = arctan((P.y - S2.y)/(P.x - S2.x))

From (1) and (2) ==> {

A = (1 + tan(a)^2) * (|v1|^2 * |v2|^2)

B = |v2|^2 * (2 * tan(a) * (S1.x - S1.y) - 2 * S1.x) +
|v1|^2 * (2 * tan(a) * (S2.x - S2.y) - 2 * S2.x)

C = |v2|^2 * (2 * S1.x^2 + S1.y^2 - 2 * S1.y * S1.x) -
|v1|^2 * (2 * S2.x^2 + S2.y^2 - 2 * S2.y * S2.x)

(4): A * P.x^2 + B * P.x + C = 0
}

By solving (4) ==> you can find P.x,

From P.x and (1) ==> you can find P.y

From P.x and P.y and (3) ==> you can find b.

• You could also double check the arithmetics to be sure. It is a LOT of operations. I can't be sure I did this correctly. – dimitris93 Mar 26 '15 at 17:50
• I'm not able to get a result for this. When I solve for P.x in (4) I get P.x = ( sqrt(B*B - 4*A*C) - B ) / ( 2*A ) and B gives me a negative number so the equation doesn't give me a real number. – Sarke Mar 26 '15 at 23:31
• @sarke i like your solution better. But in my attempt, you are supposed to find 2 P.x solutions and reject one of them. how did you get the solution above ? if it actually is wrong, is because i did the calculations wrong for A or B or C. but the idea should definitely work. Even if i did wrong calculations, you would still get a trionimal either way ( Ax^2 + Bx + C), when you combine (1) and (2). – dimitris93 Mar 27 '15 at 3:55
• @Sarke looking at your answer: "we get two results, depending on if we're forward in time or backwards. We'd have to check that the B we get is in the right direction." you also got 2 results, for the same reasons. Following my solution, you would pick the P.x that is greater than S2.x if you were moving towards the right, or the one less than S2.x if you were moving left – dimitris93 Mar 27 '15 at 3:58

This is how I would pose the problem:

Then continue to substitute terms into each other until I find what I need.

Considering your question I think what you need is d2. I guess the things you know are:

• P01, d1, v1
• P02, v2

So try to make an equation that exhibits d2 = <only stuff you know>, from the set of equations in the drawing. that has to be possible.

• hmmm also I guess I realize all of a sudden, lol, but lapse1 == lapse2. yeah that must be. – v.oddou Mar 25 '15 at 8:01
• Not sure how this helps, it rewording the question. What do you mean by "<only stuff you know>"? Both D1 and D2 are unknowns. – Sarke Mar 26 '15 at 7:35
• you know d1, d1 is a unit vector in the 80 degrees direction. if you don't know d1, there is no solution, the system would lack one constraint. – v.oddou Mar 26 '15 at 8:52
• Sorry, I misread d1 with t1, as Distance 1. So how do I solve this? And what do you mean by d2 = <only stuff you know>` – Sarke Mar 26 '15 at 11:20
• Also, I know for a fact there is enough information to solve this. I just don't know how. – Sarke Mar 26 '15 at 11:34