Vectors – Moving Point Along the Vector

3danalytic geometryfloating pointvectors

I'm making a video game in which a ball is moving towards a player. So, I have a point $P$ describing the point where the player is, and a point $B$ describing where the ball is. I know that we can represent the direction from $B$ to $A$ as a vector.

I want to move the ball a small amount towards the player, along the direction of this vector. Is there a simple way to determine the new coordinates of the ball?

Best Answer

Let's say you have a vector $\vec P = [P_1, P_2, P_3]$ that represents the point's location in space. Another vector, $\vec B = [B_1, B_2, B_3]$ represents the ball's (or bullet's or whatever) position in space.

The vector $\vec {BP}$ between the two is: $$\vec{BP} = \vec P - \vec B = [P_1 - B_1, P_2- B_2, P_3-B_3]$$

So, if you want the ball to move all the way to the player, you would say: $$\vec B_{new} = \vec B + \vec{BP}$$

Or, if you want the ball to move only $1/100$th of the way to the player, you would say: $$\vec B_{new} = \vec B + \frac{1}{100}\vec{BP}$$

In general, to move some small $\epsilon$ to the player: $$\vec B_{new} = \vec B + \epsilon\vec{BP}$$

Related Question