2
$\begingroup$

First of all, I asked this question on Stackoverflow, but I realize this is a better place to ask the question. So i moved it here.

I've looked for quite some time now to find a nice math solution for my cannon firing a projectile at a moving target, taking into account the gravity. I've found a solution for determining the angle at which the cannon should be fired, based on the cannon's position, the target's position and the start velocity. The formula is described here: http://en.wikipedia.org/wiki/Trajectory_of_a_projectile#Angle_.CE.B8_required_to_hit_coordinate_.28x.2Cy.29.

Unfortunately I can't post images yet here, so you'll have to do with the link.

This works perfectly. However, my target is moving, so if I shoot at the target and the projectile takes a few seconds to get to its destination, the target is long gone. The target's x position can be determined from the time. Lets say that: x = 1000 - (10 * t) where t is the time in seconds. The y can be described as: y = t.

The problem is, that t depends on the angle at which the cannon is fired.

Therefor my question is: How can I modify the formula as described in the wiki, so that it takes the moving target into account?

I want to fire it now and the target is in range given the speed. The cannon is at {0, 0} and isn't moving. The start speed is 100 m/s. The target is at {1000, 0} and is moving with 10 m/s towards the cannon (v = -10 m/s).

What angle should I use to hit the moving target, when I want to fire at t=0 (immediately)?

If I shoot without taking the target's speed into account, I would aim at {1000, 0} and the angle could be calculated using the mentioned formula. But it will miserably miss the target because its moving.

I could aim at i.e. {500, 0}, calculate what time it takes for the projectile to arrive at those coords (lets say 5 seconds) and wait until the target is 5 seconds away from {500, 0}, being {550, 0}. But this means that I have to wait 450m or 45 seconds before I can fire my cannon. And I don't want to wait, because the target is killing me in the mean time.

I really hope this gives you enough info to go with. I'd prefer some a neat math solution, but anything that would get me really close to firing "right away" and "right on target" is also much appreciated.

I would also be happy if you can tell me if what I want can't be captured in a formula, then I can figure out an algorithm to find it as fast as possible.

Thank you in advance for your braintime!

  • 0
    A closed form solution for $\theta$ in this case could be hard. Instead, you could simply plug in the values that you know into those equations and try to eliminate $\theta$ using $\sin^2 \theta + \cos^2 \theta = 1$ to find $t$ and then find $\theta$ using $t$.2011-04-20

1 Answers 1

1

Note: I'm using the same convention used in the wikipedia article that you mention.

Suppose the target is moving with a speed $u$, in a direction that makes an angle $\phi$ with the positive direction of the x-axis, then you can write down the following equations of motion:

$x = vt\cos\theta + ut\cos\phi$ $y = vt\sin\theta - \frac12gt^2 - ut\sin\phi$

If you eliminate $t$ from those two equations you will get an equation in a very nice form:

$gx^2+2(v\cos\theta+u\cos\phi)(vy\cos\theta+ uy\cos\phi-vx\sin\theta+ux\sin\phi)=0$

in which you are required to substitute the values of $g,x,y,v,u,\phi$ and solve the resulting equation for $\theta$. You can use a numerical method which can handle implicit equations like $f(\theta)=0$ as we have here. For instance, you could try Newton's Method.

  • 0
    Glad to help! :)2011-04-21