How to find firing angle given initial and destination positions?

Started by
4 comments, last by Portable591 22 years, 9 months ago
Ok, I''m not sure if this is possible, but is there a way to find a firing angle for a gun if you are given the initial velocity from the gun, initial position of the gun, and an exact distance to a target? Assume the gun and target are on a level plane. I know there could be multiple "correct" angles, but I want the one with the smallest "time to target". Thanks.
Advertisement
If you only know the position of the gun and its distance form the target, but you do not know the position of the target then you do not have enough information to solve this problem. This would mean you only know that the target is somewhere on circle, around the gun. You can’t hit a target if you do not know where it is.

If you do know the position of the target and the both the target and gun are stationary, then the solution would be:

Gun is at: (Gx, Gy) and Target is at: (Tx, Ty) then:

Angle from the horizontal axis = arctan ((Ty-Gy)/(Tx-Gx))

Note: If you use the Win32 function: atan() to calculate the arctan, remember the results are in radians not degrees. If you want to derive this equation just look at the properties of trigonometric functions.

If either the target or the gun is moving, this solution will not work. Let me know if you want the solution for moving objects.

Assuming a level (horizontal?) plane:

Known:

Velocity (vx is x velocity, vy is y velocity)
Distance to target (dx is x distance, dy is y distance)

Unknown:

Angle

Basic formulas:

vx = vel * cos(angle)
vy = vel * sin(angle)
time = d/v = dx/vx = dy/vy

angle = acos(vx / vel)

vx = dx/time;

angle = acos( (dx/time) / vel);

Problem with this, is we must pick a time to solve for angle.

Oh well, hopefully this is a good starting point.



This is easy if you add both the angle and time to your calculations. E.g.

time = t
angle = a
speed = v
gravity = g

Horizontal motion: x = vt cos (a)
Vertical motion: y = vt sin (a) - 0.5 * g * t * t

It hits the ground when y = 0, so

y = vt sin (a) - 0.5 * g * t * t = 0

Solving this quadratic gives this happens when
t = 0
and
t = 2v sin (a) / g

The first value is when it is fired, so it hits when

t = 2v sin (a) / g

putting this into the x equation gives

x = 2v*v sin (a) cos (a) / g

or

x = v*v sin (2a) / g

so

sin (2a) = (g * x) / (v * v)

if (g * x) / (v * v) < 1 this has two solutions, one less than 45 degrees and one greater than 45 degrees. The one with smallest time t is the smallest angle, i.e. the one less than 45 degrees,which then is:

a = 0.5 * arcsin ((g * x) / (v * v))
John BlackburneProgrammer, The Pitbull Syndicate
Good job! I was busy considering x- and y-axis velocities, which left me with an inconclusive answer (I was lacking just one term), so I didn''t post it.

**Hurriedly scribbles down solution.
Thank you so much. I was thinking along the same lines as Oluseyi. But this works very well.

This topic is closed to new replies.

Advertisement