Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Easy Question: Formula for Launch Angle? Projectile Motion.

  1. Jan 18, 2012 #1
    Edit: I solved my problem for launch angle. So now I have another question.

    I am writing a script for a cannon in a video game that will aim at XYZ coordinates by determining the angle and bearing needed to launch the projectile to hit the target. I wish to add a target-leading formula so that it can lead targets moving in a straight line with constant velocity.

    The problem is that the time to impact, "t," must be factored into determining the coordinates to aim at in order to lead the target, but in order to determine the time to impact, the coordinates must be known.

    So, given the current XYZ of the target, the initial XYZ of the projectile, the initial velocity of the projectile, and the velocity of the target, is there any way to solve for the XYZ of where the target will be in an unknown timespan "t" (the flight time of the projectile)?
    Last edited: Jan 19, 2012
  2. jcsd
  3. Jan 19, 2012 #2


    User Avatar
    Science Advisor
    Gold Member

    You need to be solving a pair of simultaneous equations. Alternatively, you could google trajectories/ballistics/artillery and there is bound to be a formula in there somewhere. (I chucked out my School Maths note book nearly fifty years ago but is was in there somewhere.)
  4. Jan 19, 2012 #3
    The equation of trajectory might be helpful in finding out the desired result.Although,it may lead to complex mathematics.
  5. Jan 19, 2012 #4


    User Avatar
    Science Advisor
    Gold Member

    It depends on what you call complex. If you are making a video game then you are, presumably, using a processor and a programming language. All the languages can evaluate simple mathematical expressions. The shape of the trajectory need not be too accurate as long as the ends are in the right place.
  6. Jan 19, 2012 #5
    It seems to me that there are too many unknowns.

    I have the trajectory equation down, I can get the right angle to hit any target at any elevation (assuming it is within range) with a mortar shot (high angle shot as opposed to low angle).

    But the target leading seems to require a "guess and check" method. The problem with such a method is that it takes time for a computer to run, time that during which, the target will move thus altering the "future position" at a time T seconds from the moment of calculation, so this method basically forces itself to give a wrong answer simply because of the time it takes to perform.

    I don't know if it is possible, unless you could somehow get an equation for T (time of flight) that does not require X and Y to be known (2d projectile motion).

    Plus it just logically feels like an impossible thing to answer. It is like asking someone with a giant supercomputer that could compute the probabilities at a subatomic level to predict the future:

    "What happens at a precise moment in time in the future?"

    "Well when is this precise moment in time?"

    "I don't know."
    Last edited: Jan 19, 2012
  7. Jan 19, 2012 #6


    User Avatar
    Science Advisor
    Gold Member

    I see your problem. It looks like you would need some successive approximation loop. Not too hard, though, because they used to use 'predictors' on AKAK guns in the WW2 and they would have been really sluggish, analogue things. The time taken would depend upon the required accuracy.

    BTW, you say you can do the 'mortar' trajectory and not the 'field gun' trajectory. They both just involve the two possible roots of the quadratic equation so either should be just as easy. (no?)
  8. Jan 19, 2012 #7


    User Avatar
    Science Advisor
    Gold Member

  9. Feb 21, 2012 #8
    I was just working out the details of this same exact problem for an AI mortar for a game. Here is what I've come up with. I am not using standard spherical coordinates so I have tried to designate my angles using different designations than usual.

    α is the change in angle from the position of the target and where the target will be when the projectile hits the ground.

    t = 2v/g cos(ψ)

    where ψ is the angle from the z-axis (I assume z is pointing upwards), and v is the muzzle velocity. The AI engine is going to be aware of the NPC's position at any given time. so at t=t₀ the NPC is located at (x₀, y₀, z₀). At t = 2v/g cos(ψ) the NPC is located at (x₁, y₁, z₁).

    Since the target is moving with velocity u along some (hopefully predictable) path. we can describe the path using some function. For my purposes these are lines but you may do something different so lets just say.

    r(t) is some vector function that returns a position at a given time. We can then use the distance formula to calculate the distances between the current position and the landing position.

    d₁ = distance between current position of target
    d₂ = distance between me and target after the projectile flies

    These give me a triangle where the only missing side is the side connecting these two points to eachother. For me this is easy to handle using r(t) = vt but in general, you can use the average value formula to connect these. Thus your AI won't be dead perfect at shooting NPC's but it will be darn close. We will call that other side d₂₁ and across from it was our earlier presented angle α.

    cos(α) = ((d₁)² + (d₂)² - (d₂₁)²)/(2√(d₁d₂))

    Which as you imagine came from the law of cosines, but more importantly, this one is not too computationally expensive depending on what your r(t) function looks like. In my code these distances had to be calculated for something else anyway so the algorithm made sense and it is not some recursive type function that reduces error with each iteration like you'd been suggested to use previously.
  10. Feb 21, 2012 #9
    On a quick note, just be aware that "the time factored in" is only dependent on your angle of launch from the vertical, not the bearing. That is where my t = 2v/g cos(ψ) came from.

    That time is basically constant in particular you can choose for your AI to launch at ψ = π/4 for all your calculations to simplify things substantially.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook