Is there a reason for this?

Hey, following a tutorial i stumbled across this code:

const float thresholdUpdate = 25f;
const float sqrThresholdUpdate = viewerMoveThresholdForChunkUpdate * viewerMoveThresholdForChunkUpdate;

yada yada yada

void Update() {
        Position = new Vector2 (viewer.position.x, viewer.position.z);

        if ((PositionOld - Position).sqrMagnitude > sqrThresholdUpdate) {
            PositionOld = Position;
            UpdateStuff ();
        }
    }

Now, this all works fine and dandy, but why does he square the values?
can’t we just use both the values not squared with the same result and avoid a whole lot of math in the process?

also, regarding if statement.
if i have a statment with 2 conditions and it checking for both to be true, if the first one is false will the second will still be calculated or skipped?
and is this true also for checking for either ( con1 || con2 ), if the first one is true will the system check the other one aswell?

Because it’s faster than doing magnitude, which involves a square root. (Not that there’s much difference these days.)

–Eric

1 Like

It should be skipped (if you’re using || or &&). It stops evaluating when it’s conclusive what kind of results there will be. It’s called “short-circuiting”.

1 Like

well don’t i feel foolish.

thank you for the clarification.

great, thank you.