Distance and Direction

Two questions run through every frame of a game: how far? and which way? How far to the enemy, how far to the wall; which way to aim, which way to walk. Both are answered by two operations on vectors — taking a length, and stripping a vector down to a pure direction. We'll build them, then steal back the cost with one classic trick.

Length, direction, distance — line by line

Step 1 — length is Pythagoras in 3-D. The magnitude of a vector is the straight-line length of its arrow. Apply the Pythagorean theorem twice — once in the floor, once up — and the three components combine under a single root:

\lVert \vec{v}\rVert = \sqrt{v_x^2 + v_y^2 + v_z^2}.

Step 2 — normalise to a unit direction. To keep which way a vector points while throwing away how long it is, divide by its own length. The result is a unit vector: same direction, length exactly 1.

\hat{v} = \frac{\vec{v}}{\lVert \vec{v}\rVert}, \qquad \lVert\hat{v}\rVert = 1.

Step 3 — distance between two points is the length of their difference. Subtract the points (point − point = vector) to get the displacement between them, then take its length:

\operatorname{dist}(A, B) = \lVert B - A\rVert = \sqrt{(B_x - A_x)^2 + (B_y - A_y)^2 + (B_z - A_z)^2}.

Step 4 — the direction from A to B is the normalised difference. Same difference vector B - A, but now stripped to unit length — a pure heading from A toward B:

\widehat{AB} = \frac{B - A}{\lVert B - A\rVert}.

That one line is every "aim at the target", "walk toward the waypoint", "face the player" in the game: subtract, then normalise.

The square-root trick

Step 5 — for comparisons, skip the square root. A square root is expensive, and a game computes distances by the thousand — "which of these 500 enemies is nearest?", "is the pickup within reach?". But to compare or threshold distances you don't need the distance itself, only the squared distance, because squaring preserves order for non-negative numbers:

\lVert \vec{u}\rVert \le \lVert \vec{w}\rVert \iff \lVert \vec{u}\rVert^2 \le \lVert \vec{w}\rVert^2,

and \lVert\vec{v}\rVert^2 = v_x^2 + v_y^2 + v_z^2 needs no root at all. Likewise a range check \lVert B - A\rVert \le r becomes \lVert B - A\rVert^2 \le r^2 — compare a precomputed r^2 and the costly \sqrt{\ } never runs. You only take the actual root when you need the real number (a distance to display, or a normalise).

For vectors \vec{v} and points A, B in \mathbb{R}^3:

Consider the most common AI query there is: "which enemies are within attack range r?". The honest version computes a true distance for each — a subtraction, three squares, a sum, and a square root — then compares to r. The square root is by far the priciest step, and it runs once per enemy, every frame.

But the comparison \lVert B - A\rVert \le r is logically identical to \lVert B - A\rVert^2 \le r^2 (both sides non-negative, so squaring keeps the order). Precompute r^2 once, compare against the rootless (B_x-A_x)^2 + (B_y-A_y)^2 + (B_z-A_z)^2, and the \sqrt{\ } vanishes from the hot loop entirely. Same answer, a fraction of the cost — the kind of trick that lets a game ask "who's near me?" for thousands of agents a frame.