# Passing a ball between two players

I am building a soccer game.

I want my user to pass a ball between two players

Player A is kicking the ball. Player B is following a line of Vector points with a constant velocity.

What kind of formula would tell me at which point in the line player A should aim for?

Thanks.

-
Can you assume the speed of the ball as a fixed constant? Or player A can adjust this speed? – Patrick Li Jan 26 '13 at 0:35
Ideally I'd like the ball to decelerate. We weren't expecting players to run in a straight line, which both the answers so far assume. – David Jan 27 '13 at 19:28

Let's assume the ball is being kicked from the origin with constant velocity, and everything is 2 dimensional. Player $B$ will be at $(b_1,b_2)$ and moving with constant velocity $<v_1,v_2>$. You try to find the angle $\theta$ at which to kick the ball with initial velocity $v_b$ such that it intercepts the path of $B$.

$B(t),$ the position of $B$ at time $t$, is given by $(b_1 + v_1t,b_2+v_2t)$, and the ball's position at time $t$ may given by $(v_b\cos\theta t- {1 \over 2} \alpha t^2,v_b\sin\theta t- {1 \over 2} \alpha t^2).$ For some constant factor $\alpha$.

This reduces to solving the system of equations: $$b_1+v_1t = v_b\cos\theta t- {1 \over 2} \alpha t^2\\ b_2+v_2t = v_b\sin\theta t- {1 \over 2} \alpha t^2$$

for $\theta$ and $t$ given all other values constant. Which produces a long and complicated output that I can't copy and paste here.

It's probably fine to ignore the acceleration factor, and assume that $\alpha = 0$. If we do this we can solve and get $$\theta = \arctan \frac{b_1+v_1t}{b_2+v_2t}$$

as well as an expression for $t$ by the following: $$(b_1+v_1t)^2 = v_b^2\cos^2\theta t^2\\ (b_2+v_2t)^2 = v_b^2\sin^2\theta t^2\\ (b_1+v_1t)^2+(b_2+v_2t)^2 = v_b^2 t^2\\ (v_b^2-v_1^2-v_2^2)t^2-(2b_1v_1+2b_2v_2)t-(b_1^2+b_2^2) =0\\$$

which produces the positive solution $$t= \frac{2b_1v_1+2b_2v_2 + \sqrt{(2b_1v_1+2b_2v_2)^2+4(v_b^2-v_1^2-v_2^2)(b_1^2+b_2^2)}}{2(v_b^2-v_1^2-v_2^2)}$$

which you can plug into the expression for $\theta$ to get a value.

-
Could you expand on where the quadratic comes from? – DJohnM Jan 26 '13 at 3:22
I added a few steps that should explain it. Note that $\cos^2+\sin^2 = 1$ – Sam DeHority Jan 26 '13 at 3:58
Thanks, for this. I was hoping the players would follow a line of points though. Is there any way for your answer to support this? – David Jan 27 '13 at 19:31
@David Sure. If the player decides to kick the ball at some non-fixed speed $v_b$ then the point for which he is aiming the ball is the line starting infinitely far away from the other player starting with $v_b = 0$ and at the target player with $v_b = \infty$., the line going in the direction the target is moving. – Sam DeHority Jan 27 '13 at 19:36

The problem can also be attacked using vectors; specifically, non-perpendicular components

Assume that the kicker is at the origin, the target is a distance $R$ at an angle θ, or at R∡θ.

The target is travelling at a speed V in a direction defined by the angle α; in other words, the target’s velocity is V∡α.

The kicker delivers the ball with a velocity S; we want to find if there is an angle β for the ball that will allow the ball to catch the target; in other words, find S∡β.

Assume first that one component of the kicked ball's velocity, S, matches the target's course and speed exactly, In other words, that component is V∡α. (Don't worry if V is greater than S!)

Since the ball’s movement is now matching the target’s movement exactly, the target will stay at the same distance and direction, R∡θ, relative to the ball. So the next thing we must do is add a velocity component with unknown size X towards the target; that is, with a velocity X∡θ. Again, these two components, V∡α, and X∡θ are not necessarily at right angles to each other. This velocity X will eventually cover the distance R, and the pass is a success! To finally solve the problem we need to calculate if a real, positive value for X exists, and what that value is. We can then find the angle β.

If we resolve these vectors into conventional x-y components and do some trig substitutions, we come up with the quadratic: $$X^2+[2V{cos}(α-θ)]X+V^2-S^2=0$$

If this quadratic has no real roots, or two negative roots, there is no possible kick direction. If there is one positive solution or two positive solutions, then there is one or two possible kick directions.

The trig mentioned before also leads to the two equations: $$S cos (β)=X cos (θ)+V cos (α)$$ and $$S sin (β)=X sin (θ)+V sin (α)$$

and now that we know X, we can calculate the value(s) of β.

Oh, and R/X gives the travel time(s)...

-
Thanks, for this. A little niggle though, I was hoping the players would follow a line of points. Is there any way for your answer to support this? – David Jan 27 '13 at 19:30