Trying to rewrite AIPath and got a simple question regarding XZsqrMagnitude

Part i question:
protected float XZSqrMagnitude(Vector3 a, Vector3 b) { float dx = b.x - a.x; float dz = b.z - a.z; return dx * dx + dz * dz; }

So i get that if you would want the sqrt then you would probably have named it something else, but for the distance between the two points, you need the squareroot of what you return, right? everywhere talking about magnitude says that, and past tells me that as well - so as i’m trying to understand the stuff, i’d really like an explaination! :smiley:

Awesome work though - i’m really learning alot by reading your classes!

Hi

This is not XZSqrtMagnitude it is XZSqrMagnitude (i.e squared).
Yes, you need to take the square root of it to get the actual distance however for comparing distances, the squared version works fine.

If I want to check if the distance to some object (say x meters away) is less than 5 I would do
if (x*x < 5*5)
Since distances are always positive, this is valid.
Calculating the squared distance to something is faster (just remove the square root).

See: http://gamedev.stackexchange.com/questions/23709/are-there-any-disadvantages-of-using-distance-squared-checks-rather-than-distanc

Okay - i saw that you used the (xx < 55) logic, few seconds as go as well. wanted to ask why, but you answered that rather fast as well! So thank you - i really think it’s awesome that you answer so quickly! :slight_smile: