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

Calculate the Pitch of a projectile with moving destination

  1. Feb 20, 2015 #1
    Hi all.

    I am not quite sure if this is the right forum to place this question but for me there are several forums that would fit with my situation :D

    I am making a game where I have to throw/shoot a projectile through a 3 dimensional space which has to land on a specific object.

    There are actually 2 similar questions that I want to ask (second one being the one with moving destination)

    1) How do I calculate the Pitch of a projectile, given the Velocity, starting location(x, y, z) and targeted location (x, y, z)?
    (x and y being horizontal location and z being the height.)

    Well this one seems pretty simple after reading some Wikipedia pages and finding that formula.
    I assume that g = 9.81 but can be modifyable in this formula... which is great.
    X and Y have different meaning which I can understand because you only have to know the horizontal_distance instead of the x_distance and y_distance.

    So given:
    g = gravity = 9.81 (m/s)
    x = horizontal_distance = √(end_location_x - start_location_x) + (end_location_y - start_location_y) (m)
    z = vertical_ditstance = end_location_z - start_location_z (m)
    v = initial_velocity = projectile_velocity (m/s)

    But if I apply the formula with those values, I get really weird results.
    I always get a value between 1.568 and 1.572 or a NaN
    And yes I exacly copied that formula.

    I guess that the NaN is when the projectile cannot reach the target with the given speed and gravity, but if I set gravity to 0, I sometimes get NaN values too which are impossible then.
    So I messed up with the gravity (I guess)

    So the question: What have I done wrong with the values?

    2) This one is a bit harder.
    Now I got to do the exact same thing, but now the target is MOVING!
    There is one relief: the target is moving with a constant speed in a specific angle in only the X and Y axis (only hoizontal)

    I talked with my friend about this one but ussually get stuff like "Why don't you just calculate the new location of the target and then apply the first formula?". And I know that they just don't understand that it is not that simple.
    However one of them made a very usable statement: polling.

    Just calculate the time that the projectile needs to reach the target location (without movement) and adjust the target based on that time, then do that again, and again, and again until you have a value that would be enough to make it almost perfect.
    However I refuse to use that because I require the exact angle and have to be carefull with performance (we are still talking about a game)

    I think that I could solve this one if I just run a few tests and compare differences, then apply factors inside the first formula to make the horizontal_distance, vertical_distance, horizontal_distance_increase and vertical_distance_increase adjust on velocity... or something similar to that.

    However I have to know what I do wrong in the first one to try this thing.
    But if there is anyone who can make a usefull statement of this situation, then he is welcome to share it.

    I know that this might be a big thing for the first question but if it was simple... then why would I be here?
     
  2. jcsd
  3. Feb 21, 2015 #2

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    There are squares missing in the formula for the horizontal distance.
    Apart from the issue above, the formulas look fine, so I would expect an error in your implementation of the remaining formulas.

    The moving target is much easier than the approaches you described: as long as you do not have air resistance, the acceleration is independent of motion (it is always given by gravity alone). If you take a given trajectory, and add a fixed initial velocity v, your path changes by v*t after time t. If you choose v to be the motion of your target, this is exactly the distance the target moved in the same time, so you always hit no matter when exactly this will happen.

    Therefore: solve the problem with the initial position of the target, calculate the three velocity components (x,y,z), add the target velocity. Convert to angles again if you like.
     
  4. Feb 21, 2015 #3
    O lol. i forgot the squares indeed.

    well i actually hoped that i misunderstood the formula variables...
    there are 2 things that i could have done wrong in the implementation of the formula:
    1) the plus-minus sign.
    i assume that using the one would lead to the high angle and the other to the low angle.
    however the plus-minus sign has a possible different meaning.

    2) a few times there are multiple variables after each other followed by a power.
    I used 2yv^2 as 2 * y * (v)^2
    i assume that that is right but it is the last thing that i can think of.
    i will post the implementation tomorrow evening, when i am back home.

    about the moving target.
    yes there is no air resistance.
    But if i understood you correctly i must disappoint you: velocity cannot be changed
    imagine a gun. if you aim at a target that moves away from you, will the initial velocity of the bullet change?
    it would be awesome but i rather want the gunner be a math genius and let him calculate the angle at which he must fire the bullet so it will perfectly hit his target :D
     
  5. Feb 21, 2015 #4

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    Right.
    Right.

    Hmm, fixed velocity... using the same approach as the wikipedia article used, it could be possible to derive equations for this case. For given launch conditions, both points follow known trajectories and the intersection can be calculated. I would transform everything to a coordinate system where the initial separation in one horizontal coordinate is zero, then you know one velocity component already (following the same logic above: has to be equal to the motion of the object). That leaves a two-dimensional problem. The horizontal coordinate gives a linear equation that allows to solve for time, then the vertical motion gives a quadratic equation.
    Could give an analytic solution, but certainly a single equation that can be solved in some way.

    Edit: Looks like a 4th order polynomial equation. There is an analytic solution, but it is messy. A numerical approach might be easier.
     
    Last edited: Feb 21, 2015
  6. Feb 21, 2015 #5
    4th order polynomial equation? 0.o
    uhmm... i think that i have to search some stuff on Google before i can continue in this situation
     
  7. Feb 21, 2015 #6

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    Something like a*x^4 + b*x^3 + c*x^2 + d*x + e = 0.
    At least that is what I got. There might be an easier solution but an inital motion of the target towards or away from the origin makes the equation complicated.
     
  8. Feb 22, 2015 #7
    I solved the formula part... i just got used to different syntax and used a wrong one.

    It seems that i have used quartic functions back at secondary school... however, never where I should make the values.
    I had this idea that you could somehow use velocity and total_distance to find the duration of the flight... but after thinking about it for a while, that is pretty much impossible... I think.

    I do believe that the formula has to be changed to use raw location values directly.
    I mean instead of x and y, start_location_x, start_location_y, start_location_z, end_location_x, end_location_y, end_location_z, end_location_x_increase, end_location_y_increase
    because if the target would move perpendicular to the direction of the projectile, the x (distance) would increase slower in the first second compared to the 40th second

    I think that the best I can do is let the master do it :D

    I am pretty much out of options but if I got some ideas I will definatly tell them.
     
  9. Feb 22, 2015 #8

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    Okay, I guess as this is certainly beyond the typical "find the angle" problems, I guess I can help more directly.

    This is the approach I would suggest: Let z denote the vertical axis, x and y are horizontal.
    Do a rotation in the x/y-system such that the distance in the "new" y becomes zero.

    We now have 4 parameters:
    the distance in x-direction, let's call this "d".
    the distance in z-direction, "h" (where positive h means the target is above the cannon)
    the target velocity in x-direction, "A" (where positive A means the target is moving away)
    the target velocity in y-direction, "B".
    vx, vy, vz denote the initial velocities of the projectile. Their total speed vtot is given by ##v_{tot}^2=v_x^2+v_y^2+v_z^2##.

    Our projectile has to match the target velocity in y-direction, so we know vy=B. This suggests to introduce the speed v in the x/z-plane, ##v=\sqrt{v_{tot}^2-B^2}##.

    Now the problem is two-dimensional. In the x-component, projectile and target are approaching each other with the relative velocity vx-A, which means we hit the target at time
    $$t=\frac{v_x-A}{d}$$
    At this time, also the vertical position has to match, which leads to
    $$v_z t - \frac{g}{2} t^2 = h$$
    We can solve the equation of v from above for v_z: ##v_z = \sqrt{v^2-v_x^2}##.
    Plugging in this and the equation for t in the equation for the vertical position, we get
    $$\sqrt{v^2-v_x^2} \frac{v_x-A}{d} - \frac{g}{2} \frac{(v_x-A)^2}{d^2} = h$$
    Multiply both sides with 2d^2
    $$2d \sqrt{v^2-v_x^2} (v_x-A)- g(v_x-A)^2 = 2hd^2$$
    and rearrange
    $$2d \sqrt{v^2-v_x^2} (v_x-A) = 2hd^2 +g(v_x-A)^2$$
    Square both sides
    $$4d^2 (v^2-v_x^2) (v_x-A)^2 = 4h^2d^4 +4hd^2g(v_x-A)+g^2(v_x-A)^4$$
    The only unknown variable here is vx. Expanding all terms gives a quartic equation, which can be solved analytically or with numerical methods to find vx. This can be used to find vz then. Reversing the initial rotation allows to find the velocity components in the original coordinate system, and trigonometry can give the angles if necessary.
    A vertical motion of the target can be included, it makes the formulas a bit longer but not much more complicated.
     
  10. Feb 23, 2015 #9
    hmm... did I already mention that I am not very good in maths or physics?

    I really try to understand what actually happens but I get lost very fast.
    All that I understand from it is that you don't have a solution.

    If so then I got an idea of a different approach:
    If you use this and this as concept (the dots are given, v_p = horizontal velocity of the projectile, v_d = horizontal velocity of the target, A = angle of where the target is moving to and a = initial/current distance between projectile and target.)
    and you can make a formula to find t, and you change the formula for c, then you have the exact collision location right?
    horizontal_velocity can be calculated if you have the time, initial velocity, difference in height, gravity and the certainty that you use the low/high angle.
    At least I believe that is possible.

    For no difference in height, the final z_velocity is the same as the starting z_velocity, so if you take half of the time, and use gravity to make acceleration of the z_velocity from 0 to ..., that ... is the initial and final z_velocity. Then you have vhorizontal2 = vtotal2 - vvertical2

    I just don't know how it can be done if height has a difference.
    Equal as I don't know what the formula would be for t in the picture I showed.

    I don't know if this is a good approach but I think it might be helpfull.
     
  11. Feb 23, 2015 #10

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    I have a solution, I described the steps but did not write out all of them in detail.

    Once you have one variable, the others are not so tricky, but finding the first part of the solution is the interesting step. See above for one approach, where I think it is the easiest one.
     
  12. Feb 23, 2015 #11
    "Do a rotation in the x/y-system such that the distance in the "new" y becomes zero."
    uhm... I lost it right there.
    The movement of the target does not increase or decrease the distance with a constant speed.
    The first second the speed increases slower than the 40th second for example... except if the direction is exacly towards or away from the projectile.
    So I don't really understand what happens in that rotation.

    "The only unknown variable here is vx."
    Yes.
    When reading the second formula (t = vx−A / d) (sorry but how do I make those formulas in these posts?)
    I thought you suddenly have that variable as a value.
    That value is an unknown dynamic variable with vtotal and vy as keys. We have vtotal but are missing vy and that one is an unknown dynamic variable with vtotal and vx as keys.
    So we require the angle to make those values with cos and sin.

    So I don't understand how that is the solution.
    As I said, I am not very good in maths.
     
  13. Feb 23, 2015 #12

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    The motion of the target is not relevant for this step, I consider the initial position only.

    The absolute distance as function of time does not change linearly, but this value is not used anywhere. Just the initial distance and the (constant) velocity are used.
    You can use LaTeX.

    We don't. The final equation there just has known values and vx. Which step is unclear?
     
  14. Feb 23, 2015 #13
    well I got no clue of how to get vx out of that equation.
    so I got no clue to get the next variable and so, no clue of how to find the horizontal angle and vertical angle.
     
  15. Feb 24, 2015 #14

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    With the formulas here or with numerical methods like Newton's method.
     
  16. Feb 26, 2015 #15
    -_- can't get it working.
    I get stuck every time.
     
  17. Feb 26, 2015 #16

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    That post gives exactly zero chance to see where the problem is.
     
  18. Mar 6, 2015 #17
    Sorry for late reply but there was a little problem.

    I can't get it to work because I don't know how I should use Newton's method or the formulas that you linked.
    I can't see how the formula that you made is similar to what those wiki pages say.
    That is why I can't figure out how the variables are set.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Calculate the Pitch of a projectile with moving destination
Loading...