1
\$\begingroup\$

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

\$\endgroup\$
0

2 Answers 2

4
\$\begingroup\$

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) ))

\$\endgroup\$
4
  • \$\begingroup\$ in shorter terms : (v.û)û \$\endgroup\$
    – Sidar
    Commented Jul 17, 2017 at 0:33
  • \$\begingroup\$ what does the hat on top of the u signify? \$\endgroup\$
    – BenKoshy
    Commented Mar 8, 2018 at 4:48
  • 1
    \$\begingroup\$ @BKSpurgeon hat usually indicate a unit vector ( a vector of length 1) this is the same as saying u/len(u). But you will see different usage in different textbooks \$\endgroup\$
    – concept3d
    Commented Mar 8, 2018 at 9:07
  • 1
    \$\begingroup\$ @BKSpurgeon using normalized vectors mean that you care about the direction not the length of the vector. \$\endgroup\$
    – concept3d
    Commented Mar 8, 2018 at 9:10
0
\$\begingroup\$

There is something wrong here.

Computing the component of V1 along V2 means that you want to know what portion of the lenght of V1 is projected to V2 in terms of the V2 length.

Lets say that V2 is 1Km long vector pointing North, your V1 vector is a 200m vector pointing to North/West; a point moved by V1 travels 100m to North and 100m to West that are both 0.1km. In this scenario the component of V1 along V2 is 0.1 (adimensional scalar).

If you multiply 0.1 by V2 then you get a 100m vector pointing to North (because 100m is 0.1 * 1Km). You can normalize your V2 so it becomes a 1m long vector pointing North; in this case the inner product will give you 100 so if you multiply by V2 normalized, you get the same result.

What you do in snipped 1 is wrong because you will get a 200m vector long pointing North, in other words you do not take into account the direction of V1.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .