I use distance squared checks for basically all my distance (vector3 length) checking, due to the performance increase from not incurring a square root (like in plain length checks).
From the looks of it, squared distance checks work fine in every situation:
if x^2 < y^2, then x < y, even when 0 < (x or y) < 1
I am not considering situations where x or y is less than 0, as distance and distance-squared is always going to be positive.
Since this works, it looks like distance checks are never ever needed, but I have a nagging feeling that i'm missing something. Will this still hold up in accuracy-critical situations?
Answer
There's no disadvantage I'm aware of when using squared length to compare distances. Think about it like that: You're just skipping the sqrt
which doesn't give you any additional accuracy. If you don't need the actual Euclidean distance, then you can safely leave the sqrt
out.
Of course the squared length scales quite differently than the Euclidean distance and is therefore a bad candidate for things like pathfinding heuristics.
No comments:
Post a Comment