1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ how do you know for sure that they intersect ? \$\endgroup\$ – dimitris93 Mar 25 '15 at 0:35
  • \$\begingroup\$ @Shiro, because I decide the angle of the cannon. \$\endgroup\$ – Sarke Mar 25 '15 at 0:36
  • \$\begingroup\$ 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 \$\endgroup\$ – dimitris93 Mar 25 '15 at 0:38
  • \$\begingroup\$ @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. \$\endgroup\$ – Sarke Mar 25 '15 at 0:41
  • \$\begingroup\$ 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 \$\endgroup\$ – dimitris93 Mar 25 '15 at 0:45
2
\$\begingroup\$

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

trig

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:

$$ \begin{align} C &= \sqrt{(b.x - a.x)^2 + (b.y - a.y)^2} &&\approx 10.05 \\ ∠C &= atan2(b.y - a.y, b.x - a.x) &&\approx 84.29° \end{align} $$ therefore:

\$∠a = ∠C - ∠B ≈ 94.29°\$

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

\$A^2 = B^2 + C^2 - 2*B*C*cos(a)\$

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

\$(B*r)^2 = B^2 + C^2 - 2*B*C*cos(a)\$

Solve for \$B\$ and we get:

\$B = ± \frac{ \sqrt{ C^2 * (cos(a)^2 + r^2 -1 ) } - cos(a)*C }{r^2 - 1} \approx 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:

$$ \begin{align} c.x &= a.x + B * cos(∠B) &&\approx 4.14 \\ c.y &= a.y + B * sin(∠B) &&\approx 1.62 \end{align} $$

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) \approx -83.72°\$

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – dimitris93 Mar 26 '15 at 17:50
  • \$\begingroup\$ 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. \$\endgroup\$ – Sarke Mar 26 '15 at 23:31
  • 1
    \$\begingroup\$ @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). \$\endgroup\$ – dimitris93 Mar 27 '15 at 3:55
  • \$\begingroup\$ @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 \$\endgroup\$ – dimitris93 Mar 27 '15 at 3:58
0
\$\begingroup\$

This is how I would pose the problem:

enter image description here

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.

\$\endgroup\$
  • \$\begingroup\$ hmmm also I guess I realize all of a sudden, lol, but lapse1 == lapse2. yeah that must be. \$\endgroup\$ – v.oddou Mar 25 '15 at 8:01
  • \$\begingroup\$ Not sure how this helps, it rewording the question. What do you mean by "<only stuff you know>"? Both D1 and D2 are unknowns. \$\endgroup\$ – Sarke Mar 26 '15 at 7:35
  • \$\begingroup\$ 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. \$\endgroup\$ – v.oddou Mar 26 '15 at 8:52
  • \$\begingroup\$ 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> \$\endgroup\$ – Sarke Mar 26 '15 at 11:20
  • \$\begingroup\$ Also, I know for a fact there is enough information to solve this. I just don't know how. \$\endgroup\$ – Sarke Mar 26 '15 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.