Math - Magnitude vs Squared Magnitude

In this lecture we look at how to find the magnitude of a vector, using what we already know about the Pythagorean theorem.
We also look at how to save some resources by using the squared magnitude instead.

Which option do you use more often when programming and why?

Responding to the mini-challenge here. One of my main reasons for taking this course was my need to figure out vector math since I’ve found that to be a bit of a challenge when dealing with why I’ve been trying to do in UE4.

I looked at unreal engine docs and for distance I found these

Although, I’m not sure what the difference between Dist and Distance is. I see Dist give Euclidean distance, but haven’t all of our vectors been in Euclidean space, or is there something I’m missing?

Also, UE4 uses size instead of magnitude

@Dendrolis, I’m not familiar enough with EU4 to know if there is a difference between FVector::Distance and FVector::Dist but it looks like they do exactly the same thing. You’ll probably find the latter is just there to match the naming convention for those other distance functions in that list.

“Euclidean distance” is just a fancy way of saying the “straight line distance”, so it’s not doing anything fancy like following the curve of a sphere.

So in lumberyard, we have 5 different length fucntion. We have AZ::Vector3::GetLength, AZ::Vector3::GetLengthApprox, AZ::Vector3::GetLengthExact, and AZ::Vector3::GetLengthSq. The top three different in how accurate, and fast those calculation. Most of the time, I use AZ::Vector3::GetLengthApprox for things that I do in the OnTick events, since it seem to be faster than both AZ::Vector3::GetLength and AZ::Vector3::GetLengthSq, but if it something one off, or I need high accuracy, I’ll use AZ::Vector3::GetLengthExact. The funny thing is that those function dont return a float but they return VectorFloat.

Thanks for sharing @ravenboilinux.
I’ve not used lumberyard before but it’s interesting that they’ve got three length calculations with varying degrees of speed/accuracy.
I might have to try digging into the code to see how each of these options are implemented.

This lecture was an eye opener! I never even considered just comparing the squared distances for performance, I wondered how game engines were so performant with Vectors. Thank you @garypettie!!

3 Likes

Unreal Engine: FMath::Dist( V1, V2 ) and FMath::DistSquared( V1, V2 )

As the course I am currently taking is geared toward Unity, I have selected this as my engine to research.

Unity offers both 2D and 3D Vector functions:

  • 2D: Unity identifies 2D Vectors as Vector2.
  1. Vector2.magnitude - This only returns the length value of a previously established Vector.
  2. Vector2.sqrMagnitude - This only returns the squared length of a previously established Vector.
  3. Vector2.Distance(a,b) - Returns the distance between the two given points (a and b).
  4. There is no Vector.SqrDistance(a,b) example that I can find in Unity for 2D vectors.
  • 3D: Unity identifies 3D vectors as Vector3.
  1. Vector3.magnitude - Read-only function that returns the value of the vector.
  2. Vector3.sqrMagnitude - Read-only function that returns the square value of the vector.
  3. Vector3.Distance(a,b) - Returns the distance between two points (a and b).
  4. Again, I found no default method or function to computer Vector.SqrDistance(a,b) in Unity!

I’m using Unity and can echo what 4verageGamer said - there are almost all functions except the square distance functions. So, writing up a simple 2D vector one would look something like this:

The call:
image

The function:

The result:
image

1 Like

Hello,
I am using raylib in another course and this is how it is implemented:

// Calculate vector length
RMAPI float Vector2Length(Vector2 v)
{
float result = sqrtf((v.xv.x) + (v.yv.y));

return result;

}

// Calculate vector square length
RMAPI float Vector2LengthSqr(Vector2 v)
{
float result = (v.xv.x) + (v.yv.y);

return result;

}

// Calculate distance between two vectors
RMAPI float Vector2Distance(Vector2 v1, Vector2 v2)
{
float result = sqrtf((v1.x - v2.x)(v1.x - v2.x) + (v1.y - v2.y)(v1.y - v2.y));

return result;

}

// Calculate square distance between two vectors
RMAPI float Vector2DistanceSqr(Vector2 v1, Vector2 v2)
{
float result = ((v1.x - v2.x)(v1.x - v2.x) + (v1.y - v2.y)(v1.y - v2.y));

return result;

}

Privacy & Terms