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

1 Like