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.
Answer
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:
No comments:
Post a Comment