skip to content

How do you tell whether a target is on a turret's left or right, using a 2D perp-dot product or a 3D cross product?

level: middleimportance: must knowfreq 70%

answer

  1. rotate the facing by 90 degrees
  2. a sine where the dot has a cosine
  3. component along the up axis
  4. handedness decides the sign

basics

~20 s

With x right and y up, the 2D perp-dot f.xt.y - f.yt.x of facing f and target offset t is positive on the left. In 3D, dot(cross(f, t), up) does the same, but handedness decides which sign means left.

solid answer

~50 s

In 2D, with x right and y up, `perpDot(f, t) = f.x*t.y - f.y*t.x` is the dot product of `t` with `f` rotated 90 degrees counter-clockwise, so it equals `|f| * |t| * sin(theta)`: positive when the target is on the left, negative on the right, zero when it is straight ahead or straight behind. In 3D I take `cross(f, t)` and dot it with the up axis; in a right-handed, y-up system a positive result means left, and in a left-handed system the same arithmetic means right, so I check the sign with one known example. If the turret stores a right vector, `dot(t, right)` gives the same verdict with one dot product. Combined with `dot(f, t)`, the two numbers give the quadrant, and the two-argument arctangent of them gives the signed turn angle.

code

pseudocode · 13 lines
pseudocode
// 2D: x right, y up
function side2D(facing, toTarget):
    s = facing.x * toTarget.y - facing.y * toTarget.x
    if s > 0: return LEFT
    if s < 0: return RIGHT
    return AHEAD_OR_BEHIND            // collinear: ask dot(facing, toTarget)

// 3D: right-handed, y up (in a left-handed system swap LEFT and RIGHT)
function side3D(facing, toTarget, up):
    s = dot(cross(facing, toTarget), up)
    if s > 0: return LEFT
    if s < 0: return RIGHT
    return AHEAD_OR_BEHIND

go deeper

for a junior

Recall the perp-dot formula and that it is a sine where the dot product is a cosine. Say which sign means left once you have named the axes.

for a middle

Explain why the cross product's up component picks the side, why the target's height drops out for a horizontal facing, and how dot and perp-dot together give the quadrant and the signed angle.

for a senior

Show that handedness and argument order both flip the verdict, that zero means ahead or behind, and that a tilted turret needs its own up axis. Verify a sign with a known case.

for a principal

The decision above this is a project-wide convention: one handedness, one up axis and shared side helpers, so gameplay code never re-derives a sign. Raise it when assets or code arrive from a system with different axes.

## Conventions this answer assumes Which sign means "left" depends on how the axes are laid out, so this answer fixes them. In **2D**, x points right and y points up, so a positive angle turns **counter-clockwise**. In **3D**, the axes are **right-handed** with **y up**. A later section shows what changes under the other handedness. Throughout, `f` is the turret's facing and `t = targetPos - turretPos` is the offset to the target. ## The 2D perp-dot product The **perp-dot product** is `perpDot(f, t) = f.x*t.y - f.y*t.x` It is the dot product of `t` with `(-f.y, f.x)`, which is `f` rotated 90 degrees counter-clockwise: the turret's **left** vector in this convention. It also equals `|f| * |t| * sin(theta)`, where `theta` is the signed angle from `f` to `t`, positive counter-clockwise. Reading it: - **positive**: the target is counter-clockwise of the facing, on the left; - **negative**: on the right; - **zero**: collinear, either straight ahead or straight behind; the dot product says which. Worked example: facing `f = (1, 0)`. A target offset `t = (0, 1)` gives `1*1 - 0*0 = 1`, so left. An offset `t = (3, -2)` gives `1*(-2) - 0*3 = -2`, so right. ## The 3D version: a cross product and an up axis The **cross product** `cross(f, t) = (f.y*t.z - f.z*t.y, f.z*t.x - f.x*t.z, f.x*t.y - f.y*t.x)` is perpendicular to both inputs, has length `|f| * |t| * sin(theta)` for the unsigned angle between them, and, in this right-handed convention, points along the right-hand-rule axis that turns `f` toward `t`. A turret turns about its up axis, so the useful number is the cross product's component along up: `side = dot(cross(f, t), up)` In a right-handed, y-up system, positive means left and negative means right. Worked example: a turret facing `f = (0, 0, -1)` has its right along `+x`. A target at `t = (1, 0, 0)` gives `cross(f, t) = (0, -1, 0)` and `side = -1`: right. A target at `t = (-1, 0, 0)` gives `(0, 1, 0)` and `side = +1`: left. The scalar triple product can be rearranged: `dot(cross(f, t), up) = dot(t, cross(up, f))`, and `cross(up, f)` is the turret's left vector in this convention. So a turret that already stores a **right** vector can use `dot(t, right)` instead, with the opposite sign, for one dot product. For a horizontal `f`, that left vector is horizontal too, so the target's height does not affect the verdict. ## Why handedness flips the sign The cross product formula is plain arithmetic; it does not know which way the axes point on screen. Take a **left-handed**, y-up system with x right and z forward. Facing `f = (0, 0, 1)` with a target on the right at `t = (1, 0, 0)` gives `cross(f, t) = (0, 1, 0)` and `side = +1`. Same formula, same target on the right, opposite sign. | convention | dot(cross(f, t), up) > 0 means | |---|---| | right-handed, y up | left | | left-handed, y up | right | Which axis is up only changes the vector you dot with; handedness is what changes the sign. When code moves between conventions, re-derive the sign from one known example rather than trusting a comment. ## Combining with front/behind The dot product and the side value together give the quadrant (right-handed signs): | dot(f, t) | side | target is | |---|---|---| | positive | positive | ahead and to the left | | positive | negative | ahead and to the right | | negative | positive | behind and to the left | | negative | negative | behind and to the right | Because one is `|f||t| cos(theta)` and the other `|f||t| sin(theta)` (for a horizontal facing, with `t` measured in the horizontal plane), the two-argument arctangent `atan2(side, dot(f, t))` returns the signed turn angle directly, with no normalisation: the common lengths cancel. ## Pitfalls 1. **Treating zero as "straight ahead".** A zero side value is also straight behind; check the dot product. 2. **Swapping the arguments.** `cross(t, f) = -cross(f, t)`, so the verdict reverses. 3. **Using the world's up for a tilted turret.** A turret bolted to a slope turns about its own up axis; use that. 4. **Assuming the sign is universal.** It is tied to handedness; state the convention next to the code. 5. **Normalising for a sign test.** Positive lengths never change the sign; it is wasted work and fails on zero vectors.

  • How do you get the signed angle the turret must turn, not just which side?
    Use both products: the dot gives `|f||t| cos(theta)` and the perp-dot, or the cross product's up component in 3D, gives `|f||t| sin(theta)`. The two-argument arctangent of (side, dot) returns the signed angle in one call, and the common length factor cancels, so neither vector needs normalising. The sign follows the same convention as the side test.
  • A teammate's code says positive means right; your test says positive means left. Who is wrong?
    Possibly neither: the same cross-product arithmetic gives opposite signs in right-handed and left-handed systems, and swapping the arguments flips it too. Settle it with one known case in the actual coordinate system, such as a target placed along the turret's right axis, and write the convention beside the helper so the next reader does not have to repeat the experiment.

  • Positive cross product always means left, whatever the coordinate system.
  • A zero perp-dot means the target is straight ahead of the turret.
  • The turret must normalise both vectors before the left/right sign is valid.
  • Uses the length of the cross product, which is never negative, to pick a side.
  • Uses the world up axis for a turret mounted on a sloped surface.