Squared vector divided by squared magnitude

That’s almost certainly a laughably simple math question, but I don’t have the vocabulary to Google it.

My understanding is that .normalized basically divides a vector by its magnitude, in turn obtained with a costly square root calculation. The page on .sqrMagnitude recommends avoiding the calculation altogether when possible, by using .sqrmagnitude with the square of whatever you’re comparing your magnitude with.

I was wondering if that method applies to vectors. Will I get a normalised vector by squaring it, then dividing it by its squared magnitude? Does .normalized already do that?

Hey

The basic answer to the question:

I was wondering if that method applies to vectors. Will I get a normalised vector by squaring it, then dividing it by its squared magnitude? Does .normalized already do that?

Is no, you can’t :slight_smile:

The vector, v = new Vector3 (v.x * v.x, v.y * v.y, v.z * v.z) has no strong mathematical meaning - that’s not how you multiply vectors together.

There are 2 ways to multiply vectors together:

  • The DOT PRODUCT, sometimes called an inner product, which gives a scalar (1 float) result. Vector3.Dot(a,b) = a.xb.x + a.yb.y + a.zb.z. The squared magnitude of a vector is actually got by doing the dot product of a vector with itself. i.e. a.sqrMagnitude = a.xa.x+a.ya.y+a.za.z
  • The OUTER PRODUCT gives a 3x3 matrix, and is a more complex mathematical concept which I won’t go into here, cos it’s not too useful until you want to work out tensors or eigenvalues and stuff! :slight_smile:

So:

a.sqrMagnitude = Vector3.Dot(a,a) = a.xa.x+a.ya.y+a.z*a.z

and

a.magntidue = Mathf.Sqrt(a.sqrMagnitude) = Mathf.Sqrt(a.xa.x+a.ya.y+a.z*a.z)

When people talk about avoiding the use of ‘magnitude’, they generally mean that if all you want to know is “is my vector less than / more than a given length”, then you can do it more efficiently by looking at sqrMagnitude:

float maxlen = 10.0f;
if(a.magnitude < maxlen)
{
    //something here
}

is exactly the same as

float maxlen = 10.0f;
if(a.sqrMagnitude < maxlen*maxlen)
{
    //something here
}

Hope that clarifies it a bit :slight_smile:

Vector(2, 0) → normalized = Vector(1, 0)

Vector(a, b) -> SqrMagnitude = a*a + b*b (pythagoras)
Vector(a, b) -> Magnitude = Sqrt(a*a + b*b) 

Vector(a, b) -> normalized = 1/Sqrt(a*a + b*b)  * Vector(a, b) = Vector(a / Sqrt(a*a + b*b), b / Sqrt(a*a + b*b) )

if you want to check if a vectors length is … say less then 10:

if( v.Magnitude < 10)

is the same as:

if( v.SqrMagnitude < 100)