Trajectory of a projectile meets a moving object (2D)

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.

-
If the target is moving with speeds $u_x$ and $u_y$ along the x and y axes respectively, then you can modify the equations of motion to take that into account as follows: $x=vt\cos(\theta)+u_xt$ and $y=vt\sin(\theta)-\frac12gt^2 - u_yt$. Observe how this change doesn't affect the equations much since $u_xt$ and $u_yt$ factor in with the existing $vt$, so that the rest of the derivaton should follow routinely as before. –  quantumelixir Apr 18 '11 at 17:38
Dear QuantumElixer, asking you this feels a bit like not doing my homework, but I didn't manage to solve the equation with the "+ ux/uy t" part. To be quite honest, I just copied the first formula into code straight from the wiki. Could you help me out a bit? Thank you in advance! –  sdk Apr 19 '11 at 17:42
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$. –  quantumelixir Apr 20 '11 at 5:01

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.

-
Thx alot, this works like a charm and is also something I understand completely! Thx alot for your insight! –  sdk Apr 20 '11 at 19:15
Glad to help! :) –  quantumelixir Apr 21 '11 at 12:53