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.
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.