I have a generic not normalized Vector3 v1
and I want to calculate its component along another Vector3 v2
. I used to normalize the vector I want to be the direction and then multiplying the other vector's magnitude for it. I can't really understand if it would also work to multiply the other vector (not just its magnitude), but I want to know if there is a lighter way to do it, since compute the magnitude of a vector implies evaluating a square root.
In code I would say
Vector3 v1;
Vector3 v2; v2.Normalize();
Vector3 resulting_v = v1.Length() * v2;
the other case (I am not sure if can work) would be
Vector3 v1;
Vector3 v2; v2.Normalize();
Vector3 resulting_v = v1 * v2;
or, if there is a better solution, I'm listening
Answer
In order to project a vector v on u you can start by this equation,
len(v) * len(u) * cos(theta) = v . u
In order to the get the v component in the u direction. You can simply rearrange the equation by dividing on len(u), you get:
len(v) * cos(theta) = (v . u)/len(u)
Since len(v) * cos(theta)
is the v component in the u direction and is a scalar. we can simply make it a vector by multiplying we simply multiply by a normalized u direction.
len(v) * cos(theta) = (v . u)/ len(u) * (u / len(u) )
And finally
ProjVonU = ((v . u)/ len(u)) * ((u / len(u) ))
No comments:
Post a Comment