Projectile trajectory

Started by
10 comments, last by ilkhan_v4 22 years, 6 months ago
I saw a post about this just a minute ago and it reminded me of a problem I''m having. Kind of similar, but not really. I''m working on a 3D RTS. Let''s say I''ve got the tallest point between one of my units and an enemy unit. I''d like to know the right angle to fire the gun at to clear the tallest point and hit the unit on the other side. If the enemy unit can''t be hit with any angle, the unit needs to move forward. This way, the unit only gets as close as it needs to. It would also be nice to be able to constrain the angle to certain values. I''ve got some basic equations for finding the distance traveled, maximum height, etc., but my brain is too fried to be able to figure out what I need. Can anyone help?
Advertisement
how are you computing the missle range? does every shot go the same distance? if you know the power of the shot every time you can jsut test an equation for every angle (shouldn''t take long) and if one works, the craft fires.
The problem is, the horizontal range is dependent on the angle. With real guns (which is what I''m trying to simulate), there''s really no "range" per se. There''s only the muzzle velocity of the gun. Sure, there might be a maximum range, but that''s firing at a level or slightly high angle.

I really just want to make the units smart enough to fire over obstacles but still stay as far away as they can. I''ll deal with visibility, accuracy, etc. later. Plus, I''m using floating point numbers, so going through the possible angles would take a long time, especially when units are moving!
Well, I don''t know how much help this will be, because I am just taking Physics now, but I think this is a simple Kinematic Equation problem.

Remember, to get maximum distance from a projectile, you need to launch at 45 degrees. Then from that, convert that vector into its X and Y components, and solve the Kinematics for the problem. I wish I could be of more help, but I don''t have all the equations off hand right now. Sorry.
"...."
No problem. I''ll keep working on it, but I was just hoping someone else had run across the same problem and already had a solution.
What is your ballistic equation? Is it based on Newton''s law or a differential equation using RK4? The type of equation will make a big difference when determining its maximum range.

Guy
Guy
I believe it''s based on a Newtonian equation. I''m discounting air resistance and using what I''ve found on some physics tutorials online which is this:

vertical velocity = muzzle velocity * sin theta
horizontal velocity = muzzle velocity * cos theta

vertical displacement = vertical velocity * t * 0.5 * g * t^2

Where t is time and g is gravitational constant. Problem is, I can''t figure out vertical displacement without knowing t, but I can''t figure out t without knowing the angle, but to know the angle, I need to know t! It''s pretty frustrating. Basically, if I know what I need the vertical displacement to be, but I want to know the angle to get there. Once I figure that out, I can figure out the horizontal displacement which will tell me if the unit''s in range or not. Then I can stop ripping out my hair.
quote:Original post by IceWizard
What is your ballistic equation? Is it based on Newton''s law or a differential equation using RK4? The type of equation will make a big difference when determining its maximum range.

It shouldn''t! If your differential equation doesn''t get the same range as the physics one, there is something wrong with it.
Due to air resistance, firing angles a little lower than 45 degrees yield higher ranges, especially for smaller, faster projectiles (since they have less momentum.) Without air resistance, for example, a modern rifle would have an insane range. It doesn''t.
Disclaimer: The following is totally impractical in a video game. I''ve been thinking about it for about 20 minutes and haven''t come up with any kind of approximation that would be much more acceptable, but I''m still thinking and may post back later ;p

The problem you were talking about, needing t to find theta and theta to find t, can be eliminated by making t a function of x and theta. That is, you know the distance to the wall, x, and you know the muzzle velocity, V, and so you know that x will equal V*cos(theta)*t. Solving that for t, you find t=x/(V*cos(theta)).

Now, you take your height equation, y=V*sin(theta)*t - 0.5*g*t^2 and substitute what you found t to be. That is, y=x*tan(theta) - 0.5*g*x^2/(V*cos(theta))^2. You know all of those variables except theta, so you could theoretically solve for theta. Actually doing it would be quite a trick, though - I certainly don''t see an easy way to do it, though I may be missing some substitution that could be done. If you looped through a couple (well, a _lot_) of values of theta you could probably find one that gave you an answer acceptably close to correct. Talk about processor consumption, though... - you really couldn''t do it.

I''m trying to work out some approximation based on the fact that the problem gets much simpler if we ignore the effects of gravity. It''s really easy to find theta without gravity. If there was some way to estimate the time it would take to get to the wall and then add the height ''lost'' due to gravity in that time, then you could just add that height to the height you''re trying to achieve and get your answer in one step, not a loop of checking solutions.

Anyway, I think you''re looking at the problem as simpler than it really is. The first mistake I think you''re making is thinking that the maximum range can be reached by firing the gun horizontally, when in actuality the maximum range is reached at an initial angle of 45 degrees, as was mentioned by someone else. This means that you can''t just check to see if the projectile will reach the target at the angle you found to clear the wall - there may be another angle that will clear the wall and _will_ reach the target.

Also, you won''t be able to simply find the highest wall between the shooter and the target and see if the projectile will clear it. There could be shorter walls that would block the projectile if they''re at the right position (imagine an 8 foot wall 1 foot away from you, and a 40 foot wall 100 feet away from you - you could clear the 40 foot wall easily but you''ll hit the 8 foot wall if you aim for the top).

I think an easier way to look at the problem would be to do it backwards. The maximum number of angles that will land a projectile at a certain distance there will ever be is 2. The maximum range can only be reached with one angle, anything past the maximum range cannot be reached at all, and there are two angles that will reach every point in between the shooter and the maximum range. You can find those two angles pretty easily (I think you already mentioned you had a method to do this) - the simplest method might just be to check those two angles and see if they''ll clear all the obstacles between the shooter and the target - if not, the shooter should move. Note that moving backwards will sometimes be the solution, though - moving forward won''t always be better. Also, moving sideways could help, because if you sidestep out of the way of a wall.. as you can see, deciding which way to move gets more complicated than finding the angle to shoot.

By the way - if you do find that both angles will clear all obstacles, choose the lower one - the rocket''ll get there faster.

-riley
--Riley

This topic is closed to new replies.

Advertisement