Take the 2-minute tour ×
Physics Stack Exchange is a question and answer site for active researchers, academics and students of physics. It's 100% free, no registration required.

Overview: I'm working on programming a simulation that requires 'shooting' projectile-type objects at other moving objects. How can I calculate the angle at which to shoot the object to hit?

Details:

Imagine you're holding some gun-type object that shoots projectiles at velocity $v_d$ in straight lines. You are moving with velocity $v_p$ at angle $\theta_p$, in a straight line. Another object, which we will name the target, is located a distance $R$ away from you at an angle $\theta_{tp}$, and is moving in a straight ine with velocity $v_t$ at angle $\theta_t$.

I need to calculate the angle $\theta_a$ I should aim the gun in order to hit the target. Note that there is no decision on the TIME to fire. I have to fire immediately, at $t=0$, so this adds a helpful constraint but also makes it so that some targets are not able to be hit based on the numerical values of the parameters mentioned, which is fine. The target is moving so you need to lead it with your shot to compensate. Also, the projectile will inherit your velocity on top of its own since you are also moving, which will further modify the angle you need to shoot at.

It seems to me that the strategy should be that I find a point in space which will be occupied by both the projectile and the target. I know all the points in space that the target will take along its linear path, and the times at which it will take all points. However, because the total velocity of the projectile varies based on the angle you shoot it (e.g. the magnitude of the velocity vector will be different depending on what Theta_a you decide to shoot at), I'm not sure how to figure out where to shoot it.

Any help would be appreciated!

In addition, if it is easy, a solution to a target moving in a circular path with radius $R$ (same $R$ as the distance between you and it, so it's making a perfect circle around you) would be appreciated!

share|improve this question

migrated from stackoverflow.com Dec 18 '11 at 14:20

This question came from our site for professional and enthusiast programmers.

1  
How does the magnitude of the velocity vary with the angle? –  David Z Dec 18 '11 at 11:14
1  
Possible duplicate of stackoverflow.com/questions/4107403/… –  Gareth Rees Dec 18 '11 at 11:39
    
It varies because if you add two vectors of length A and length B, depending on the angle between them, the resultant vector has a different magnitude. Since when I change the angle I'm aiming at this changes the angle between the gun velocity vector and the projectile velocity vector, the resultant total magnitude of velocity of the projectile, which is the sum of both vectors, should change. Either that or I'm dense :P I'll check out the duplicate, seems relevant. –  user6737 Dec 18 '11 at 20:27
    
The link was indeed relevant, and I was able to solve the problem. There, by using vector notation it becomes clear on how to deal with the 'varying velocity' issue, which is just incorporated into the components of the overall equation for t_intersect instead of using it as a scalar magnitude like I was doing when solving the problem for a stationary guy and then trying to use that approach for the moving gun incorrectly. What's teh appropriate procedure for accepting an answer if it is found in a different post? –  user6737 Dec 18 '11 at 22:11
1  
Hi CHP and welcome to Physics Stack Exchange! You can post an answer here explaining how you were able to solve the problem and linking to the other answer that allowed you to do so. Make sure you don't just post a link, though; people should be able to understand your answer without following any links. –  David Z Dec 19 '11 at 0:46

2 Answers 2

First of all, you should do the math in terms of vectors, i.e. position $\vec{x} = (x, y)$, velocity $\vec{v} = (v_x, v_y)$. Then for every object the position over time can be expressed as $\vec{x}(t) = \vec{x}_0 + t\vec{v}$, where $\vec{x}_0$ denotes the position at the time $t=0$, $\vec{v}$ the constant velocity, and $\vec{x}(t)$ the position at a given time $t$.

To simplify the picture, you can do the calculations in the co-moving reference frame of the gun. The pointing angle of the gun will not change by this (as long as you aren't considering relativistic velocities ;) ). The ejection velocity is defined in the gun's frame of reference anyway.

To do this, let's write the target time dependent position in the universe frame as:

$$\vec{x}_t(t) = \vec{x}_{t0} + t \vec{v}_t$$

and the gun's position as

$$\vec{x}_g(t) = \vec{x}_{g0} + t \vec{v}_g$$

To transform to the gun's reference frame, just subtract the gun's position equation; the ' denotes the new reference frame. Then we get:

$$\vec{x}_t'(t) = \vec{x}_{t0} - \vec{x}_{g0} + t (\vec{v}_t - \vec{v}_g),\quad \vec{x}_g'(t) = 0$$

Or, with $\vec{x}_{t0}' = \vec{x}_{t0} - \vec{x}_{g0}$, and $\vec{v}_t' = \vec{v}_t - \vec{v}_g$ you get

$$\vec{x}_t'(t) = \vec{x}_{t0}' + t \vec{v}_t'$$

Into this equation you can simply insert the time when you want to hit the target, and you get the position of the hit. As the gun is located at $0$, this also gives the direction to which you have to point your gun. The required velocity then is defined by the distance to the hitting point divided by the hitting time.

The angle can simply calculated with the atan2 (a 360-degree complete version of the arctangent function available in most programming languages) of the required velocity vector for the projectile $\vec{v}_p = (v_{px}, v_{py})$ by theta = atan2(vpy, vpx). This of course depends on your definition of theta, so you might need to adjust it.

If you shoot with a predefined velocity, you can write an equation with the hitting time as a variable and solve for that.

This should show the way to get your result, you might have to work out some more details, or ask again for something specific (e.g. vector arithmetics) if this isn't sufficient.

share|improve this answer
    
Hi Peter, and welcome to Physics Stack Exchange! Nice answer :-) We have MathJax available on this site for writing mathematical notation, so I went through and edited your math to make it look better. –  David Z Dec 19 '11 at 0:57
    
great, thanks David! It has been moved from stackoverflow.com, so I just incidentally started posting on Physics Stack Exchange. As I'm professionally related to physics, it isn't a bad thing though ;) –  Piotr99 Dec 21 '11 at 9:24

