Jump to content
  • Advertisement
Sign in to follow this  

Angle of Launch if Source and Destination are not level

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

Hi,

I am trying to calculate the required angle of launch given that a shooter at say 0,0 is trying to shoot a target at level 200,-200.

I have been using the calculation for the angle required to hit a target shown in wikipedia but I am thinking that this requires the source and target locations to be on the same level? Is this correct?

http://upload.wikimedia.org/math/c/1/4/c14d095c7d480ca398e25c01a53ed7be.png


The physics I am trying to reprouce are the same as those shown in the game peggle where a shooter will shoot the ball towards the mouse position and the ball will pass through the position indicated by the mouse.

I hope that makes sense and someone can point me in teh right direction!!! :)

Many thanks
James

Share this post


Link to post
Share on other sites
Advertisement
Your problem is underdetermined. You need to specify some additional information, like time of flight.

They don't have to be at the same height. The general problem you are solving is

1/2 (0,g) t^2 + (v_x, v_y) t = (x, y),

where g is gravity (I'm assuming it points down), (v_x, v_y) is the initial velocity of the projectile, and (x,y) is the desired target. If t is given, then you can solve for (v_x, v_y), at which point it is trivial to get the angle (sin^-1 (v_y / (v_x^2 + v_y^2))). Otherwise, you have 2 equations and 3 unknowns.

If you specify something like the magnitude of the velocity (v_x^2 + v_y^2), that should do it too, although I think you might get multiple solutions at that point, but it should be obvious which one you want.

Share this post


Link to post
Share on other sites
Ah thank you for that, I could probably estimate the time value. I am using bullet physics with my projectile with mass 1kg and giving an impulse force of 600 (which I am assuming would give a velocity of 600) so I could estimate the time by finding the distance between the points and applying a constant?

It seems the time of flight would be easier as it would save having two values for the answer :)

Share this post


Link to post
Share on other sites
If you know the impulse is 600, then that gives you the magnitude of the initial velocity, which I'll call m:

v_x^2 + v_y^2 = m^2 = 600^2

This gives you 3 equations for 3 unknowns (v_x, v_y, t) which you can solve. Here are the equations explicitly:

v_x t = x
1/2 g t^2 + v_y t = y
v_x^2 + v_y^2 = m^2,

Since they're non-linear they are a little messy, but you only have to do the algebra once and you'll have closed-form expressions. You could also stick them into Mathematica and solve. Keep in mind that you will get multiple solutions because v_x = +/- sqrt(m^2 - v_y^2), but it should be obvious whether to use the positive v_x or the negative, depending on where your target is.

BTW, I could be overlooking some simpler way of solving this by using the angle directly instead of solving for (v_x, v_y) then computing the angle.

Specifying the time of flight t would mean you don't need the extra eqn. to solve for (v_x, v_y), but it would also mean that the time of flight would be the same for all projectiles, no matter how far away they are from their target. This could look awkward.

Share this post


Link to post
Share on other sites
Another caveat: Setting a fixed t to target will mean that your projectile speed is dependent on distance (if an object is farther away, you'll shoot a faster bullet). Setting a fixed projectile speed, however, will result in certain positions being outside your range. You should take these into account to avoid undefined behaviors in your program. For example, the height that a projectile with a fixed speed will reach is determined by

1/2 g t^2 + v_y t = y

derive for max value:

g*t + v_y = 0
g*t = - v_y
t = -v_y/g

Replace t

1/2 g * (-v_y/g)^2 + v_y (-v_y/g) = y_max
- 1/2 * v_y^2 / g - v_y^2/g = y_max
- 3/2 * v_y^2 / g = y_max

If your target y is higher than y_max, you'll never reach it.

Share this post


Link to post
Share on other sites
Those are probably fine, but notice that they are identical, modulo the sign.

A simple test of the target location compared to the launch location should tell you which sign you need. For example, if target_x > start_x, then a negative x velocity will obviously not get you there. Ditto for the y velocity.

So just code up these equations and do some quick tests to determine the sign. And I'm pretty sure a negative sign doesn't make sense, and probably correlates to the issue Kian was talking about.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!