Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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

share|improve this question
    
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

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
Great, that makes a lot more sense. Thanks Joriki! –  David Jun 18 '11 at 0:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.