Get directional vector from point A to point B

$\begingroup$

Im currently trying to figure out following vector maths problem:

I have a point $A$ and a point $B$ and I want to get the directional vector (if that's incorrect, please tell me) that is needed to go from $A$ to $B$. Normally I would get this vector as a user-based input, like something from a gamepad, keyboard, etc. Since this is a mathematical simulation though, I "just" need the exact vector I need to apply on each iteration to move towards the destination $B$ (About one unit per iteration, like $(1, 0, 0.5)$).

I hope I outlined my problem understandable enough, if not just tell me in a comment.

Much thanks in advance!

Example:If $A = (0,0,0)$ and $B = (3, 0, 0)$ the "directional vector" that I'd need would be $(1, 0, 0)$, since I only need to move along the first axis.

$\endgroup$ 3

2 Answers

$\begingroup$

The vector pointing in the direction from point $A$ to point $B$ is $\vec{BA} = \vec{OB} - \vec{OA}$. In your case you'd have $\vec {AB}=(3,0,0)-(0,0,0) = (3,0,0)$. Then if you'd like to normalize it, you divide by its length: $\widehat {AB} = \dfrac {(3,0,0)}{3}=(\dfrac 33, \dfrac 03, \dfrac 03)=(1,0,0)$.

In case you've forgotten (or haven't taken linear algebra), the length of a vector $(a_1, a_2, \dots, a_n)$ is just $(a_1^2+a_2^2+\cdots +a_n^2)^{1/2}$.

If you'd like a vector $\vec v$ such that $\vec{AB}=4\vec v$ (or however many iterations you want), then after you find $\vec{AB}$, you divide it by that number, that is $\vec v=\dfrac {\vec{AB}}{4}$.

$\endgroup$ 2 $\begingroup$

It seems like you are looking for a unit vector in the direction of the direction vector from $A$ to $B$. To get that, you calculate the direction vector, and the divide each coordinate by the length of the vector: $\sqrt{x_1^2 + x_2^2 + x_3^2}$. If you want even smaller iterations, you can divide each coordinate by twice, or ten times the distance between the points.

$\endgroup$ 1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like