I'm trying for a week now and I really have a hard time with this one. I'm pretty sure that this is something very simple but I can't seem to find a solution. I'm making a game where the enemy is shooting arrows to the player without including air friction.
We already know the following values (just some random values):
- Enemy position [startPoint = (0,0)].
- Player position [endPoint = (20,10)].
- Gravity [g = 10] (In-game physics are using this value).
- Velocity [Vo = 25].
The question is "How to calculate the direction vector [u] using these values?".
I have already used u = 2Δ - gt² / 2Vo*t where Δ = endPoint - startPoint but it doesn't give me the correct vector which later will be multiplied with velocity.
I can see that because, by finding the angle of the vector θ = atan2(u.y, u.x) and getting back the direction vector u = (cosθ, sinθ), the vector is not the same.
After u is known, I can find the starting horizontal [Vox] and vertical [Voy] velocities and apply them to the arrow by doing:
Vox = Vo * u.x
Voy = Vo * u.y