Why do game range checks often compare squared distances instead of distances, and when does that shortcut stop working?
answer
- both sides non-negative
- squaring keeps the order
- square the radius once
- sums and falloffs need the root
basics
~20 sDistances are non-negative and squaring keeps their order, so length(d) < r exactly when dot(d, d) < r*r, skipping a square root per check. It stops working when the distance itself is needed, as in summing distances or a linear falloff.
solid answer
~40 sFor an offset `d = b - a`, `length(d) = sqrt(dot(d, d))`, and both it and the range `r` are non-negative. On non-negative numbers squaring preserves order, so `length(d) < r` holds exactly when `dot(d, d) < r*r`. The check then costs three multiplies and two adds, with `r*r` computed once; a square root is not slow on modern hardware, but in a loop over thousands of pairs dropping it is a free win. Nearest-target sorting works the same way. The shortcut stops when the distance itself is needed: adding path legs, a falloff linear in distance, or showing a distance to the player. The usual bugs are comparing a squared distance against the unsquared radius, and integer coordinates overflowing when squared.
go deeper
Recall that a distance is the square root of the dot product of the offset with itself, and that comparing squares gives the same verdict for non-negative values.
Explain why the comparison is exact, not approximate, and where it fails: sums of distances, linear falloffs, displayed values, fixed-length steps. Show sphere overlap with the radii summed before squaring.
Point to the bugs this causes in shipped code, unsquared thresholds and integer overflow on large maps, and keep the optimisation in hot loops rather than everywhere.
Nothing here is a standing decision; at lead level, keep a shared helper that takes the plain range and squares it internally, so callers cannot pass the wrong units.
## Why the comparison is equivalent The **distance** between points `a` and `b` is the length of their offset `d = b - a`: `length(d) = sqrt(dot(d, d)) = sqrt(d.x*d.x + d.y*d.y + d.z*d.z)` The quantity under the root, `dot(d, d)`, is the **squared distance**. The argument for comparing squares instead takes three steps: 1. A distance is never negative, and a range or radius `r` is never negative either. 2. For non-negative numbers `x` and `y`, `x < y` exactly when `x*x < y*y`: squaring is **order-preserving** on that range. 3. So `length(d) < r` exactly when `dot(d, d) < r*r`, and the same holds for `<=`. Step 2 is where the non-negativity matters. For arbitrary real numbers squaring does not preserve order: `-3 < 2`, but `9 > 4`. ## A worked check A turret at the origin has range `25`, so `r*r = 625`. | player offset | squared distance | `<= 625`? | |---|---|---| | `(14, 0, 20)` | `196 + 400 = 596` | yes, in range | | `(15, 0, 20)` | `225 + 400 = 625` | yes, exactly on the boundary | | `(16, 0, 20)` | `256 + 400 = 656` | no, out of range | The middle row is exactly 25 units away (a 15-20-25 triangle), which also shows that `<` and `<=` differ only on the boundary. ## What it saves - The square root per check disappears; only multiplies and adds remain. - The squared radius is computed **once**, not per check. - On modern processors a hardware square root is a single instruction, but it has longer latency and lower throughput than a multiply. Inside a loop over every enemy against every pickup, or every particle against an attractor, avoiding it costs nothing and helps. - It is not a reason to contort code elsewhere: one square root per frame is irrelevant. ## Typical uses - **Range and trigger checks:** is the player within the turret's range, is a pickup within reach. - **Nearest-target search:** the smallest squared distance belongs to the same target as the smallest distance, because the order is preserved. - **Sphere overlap:** two spheres overlap when `dot(c2 - c1, c2 - c1) <= (r1 + r2) * (r1 + r2)`; note the radii are added *before* squaring. ## When the shortcut stops working | task | why squares fail | |---|---| | total length of a path with several legs | `sqrt(a) + sqrt(b)` is not `sqrt(a + b)`, so squared legs cannot be summed | | falloff linear in distance, such as damage dropping off with range | the falloff needs the distance itself (unless a squared falloff is chosen on purpose) | | showing a distance to the player | the number has to be in real units | | moving a fixed distance along a direction | a step of a given length needs the length to normalise | ## Mistakes - **Squared against unsquared.** `dot(d, d) < r` compares square units with plain units; with `r = 25` it acts as a range of 5. - **Squaring the sum wrongly.** For two spheres, `r1*r1 + r2*r2` is not `(r1 + r2)^2`. - **Integer overflow.** With 32-bit signed integer coordinates, a single axis difference above about 46,340 already overflows when squared, and the sum of two squares overflows at smaller differences still. Widen to a larger type before squaring. - **Optimising the wrong thing.** Replacing a square root in a function called once per frame buys nothing; the shortcut matters in hot loops.
- How do you test whether two spheres overlap without a square root?Compare the squared distance between centres with the square of the summed radii: overlap when `dot(c2 - c1, c2 - c1) <= (r1 + r2) * (r1 + r2)`. The radii must be added before squaring; summing the squared radii is a different, wrong threshold.
- Can you find the nearest of many enemies using squared distances only?Yes. Squaring preserves the order of non-negative numbers, so the enemy with the smallest squared distance is also the nearest. Take one square root at the end only if the actual distance is needed, for example to display it.
- Compares the squared distance directly against the unsquared range.
- Squaring preserves order for any numbers, so the trick works for signed values too.
- Adds squared leg lengths to get the squared length of a whole path.
- Squares integer coordinate differences without checking for overflow.
- Uses r1*r1 + r2*r2 as the threshold for two spheres overlapping.