# Targeting with a ballistic gun I am trying to determine the gun elevation angle of a gun that fires ballistic projectiles.

For a target at a certain distance D, I will have to compute an angle that increases with D.

This relation ship is non-linear, by the way, because the projectile loses velocity as it travels, for starters, due to drag.

In the past, I have done this with a lookup table.... for a whole bunch of test firings, see where the projectile hits the ground.

But this only works if there is no delta in the elevation between gun and target.

A target that is on a hill top, will need adjustment for extra range. Whereas a target in a valley below, would mean a lower gun elevation.

This means that the table-lookup will break down, and a 2D table seems like a kludge.

What would be an effective way to compute this targeting? I know things also depend on nozzle-velocity and air-drag, but these will not vary (but drag will be non-zero.) Neither will I be modeling the wind.

This is for AI assisted targeting. NPC or player targets a point in the world. The algorithm will find the corresponding angle to hit the target.

Currently, I am using Bullet Physics, with a discretely stepped world.

• If your physics engine is reality, the projectile motion equations would work. In practice for a physics engine, they might be off by a bit if the engine integrates imprecisely. What engine are you using? – Anko Dec 3 '18 at 21:26
• If you're not modelling drag due to air resistance, then we have several existing answers you should be able to use already. If you do need to model air drag, then the strategy gets more complicated, and will depend on what drag model you're using. Can you fill in this detail? – DMGregory Dec 3 '18 at 23:48
• Also, for what it's worth, it looks like Oskar Stålberg uses a 2D lookup table for ballistic arcs in Bad North, so it's not by any means a forbidden approach. ;) – DMGregory Dec 4 '18 at 4:29

Is this meant for A.I., testing, or educational purposes? This may dictate if approximated methods are an option.

The Wikipedia page on projectile motion is really descriptive on the behavior of 2D ballistics. It includes a section for Angle required to hit coordinate (x,y) and another for when considering air resistance.

Is the speed at which the gun shoots constant? If not, applying a binary search on v while solving for theta might be viable.

• This answer would be better if it included the formulas from Wikipedia and showed how to apply them to this problem. As a link, it's possible that an editor later changes / removes that information from the Wikipedia page, leaving this answer less useful than it aims to be. – DMGregory Dec 22 '18 at 23:48

Those are good references suggested by @Anko and @Rodrigo Borges I am a Mechanical Engineer, so for fun thought I would answer this question directly and save you sometime:

Given:
v_0: initial velocity of projectile
v_f: final velocity of projectile
g: gravity
a_d: acceleration due to drag (you state constant in your questions, simplifying the physics)
t: time
dt: differential of time
D: distance traveled
x_0: initial position in x
y_0: initial position in y
x_f: final position in x
y_f: final position in y

conservation of momentum:
m*v_f=m*v_0-g*m*t-m*a_d*t
divide by m:
v_f=v_0-g*t-a_d*t
Integrate to get position from velocity:
D=∫(v_0-g*t-a_d*t) dt
D=v_0*t-1/2*g*t^2-1/2*a_d*t^2+const.
distance, D, component wise use basic trigonometry:
y_f=v_0*t*sinθ - 1/2*g*t^2 - 1/2*a_d*t^2*sinθ + y_0
x_f=v_0*t*cosθ - 0 - 1/2*a_d*t^2*cosθ + x_0


Since you know the firing position (x_0,y_0) and the target position (x_f,y_f) You have two unknowns (t & θ) and two equations, so you can solve for both.

• Thanks. But v_f is unkown though? And I meant to say aerodynamics dont vary. Drag itself should probably scale w square of vel magnitude? – Bram Dec 4 '18 at 14:41
• Once you solved for t then substitute and solve for v_f in the equation before integration. Yes, drag is proportional to square of vel. But if you model that in full, all of this gets a lot more complicated. For games, your assumption of constant drag is appropriate and just scale it proportional to the size of objects (larger object receive more drag force). At high velocities, a proportional to square velocity model introduces terminal velocity, the fast speed you can go in air. At low velocity, you won't see a difference on short paths with a constant drag and it will look real. – Chris Dec 16 '18 at 18:29

Per @DMGregory who posted a link to Oskar Stålberg's tweet.

By far the easiest solution is a 2D lookup table. Personally, I fill the table by sampling of test firings. I perform test firings by stepping through the range in steps of 0.02 radians, which is 1.146 degrees. For my purposes, this has been accurate enough.

It will be able to handle complex stuff like air drag that scales with the square of the velocity.

The only downside: if you tweak your simulation, you need to re compute the tables. 