Search code examples
javascriptphysicsgame-physics

2d parabolic projectile


I'm looking to create a basic Javascript implementation of a projectile that follows a parabolic arc (or something close to one) to arrive at a specific point. I'm not particularly well versed when it comes to complex mathematics and have spent days reading material on the problem. Unfortunately, seeing mathematical solutions is fairly useless to me. I'm ideally looking for pseudo code (or even existing example code) to try to get my head around it. Everything I find seems to only offer partial solutions to the problem.

In practical terms, I'm looking to simulate the flight of an arrow from one location (the location of the bow) to another. I have already simulated the effects of gravity on my projectile by updating its velocity at each logic interval. What I'm now looking to figure out is exactly how I figure out the correct trajectory/angle to fire my arrow at in order to reach my target in the shortest time possible.

Any help would be greatly appreciated.


Solution

  • Pointy's answer is a good summary of how to simulate the movement of an object given an initial trajectory (where a trajectory is considered to be a direction, and a speed, or in combination a vector).

    However you've said in the question (if I've read you correctly) that you want to determine the initial trajectory knowing only the point of origin O and the intended point of target P.

    The bad news is that in practise for any particular P there's an infinite number of parabolic trajectories that will get you there from O. The angle and speed are interdependent.

    If we translate everything so that O is at the origin (i.e. [0, 0]) then:

    T_x = P_x - O_x            // the X distance to travel
    T_y = P_y - O_y            // the Y distance to travel
    
    s_x = speed * cos(angle)   // the X speed
    s_y = speed * sin(angle)   // the Y speed
    

    Then the position (x, y) at any point in time (t) is:

    x = s_x * t
    y = s_y * t - 0.5 * g * (t ^ 2)
    

    so at impact you've got

    T_x = s_x * t
    T_y = -0.5 * g * (t ^ 2) + s_y * t
    

    but you have three unknowns (t, s_x and s_y) and two simultaneous equations. If you fix one of those, that should be sufficient to solve the equations.

    FWIW, fixing s_x or s_y is equivalent to fixing either speed or angle, that bit is just simple trigonometry.

    Some combinations are of course impossible - if the speed is too low or the angle too high the projectile will hit the ground before reaching the target.

    NB: this assumes that position is evaluated continuously. It doesn't quite match what happens when time passes in discrete increments, per Pointy's answer and your own description of how you're simulating motion. If you recalculate the position sufficiently frequently (i.e. 10s of times per second) it should be sufficiently accurate, though.