I found the solution posted by Gareth Rees to work, which was posted at this link:

http://stackoverflow.com/questions/4107403/ai-algorithm-to-shoot-at-a-target-in-a-2d-game

Notation: I write vectors in capital letters, scalars in lower case, and ∠V for the angle that the vector V makes with the x-axis. (Which you can compute with the function atan2 in many languages.)

The simplest case is a stationary shooter which can rotate instantly.

Let the target be at the position A and moving with velocity VA, and the shooter be stationary at the position B and can fire bullets with speed s. Let the shooter fire at time 0. The bullet hits at time t such that |A − B + t VA| = t s. This is a straightforward quadratic equation in t, which you should be easily able to solve (or determine that there is no solution). Having determined t, you can now work out the firing angle, which is just ∠(A − B + t VA).

Now suppose that the shooter is not stationary but has constant velocity VB. (I'm supposing Newtonian relativity here, i.e. the bullet velocity is added to the shooter's velocity.)

It's still a straightforward quadratic equation to work out the time to hit: |A − B + t(VA − VB)| = t s. In this case the firing angle is ∠(A − B + t (VA − VB)).

What if the shooter waits until time u before firing? Then the bullet hits the target when |A − B + t(VA − VB)| = (t − u) s. The firing angle is still ∠(A − B + t(VA − VB)).

Now for your problem. Suppose that the shooter can complete a half rotation in time r. Then it can certainly fire at time r. (Basically: work out the necessary firing angle, if any, for a shot at time r, as described above, rotate to that angle, stop, wait until time r, then fire.)

But you probably want to know the earliest time at which the shooter can fire. Here's where you probably want to use successive approximation to find it. (Sketch of algorithm: Can you fire at time 0? No. Can you fire at time r? Yes. Can you fire at time ½r? No. etc.)

share|improve this answer

Your Answer

 
discard

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