How does the sign of a dot product tell a sentry turret whether the player is in front of it or behind it?
answer
- lengths are never negative
- sign follows the cosine
- offset from turret to player
- compare against cos(halfAngle) for a cone
basics
~20 sDot the turret's world-space forward vector with the offset from turret to player: positive means in front, negative behind, zero exactly beside, because the dot product's sign is the sign of the cosine of the angle between them.
solid answer
~40 sThe dot product `dot(a, b) = a.x*b.x + a.y*b.y + a.z*b.z` also equals `|a| * |b| * cos(theta)`. Lengths are never negative, so for non-zero vectors the sign comes from `cos(theta)` alone: positive under 90 degrees, zero at exactly 90, negative beyond. For the turret I take `toPlayer = playerPos - turretPos` and the forward the turret currently faces in world space, and test `dot(forward, toPlayer) > 0`; the sign test needs no normalisation. For a vision cone instead of a half-space, I keep `forward` unit length, normalise `toPlayer` and compare the result with `cos(halfAngle)`, computed once. The classic bugs are subtracting the wrong way round, which swaps front and behind, and using the model's authored forward axis instead of its current world-space facing.
code
pseudocode · 9 linesfunction isInFront(turretPos, forward, playerPos):
toPlayer = playerPos - turretPos // turret -> player, not the reverse
return dot(forward, toPlayer) > 0 // sign only: no normalising needed
function isInCone(turretPos, forward, playerPos, cosHalf): // forward is unit length
toPlayer = playerPos - turretPos
lenSq = dot(toPlayer, toPlayer)
if lenSq == 0: return true // player on the turret's pivot
return dot(forward, toPlayer) >= cosHalf * sqrt(lenSq)go deeper
Recall the two forms of the dot product and that only the cosine can be negative. Say which way the offset is subtracted and why it matters.
Explain why normalising is unnecessary for the sign but required for a cone, and how to turn a cone width into a cosine threshold computed once rather than an angle every frame.
Show the bugs you have seen: swapped subtraction, authored versus world-space forward, arccosine of a rounded value. Point out that front is a half-space, not visibility, and that sight lines are a separate check.
Little to decide here; the lead-level angle is consistency, such as one shared helper for facing and cone tests, so every gameplay system agrees on the subtraction order and on whether thresholds are cosines or angles.
## What the dot product computes For two 3D vectors `a` and `b`, the **dot product** is `dot(a, b) = a.x*b.x + a.y*b.y + a.z*b.z` (drop the `z` term in 2D). It has a second, geometric form: `dot(a, b) = |a| * |b| * cos(theta)`, where `|a|` is the **length** of `a` and `theta` is the angle between the two vectors. Both forms give the same number, which is why three multiplies and two adds can answer an angle question without any trigonometry. ## Reading the sign A length is never negative, so whenever both vectors are non-zero the sign of the dot product is the sign of `cos(theta)`: | dot(forward, toPlayer) | angle between them | meaning for the turret | |---|---|---| | positive | less than 90 degrees | the player is in front | | zero | exactly 90 degrees | the player is exactly beside it | | negative | more than 90 degrees | the player is behind | The dividing surface is the plane through the turret perpendicular to its forward vector. Everything on the forward side of that plane gives a positive result, however far off to the side it is: the test answers "in front", not "in view". ## The front/behind test step by step 1. Compute the **offset** from the turret to the player: `toPlayer = playerPos - turretPos`. The order matters; subtracting the other way round flips the sign of every answer. 2. Take the direction the turret **currently** faces in world space. If the turret has rotated, that is its rotated forward axis, not the forward axis of the model as it was authored. 3. Evaluate `dot(forward, toPlayer)` and compare it with zero. Worked example: the turret stands at `(2, 0, 5)` facing `(0, 0, 1)`. A player at `(4, 0, 2)` gives `toPlayer = (2, 0, -3)` and a dot product of `-3`, so the player is behind. A player at `(1, 0, 9)` gives `toPlayer = (-1, 0, 4)` and a dot product of `4`, so the player is in front. Neither vector needs normalising for this test: scaling a vector by a positive length cannot change the sign of the result. ## From a half-space to a vision cone "In front" is a half-space; a turret usually sees only a cone. When both vectors are **unit length**, the dot product *is* `cos(theta)`, so a cone test is one comparison: - keep `forward` unit length and normalise `toPlayer`; - precompute `cosHalf = cos(halfAngle)` once, where `halfAngle` is half the cone's full width; - the player is inside when `dot(forward, normalize(toPlayer)) >= cosHalf`. For a 60-degree cone, `halfAngle` is 30 degrees and `cosHalf` is about `0.866`. The player at `(1, 0, 9)` has `toPlayer = (-1, 0, 4)` with length `sqrt(17)`, about `4.12`; its normalised z component is about `0.970`, above `0.866`, so the player is inside. When the square root matters, the same test for half-angles below 90 degrees can be written without it as `dot(f, t) > 0` and `dot(f, t)^2 >= cosHalf^2 * dot(t, t)`; here `16 >= 0.75 * 17 = 12.75`, which agrees. ## Mistakes that flip or break the test - **Reversed subtraction.** `turretPos - playerPos` points from the player to the turret, so front and behind swap. - **Dotting a position instead of an offset.** `dot(forward, playerPos)` measures the player against the world origin, not against the turret, and the verdict changes whenever the turret moves. - **Authored forward instead of current forward.** A turret that has swung round 180 degrees keeps the same model-space forward axis; only the rotated one says where it looks. - **Comparing against degrees.** A dot product of unit vectors lies between -1 and 1. Testing it against `30`, or against `0.5` when a 30-degree half-angle was meant (0.5 is the cosine of 60 degrees), accepts the wrong cone. - **Taking `acos` of a rounded result.** Two unit vectors can produce `1.0000001` after rounding, and the arccosine of a value above 1 is NaN. Clamp to `[-1, 1]` before recovering an angle, or compare cosines and never compute the angle at all. ## Why interviewers open with it The test is cheap, works the same in 2D and 3D, and needs no angles. Gameplay code is full of it: is a pickup ahead of the player, does a surface face a light, is a ball approaching a wall or leaving it. A candidate who reaches for `acos` and degrees first is solving a harder problem than the one asked, and one who can say why the length drops out of the sign has shown the geometric reading the question is really probing.
- How do you turn the front/behind test into a vision cone of a given width?Keep the forward vector unit length, normalise the offset to the player, and compare their dot product with the cosine of half the cone's width, computed once. Because cosine falls as the angle grows, a larger dot product means a smaller angle, so the test is `dot >= cos(halfAngle)`. A 90-degree cone uses about 0.707, a 60-degree cone about 0.866.
- When both vectors are unit length, what does the dot product's magnitude tell you, and what is the trap in recovering the angle?It is the cosine of the angle between them: 1 for the same direction, 0 for perpendicular, -1 for opposite. Recovering the angle with an arccosine is where it breaks: rounding can give 1.0000001, and the arccosine of that is NaN. Clamp to [-1, 1] first, or better, compare cosines directly and skip the angle.
- Why does the test still say in front for a player standing far off to the turret's side?The sign test splits space with a plane through the turret, perpendicular to its forward vector. Any point on the forward side of that plane gives a positive dot product, even at 89 degrees off-axis. To mean in view rather than merely in front, the test must become a cone, and seeing through walls is a separate line-of-sight check.
- Both vectors must be normalised before the dot product's sign means anything.
- Subtracts turret position minus player position and still reads positive as in front.
- Dots the player's world position, not the offset from the turret, with the forward vector.
- A negative dot product means the player is exactly opposite the turret's facing.
- Compares the dot product of unit vectors directly against a cone angle in degrees.
- Uses the model's authored forward axis after the turret has rotated.