Jump to content
  • Advertisement
Sign in to follow this  

Projectile motion

This topic is 4374 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

I'm attempting to find the angle to fire a projectile at such that it lands at a specific point. I have only done (in fact, I havn't even completed) high school level physics and calculus, so please bear with me. Here's what I've done so far: T is the angle fired at (for lack of a theta) Vx is initial horizontal velocity Vy is initial vertical velocity s is the initial speed of the projectile t is time d is horizontal distance to target h is difference in height of target
Vx = cos(T).s
Vy = sin(T).s
d = Vx.t
t = d/Vx
t = d/cos(T)/s
h = Vy.t + 1/2.a.t^2
h = sin(T).d/cos(T) + 1/2.a.(d/cos(T)/s)^2
h = sin(T).d/cos(T) + 1/2.a.d^2/cos(T)^2/s^2
h = tan(T).d + 1/2.a.d^2/cos(T)^2/s^2
So if I could solve that last equation and find T, I'd be pretty much done. But I'm not sure how to do that, and for all I know I'm barking up entirely the wrong tree anyway. Also, if it's not possible to hit the target point, it would be nice to find the angle that comes closest. But it's not essential, I can just set it to 45 degrees and call that good enough. Thanks.

Share this post

Link to post
Share on other sites
Express the involved polynomials in cos(T) and sin(T) in terms of cos(2T) and sin(2T); you'll get a linear equation in those, which can be easily solved e.g. by converting it further using a "helper" angle.

As to "the angle that comes closest", what exactly is your measure of closeness? The answer depends on this a lot, but in any case, you can calculate the locus of reachable point pretty easily and proceed from there.

Share this post

Link to post
Share on other sites
well, you could either sole the trig equation - for that you'd first have to decide on an initial speed (s). or to simplify you could decide on an initial horizontal velocity (Vx) and then use it to get the time (d = Vx*t) and use t to find Vy (h = Vy*t - 0.5g*t^2). with Vy and Vx you can get V and the angle. but if you need a certain V and you only want to get the angle you have to solve the trig equation.

Share this post

Link to post
Share on other sites
as Darkstrike wrote, you need to transform the sin & cos terms using
sin(2x)=2*sin(x)*cos(x) and

When I calculated it, I used a different direction of the acceleration, but it's just to insert (-a) instead of a.

s^2*h*cos(T)^2 = s^2*sin(T)*cos(T)*d - a*d^2/2

(1+cos(2T))*h/2 = sin(2T)*d/2 - a*d^2/(2*s^2)

(a*d^2+h*s^2)/s^2 + h*cos(2T) = d*sin(2T)

Set (a*d^2+h*s^2)/s^2 = C.

h*cos(2T) + C = d*sin(2T)

cos(2T)*h/d + C/d = sin(2T)

For the rest I made a very nice Paintbrush picture ^^

description of your image

T = (asin(C/sqrt(d^2+h^2)) + atan(h/d))/2

I could have forgot some special case for asin or atan, but the basics should be correct. Probably a couple of typos too... =)

Share this post

Link to post
Share on other sites
Sadly, what you described neglects air resistance/wind -- which are rather important, if you want realistic projectile motion.

There is an alternative way to solve such problems. It isn't always best.

Basically, you search for the right firing solution, instead of calculating it.

Start out with sampling the "space" of possible firing options either randomly, or with some best guesses.

Now use those samples to figure out a better set of samples. Repeat until you are close enough.

If you have a simple enough problem, you can often get solutions that 'braket' your target -- one shot is to the left, the other to the right. In that case, a simple binary or dictionary search often rapidly produces an accurate solution.


In your particular example, let's first solve for t in terms of h and Vy.

a/2 t^2 + Vy t - h = 0

t = -Vy +/- sqrt(Vy^2 + 2 a h)

There are two solutions to the above. So for a given Vy, we have t0 and t1.

Next, see how far away horizontally the shot is at t0 and t1:

d = Vx * t
d0 = t0 Vx
d1 = t1 Vx

That is pretty easy!

Now, we start out by testing a bunch of different random firing solutions. We stop after testing a large number, or after we have 'bracketed' the target (got one to land on the far side, and one to land on the near side, of the target).

Once we have 'bracketed' the target, we simply engage in a binary search through the firing space between the two options.

If we haven't bracketed the target, it is possible the target cannot be hit. We take the closest shot to the target in t0, search the solutions near it, and give up if we stop getting closer. We then do the same with t1 solutions.

All of the above does is solve the problem in a way that doesn't rely on the equations described being 'easy to solve' -- if you added tweaks to the equations (wind resistance), the above solution would be more robust (it wouldn't be guaranteed to work!) compared to the analytical solution.

Share this post

Link to post
Share on other sites
Thanks for your help guys! Especially Necator. It works perfectly.

Original post by NotAYakk
Sadly, what you described neglects air resistance/wind -- which are rather important, if you want realistic projectile motion.

I'm not worried about air resistance right now, but that technique does look useful.

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!