0

I don't even know if it's mathematically feasible, but let's say I have a tower at (Tx, Ty) shooting at a monster located at (Mx(t),My(t)).

The thing is, the path followed by the monster is jagged and swirly, meaning that predictive aiming from a distance based on velocity/direction at -that exact time- would be useless. The monster would've changed directions two times over by the time the bullet reached its target.

To solve this, I have a function to fast forward the monster for (t) frames and get its position as (Mx(t), My(t)) assuming its velocity remains constant. I can get the monster's position at t = 0 (current position) or t = 99999, or anything in between. Think of it as a lookup table.

The hard part is having the turret predictably aim at a position derived from that function.

I have to know t beforehand to know what to put into (Mx(t), My(t)). I have to know (Mx(t), My(t)) to know the distance and calculate the t from that. Is this even possible? Are there alternatives?

Any kind of pseudocode is welcome.

  • I think your title is a bit confusing as it doesn't have anything to do with prediction. You said you fast-forwarded to find the future position, so you have solved the prediction problem. It is a "how do I calculate the angle" problem. – rghome Jan 09 '16 at 16:51
  • Can you give some more details regarding the monsters' path? I doubt they can be written in single polynomials, but if they can at least be written as piecewise functions we can narrow the task down to finding which "piece" or segment of path to fire at, and then the calculation after that would be much simpler. – busukxuan Jan 18 '16 at 09:55

2 Answers2

0

It is, if I understand your problem correctly, basically the transformation for euclidian to polar coordinate system If you have the relative position x=Mx(t)-Tx and y=My(t)-Ty which is

 distance= sqrt(x^2+y^2)
 phi = arctan(y/x)

this assumes infinite speed of the shoot. However you can first calculate the approximate time for the projectile using the current position. Calculate the flying time based on the estimated distance and iterate this process.

If convergence speed is to slow (not much difference in projectile/monster speed) You can also use the idea of Divide and conquer. You calculate the maximum distance the monster runs if it maximises distance. Calculate the distance for now, the max distance and the middle. Then you can decide in which half the monster is and calculate again distance in the middle.

I think everything else would require more information on how you plan to calculate Mx(t) and My(t)

CAFEBABE
  • 3,983
  • 1
  • 19
  • 38
  • So I just iterate until the distance between the bullet position at (t) and the monster position at (t) is 0? I iterate from angle 0 and go to angle 359, picking out the closest option? Seems extremely slow. – user5766981 Jan 09 '16 at 15:29
  • Convergence speed depends on the relative speed difference. However, you can also just transform all distances for all time points. I'll add that to the answer. – CAFEBABE Jan 09 '16 at 15:34
  • y/x could give divide-by-zero if x is zero. – rghome Jan 09 '16 at 16:45
0

You want the atan2 function.

If the tower is at tx,ty and the monster's predicted position is mx, my then the angle of the tower's gun has to be:

angle = atan2(my - ty, mx - tx)

This should be available in most languages.

Distance is, of course:

square-root((my - ty)^2, (mx - tx)^2)

where ^2 is "squared".

rghome
  • 8,529
  • 8
  • 43
  • 62