I need my entity to go from point A to point B with a given launch angle over a given time.

I've been able to figure out the initial velocity without the constraint of the launch angle, but I can't figure out how to incorporate that into the equation.

After launch the entity is only affected by gravity, and point A and B are not necessarily at the same height.



1 Answer 1


Note: the situation you describe (angle, start, end, and time of flight) is overdetermined. For many (most) inputs, there will be no solution that simultaneously satisfies all of these constraints.

(eg. If we take the start, angle, time, and just the horizontal coordinate of the endpoint, that already completely determines the launch speed - with no guarantee that this hits the necessary vertical coordinate of the endpoint!)

To address this I'll remove the time of flight constraint, so we're finding a ballistic trajectory from the start to the end point, with a given firing angle. The launch speed and duration before it hits the target will be left as unknowns to solve for.

I'll assume we're in 2D here. If 3D, you can reduce it to the 2D case by projecting onto the plane containing the start/end points and the gravity vector.

We'll subtract out the start position, so it becomes zero and disappears from our equations, and our target vector represents the offset from the start to the end of the trajectory, fired with an initial inclination of angle in radians.

Our initial velocity is then the speed of the projectile times a unit vector in the firing direction:

initialVelocity = speed * Vector2(cos(angle), sin(angle))

With this, the projectile's position at time t is given by:

positionAt(t) = initialVelocity * t + (gravity/2) * t*t

(where gravity is a downward-pointing vector, like (0, -9.8))

Taking just the horizontal, x component at the time of impact T:

target.x = initialVelocity.x * T
target.x = speed * cos(angle) * T
T = target.x/(speed * cos(angle))

Substituting this into the vertical, y component:

target.y = speed * sin(angle) * T + (gravity.y/2) * T^2
target.y = speed * sin(angle) * target.x / (speed * cos(angle)
         + (gravity.y/2) * (target.x/(speed * cos(angle))^2
target.y = target.x * tan(angle) + (gravity.y/2) * (target.x^2/(speed^2 * cos^2(angle))
target.y - target.x * tan(angle) = (gravity.y*target.x^2/2*cos^2(angle))/(speed^2)
speed^2 = (gravity.y*target.x^2)/(2*cos^2(angle)*(target.y - target.x*tan(angle))

Finally, since a speed is positive by definition (direction coming from the unit vector above):

speed = sqrt((gravity.y*target.x^2)/(2*cos^2(angle)*(target.y - target.x*tan(angle)))

Ta-dah! :D

A few things to note: this becomes undefined...

  • if angle is vertical: you can still hit something directly below you with any velocity at all (gravity does all the work) or directly above you if you fire straight up with speed >= sqrt(-g*target.y/2) If it's to the left or right at all then we can't hit it with this angle.

  • if target.y >= target.x * tan(angle): the target is too high. The parabola kisses the line y = x * tan(angle) only at the moment it's fired - from then on gravity pulls it downward, so if the target is above that line then we'll never hit it even with infinite speed.

I've written a few previous answers on the subject of planning ballistic trajectories - you might also find those useful for reference:

  • \$\begingroup\$ Yeah, I was wondering about that, that's why I was having a hard time figuring it out I guess :P ...thanks for the thorough explanation! Much appreciated. My dilemma is that I want to decrease the duration, but in doing so it will simply decrease the arc (with the current trajectory formula I'm using), but then my object can potential hit ledges on the way up, so I need to maintain a minimum arc, while simultaneously maintaining a minimum speed. Sort of a dilemma :/ \$\endgroup\$ Apr 15, 2016 at 17:59
  • 1
    \$\begingroup\$ As you can see in the linked answers, start+end+time completely determines the shot. You don't have any additional tuning variables if you fix those three, but you can clamp down to the minimum shot time that clears all obstacles. \$\endgroup\$
    – DMGregory
    Apr 15, 2016 at 18:01
  • \$\begingroup\$ And the velocity would adjust according to the angle. If the velocity varies to keep the arc, then time must vary as well. Seems obvious now :D \$\endgroup\$ Apr 15, 2016 at 18:08
  • 1
    \$\begingroup\$ @RamonJohannessen Sure! Write it up as a new question, something like: "How do I take 2D ballistic trajectory planning and apply it to 3D?" — that way it will be searchable for other users with similar questions. \$\endgroup\$
    – DMGregory
    Apr 18, 2016 at 17:36
  • 1
    \$\begingroup\$ Done. Thank you! Question is here gamedev.stackexchange.com/questions/120085/… \$\endgroup\$ Apr 18, 2016 at 17:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .