# Vector Projection - Sign?

I'm building a game where I need to calculate the projection of one vector on another. I've written a C++ function to return the projection of a vector onto another vector, but I'm getting surprising results that have me questioning my understanding of this operation...

I have a vector $\vec{A}$ defined in $3$-space as: $(5, 3, 2)$. I have a vector $\vec{B}$ defined in 3-space as: $(0, 0, -1)$. Now, if I understand this right, projecting $\vec{A}$ onto $\vec{B}$, would result in a vector in the direction of $\vec{B}=(0, 0, -1)$ with the magnitude of $\vec{A}$... or:

$(0, 0, -2)$.

Is this right, or I am butchering it? My function keeps returning (with the above test case): $(0, 0, 2)$. The magnitude seems correct, but the direction seems wrong. I'm calculating this by doing:

$$\frac{(\vec{A} \cdot \vec{B})}{(\vec{B} \cdot \vec{B})} \cdot \vec{B}.$$

-
Eh... is it as simple as taking the absolute value of the scalar on the left side of the equation? – David Jun 17 '11 at 22:06

Your formula, your calculation and your answer $(0,0,2)$ are correct. This has nothing to do with the magnitude of (A), which is $\sqrt{5^2+3^2+2^2}=\sqrt{38}$. The projection of (A) along (B) is the component of (A) that lies along the line spanned by (B); this can be a vector pointing in the same direction as (B) or in the opposite direction, depending on (A).
To get a clearer grasp of this, you could restrict your example to two dimensions, say, $(3,2)$ for (A) and $(0,-1)$ for (B), and draw these vectors and the resulting projection on graph paper.