Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

How do I aim a constant speed projectile to hit a target if there is a constant acceleration vector acting on it? (For example, the wind and gravity from Worms.)

share|improve this question
add comment

1 Answer 1

up vote 7 down vote accepted

Let x be the position of the target relative to us, and let v be our (the projectile's) velocity relative to the target. The speed ||v|| of the projectile and the acceleration vector a are constant. We set up the usual equation of motion:

Final equation of derivation: 0 = x.x - (x.a + ||v||^2)*t^2 + a.a * t^4 / 4

This is now simply a biquadratic equation, which we can solve for t^2 with the usual quadratic formula, and take the square root again to get t:

t=sqrt((x.a+||v||^2 +- sqrt((x.a + ||v||^2)^2 - (a.a)(x.x))/((a.a)/2))

The lesser and greater positive real roots are the minimum (shallowest) and maximum (steepest) flight times of the projectile, respectively. Both of these will exist if there is any solution. We can then just plug them back into v=x/t-1/2 * a * t to recover the actual velocity vector. We're normally looking for the minimum flight time solution, but if e.g. there's a hill in the way, the maximum time solution might be able to shoot over it.

share|improve this answer
1  
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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