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