# Calculating initial velocities given trajectory parabola

I'm working on a volleyball game but my maths/physics knowledge isn't quite up to par. I need some help with the following problem:

The player can hit the ball from anywhere on the left of the court into one of three points on the right:

    P    | x  x  x


The ball travels in a parabola defined by the three points, all of which are known:

• The players X coord and height (Xp, Yp)
• The point just above the top of the net (0, Yn) (assume the left of the court is negative along the x axis)
• The point where the ball impacts the ground on the other side of the net (Xi, 0)

I need to calculate the initial X/Y velocities when the player hits the ball so that the ball, affected only by gravity, follows that parabola. EDIT Alternatively, the velocity magnitude and angle from which the X/Y component velocities can be calculated.

I'm able to find the function of the parabola using pen and paper but since this involves "saving" equations with unknown variables I have no idea how to translate that to code (how could you have variable a = 2b for example if b is undefined?). I'm not even sure if finding the function is necessary for this problem.

Any help?

EDIT: I just came to the realisation that if I can calculate the flight time then I can divide the distance traveled each frame along the x axis and just plug that into the parabolic function to get the ball's y coordinate. I guess that's my next avenue of research.

• So in simple words you want to move your ball in projectile trajectory? – Hamza Hasan Jan 11 '16 at 5:26
• Yes, but the trajectory needs to move through those specific points. I can easily give the ball some arbitrary initial velocities, slap some gravity on and have the ball follow a projectile trajectory, but I need the ball to land in a specific location AND just clear the net (if it flies well over the net it's not going to be a very good volleyball shot). – NoeL Jan 11 '16 at 5:36
• I posted an answer, may be not 100% is what you want, but you may derive your needs. – Hamza Hasan Jan 11 '16 at 5:41

To simplify the math, let's translate everything so the projectile starts at the origin (subtract player position from the other points). Translating the whole problem doesn't modify the velocity, so we don't need to do any compensation at the end.

P1 = point above the net - player position (reached when t=t1)
P2 = target point - player position (reached when t=t2)
v = initial velocity vector
g = gravitational acceleration vector


Then the general form of our parabola is...

P(t) = t * v + t*t * g/2


We can substitute t1 & P1, t2 & P2 to get...

P2/t2 - t2 * g/2 = v = P1/t1 - t1 * g/2


Since gravity only acts on the vertical axis (in this case), the x component is a bit simpler:

P2.x/t2 = P1.x/t1
t1 * P2.x = t2 * P1.x
t1 = t2 * P1.x/P2.x


(If by chance anyone wants to apply this formula to a case with arbitrary gravity direction, you can first form a local coordinate system with one axis parallel to the gravity direction and one axis perpendicular, solve there, then transform the result back to the original coordinate system)

Now we can substitute this into the above formula for the y axis and solve until we get...

t2 = sqrt(2 * (P2.y - P1.y*P2.x/P1.x)/(g.y*(1 - P1.x/P2.x)))


(If the inside of the sqrt is negative, then there is no ballistic arc joining the three given points with this gravity value. There might still be a solution if the point above the net is moved vertically)

Note that, because both P1.x and P2.x appear in denominators, this will behave badly if the player (and their target) can get right up to the net. You can either force the player & target to keep some minimum distance to the net, or fall back on a different approach for close-to-net shots.

Once you have t2 in hand, you can solve for v:

v = P2/t2 - t2 * g/2;


Note that this gives a lower bound for the time to hit the target. Increasing the time will result in higher arcs with more clearance above the net. (ie. they pass above P1 instead of passing through it)

Here's an animation of this method in action, moving the three points freely:

• This looks fantastic and more versatile than the solution I came to myself. I'll try implementing it and see how it goes. Thank you! – NoeL Jan 12 '16 at 12:13
• @DMGregory Hi, I have the same problem. But I'm having hard time understanding how do I get the P1 and P2 – ronscript Aug 2 '16 at 6:04

If I understand your question, your objective is to move your ball like projectile. If so then you can use parametric equation of Projectile, that is,

x = (v * t * Cos(theta)) + originX
y = (v * t * Sin(theta) - 0.5 * g * t * t) + originY


where v is initial velocity, g is gravity, originX and originY is your ball initial coordinates, theta is initial angle and t is the time.

You can use this equation in your GameLoop with everything known.

For example, initially you can set v, theta, g, originX, originY.

I can demonstrate via pseudo code.

Let say, your ball is at (3,5) then,

v = 5
g = -9.81
theta = 35
originX = 3
originY = 5
t = 0

GameLoop{
ball.x = (v * t * Cos(theta)) + originX
ball.y = (v * t * Sin(theta) - 0.5 * g * t * t) + originY

// Nothing to change except "t"
t++
}


By using this equation your ball will move in projectile trajectory. You can further play with it by changing theta, velocity (v).

I hope this helps.

• Thank you, but unfortunately this doesn't help me since I don't know v and theta (they're what I'm trying to calculate). – NoeL Jan 11 '16 at 5:50
• So you want to get v and theta known before firing? – Hamza Hasan Jan 11 '16 at 5:52
• Yes, that's correct. EDIT: Or if there's another method to just move the ball along the parabola over time in a way consistent with a constant gravity. – NoeL Jan 11 '16 at 5:53
• So have look on this question I asked. Although I have not tested it yet, but it might help you – Hamza Hasan Jan 11 '16 at 5:54
• I just came to the realisation that if I can calculate the flight time then I can divide the distance traveled each frame along the x axis and just plug that into the parabolic function to get the ball's y coordinate. I guess that's my next avenue of research. – NoeL Jan 11 '16 at 6:11