Jump to content
  • Advertisement
Sign in to follow this  

Calculating trajectories

This topic is 4526 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So, I have a tank that fires shells at a certain velocity. I have a target a certain distance from the tank. Is there a friendly way to determine the angle at which to fire the shell in order for it to hit the target. I tried to solve this seemingly simple problem in an earlier game, and now I'm faced with it again. Anybody have any ideas? ps. The tank and target are stationary, no wind/funky stuff.

Share this post

Link to post
Share on other sites
Here is a site that I found, that has all sorts of stuff related to your topic. I think you'll be most interested in the section near the bottom, about ballistic trajectories. The site is: http://hyperphysics.phy-astr.gsu.edu/hbase/traj.html

Share this post

Link to post
Share on other sites
this has troubled me for ages, i understood and implemented the answer only recently.

x,y is the target
j,k is the beginning point
g is gravity
v is velocity
θ is the angle of elevation

b = (x - j)
a = g(b^2) / 2(v^2)
c = k - y + a
d = b^2 - 4ac

if (d < 0) then target is out of range (gravity is too strong, or not enough velocity)

θ_1 = artan((-b + sqr(d)) / 2a)
θ_2 = artan((-b - sqr(d)) / 2a)

the range of artan is restricted to [-pi/2, pi/2], so if the target is 'behind us,' (negative distance) the angle needs to be rotated pi radians (180 degrees)

if( b < 0 ) then
θ_1 = θ_1 + pi
θ_2 = θ_2 + pi
end if

there are two answers, one is a lob shot, and the other is a direct and faster shot. (you can find the time it takes to hit the target with each angle by using the equation below)

the functions that model the horizontal and vertical movement over time are respectively:

(1) x = vt cos θ + j
(2) y = ½ gt^2 + vt sin θ + k

to find θ:

re-arrangle (1) for t:
t = (x - j) / v cos θ

substitute into (2)
y = g(x - j)^2 / 2v^2 cos^2 θ + v sin θ(x - j) / v cos θ + k

sin θ / cos θ = tan θ
1 / cos^2 θ = tan^2 θ + 1

simplify and re-arrange:
0 = [g(x - j)^2(tan^2 θ + 1) / 2v^2] + tan θ(x - j) + k - j

expand a little to get the form 0 = ax^2 + bx + c:
0 = [g(x - j)^2 tan^2 θ / 2v^2] + tan θ(x - j) + k - j + [g(x - j)^2 / 2v^2]

use the quadratic formula to find tan θ


how does that look?

[Edited by - phillipj on June 1, 2006 3:02:01 AM]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!