You are not logged in.
Pages: 1
Hi! I'm a flash game developer, check out some of my games at herointeractive.com!
I'm working on a new game, and a problem has come up that I simply haven't been able to find an answer to!
The problem is this:
If I have a cannon that launches a projectile at location 0,0 on a cartesian coordinate system, and that particle is fired with a velocity v and is subject to gravity g, what is the angle theta that is needed in order to hit target x,y. In particular, if the target is at a different height than the launch point.
I've tried some equations that have worked if the target's y location is that of the launch point, but they won't work if it is different. I can control g and v and all that good stuff, I don't need to find x or y, what I need is theta, the angle to rotate my gun to, in order to hit a location x, y.
Can anyone please help me?
Offline
First, the projectile has to be launched with enough velocity v to reach the target. If v is too small, the projectile will never hit the target.
Assuming that v is sufficiently large, then let the projectile be fired at angle θ to the horizontal. If it takes time t to hit the target, then
Eliminating t from the simultaneous equations will give you a quadratic equation in tanθ. Then you just solve the quadratic equation for tanθ, and hence for θ.
Note that there may be two possible values for θ. This is because you can hit the target either with the projectile on its way up or with the projectile on its way down.
Last edited by JaneFairfax (2007-08-17 07:59:51)
Offline
Thanks JaneFairfax for your help!
I was under the same impression to what you are saying. I found something similar to what you said on wikipedia:
http://en.wikipedia.org/wiki/Trajectory_of_a_projectile
I found it under "Angle theta required to hit a coordinate (x,y)".
I've attached their resulting equation to this post, they suggest that it can hit an arbitrary point x,y that isn't level with the launch point.
This seemed perfect, so I tried it out.
My results are that this still only works when the target is level with the launch point. If I raise the target up, the projectile is too short, if I lower it below the launch point, it goes too far. If I'm level with my target, it hits dead on. Do you think there is something I'm missing?
Offline
Yes, thats what you should get after eliminating t from the equations I stated.
Note that in order for θ to have a real solution, the discriminant must be non-negative; i.e. you must have
The latter inequality can be discarded because y−√(x[sup]2[/sup]+y[sup]2[/sup]) ≤ 0 but v[sup]2[/sup] > 0. Hence
Your initial velocity must not be less than this threshold value, otherwise youll never hit the target.
Last edited by JaneFairfax (2007-08-17 10:04:03)
Offline
Yeah, I understand that the discriminant must be non-negative and thus non-imaginary in order to hit the target. But even when I can hit the target (I'm in range), if the target is higher than the launch point, the bullet is too short. If the target is lower than the launch point, the bullet will overshoot. If the bullet is level with the launch point, it's perfect.
Shouldn't it work even if the target is not level with the launch point? If not, what will?
Offline
Did you vary the angle of launch and/or the velocity as well? If you change the co-ordinates of your target, you must also change at least one of θ and v unless the new location of your target is in the projectiles trajectory. Since you are moving the target vertically, the new location is definitely not in that trajectory; therefore you will not hit the target with the same values of θ and v as before. Change one or both of them.
Last edited by JaneFairfax (2007-08-17 11:33:08)
Offline
i have also done these types of balistic calculations in games however there is one slight potential problem.
If your gravity is as it is in most games, then your gravity is added to the vertical velocity once every frame. Which means if your vertical velocity begins at 4 and the gravity is 1, then in three frames you will move 4 + 5 + 6 units. a distance of 15.
but if you consider the continuous case, you have that the acceleration is 1 so the velocity is given by t + 4, and so the distance formula is 0.5t^2 + 4t which means from t = 0 to 3 you will travel 16.5 units. Ouch!
The continuous calculation jane gave is correct in the real world but in this case its only an approximation. A potential solution is to eliminate speed from the projectile and instead calculate a position function based on time, and assign the objects position based on t, in fact you could simply say, velocity = position(t+1) - position(t) at every frame, but you still need to have the position function.
I should also mention we have a new forum for programming and computer science, Coders Corner, feel free to post there about any of your programming issues.
Last edited by mikau (2007-08-17 11:50:19)
A logarithm is just a misspelled algorithm.
Offline
Pages: 1