skip to content

How do you derive the vector a ball bounces off along when it hits an angled wall with unit surface normal n?

level: middleimportance: must knowfreq 70%

answer

  1. split into two perpendicular parts
  2. keep one part, flip the other
  3. projection onto the normal
  4. the factor of two

basics

~20 s

Split the incoming vector v into its part along the normal, dot(v, n)n, and its part along the wall. A bounce flips only the normal part: r = v - 2dot(v, n)*n, which keeps the length when n is unit length.

solid answer

~50 s

With `n` unit length, `dot(v, n) * n` is the part of `v` heading into the wall and `v - dot(v, n) * n` is the part skimming along it. A mirror bounce keeps the along-the-wall part and reverses the normal part, so `r = (v - dot(v, n)*n) - dot(v, n)*n = v - 2*dot(v, n)*n`. The two parts are perpendicular, so `|r| = |v|`, and replacing `n` by `-n` gives the same `r` because both factors flip sign. Two traps: a non-unit normal scales the subtracted term by its squared length, so either normalise it or divide by `dot(n, n)`; and the bounce should only apply while the ball is moving into the wall, `dot(v, n) < 0` with `n` facing the ball, or a ball still overlapping the wall gets reflected back into it every frame.

code

pseudocode · 11 lines
pseudocode
function reflect(v, n):                       // n must be unit length
    return v - 2 * dot(v, n) * n

function bounce(v, ballPos, wallA, wallB):    // 2D, x right, y up
    d = wallB - wallA
    n = normalize(vec2(-d.y, d.x))            // one of the wall's two normals
    if dot(ballPos - wallA, n) < 0:
        n = -n                                // face n toward the ball's side
    if dot(v, n) < 0:                         // only while moving into the wall
        v = reflect(v, n)
    return v

go deeper

for a junior

Recall the formula r = v - 2*dot(v, n)*n and that n must be unit length. Be ready to say which part of v it keeps and which part it flips.

for a middle

Derive it from projection and rejection on a whiteboard, show why length is preserved and why n and -n agree, and work one numeric example such as a ball meeting a 45-degree ramp.

for a senior

Name the production bugs: unnormalised normals, reflecting a ball already leaving so it sticks and jitters, the wrong one of two normals, and the opposite direction convention used in shading code.

for a principal

There is no open decision in the formula; the lead-level angle is keeping one shared reflect helper with a documented convention, so gameplay and shading code do not each invent a sign.

## Setting up Work in 2D with x right and y up; the formula is identical in 3D. The ball's incoming vector is `v`: its direction of travel, or its velocity, since the geometry is the same. The wall has a **unit normal** `n`, a length-1 vector perpendicular to the wall surface. For a wall segment from point `a` to point `b`, the direction is `d = b - a`; rotating it 90 degrees gives `(-d.y, d.x)`, and normalising that gives one of the wall's two normals. Which of the two you get depends on the order of `a` and `b`. ## Splitting v into two parts 1. **Projection onto the normal**: `vn = dot(v, n) * n`. This is the part of `v` heading into or out of the wall. The formula is only this simple because `n` is unit length. 2. **Rejection from the normal**: `vt = v - vn`. This is the part skimming along the wall, perpendicular to `n`. 3. By definition `v = vt + vn`, and `dot(vt, n) = 0`. ## Deriving the reflection A perfect mirror bounce keeps the along-the-wall part and reverses the into-the-wall part: `r = vt - vn = (v - vn) - vn = v - 2*vn = v - 2*dot(v, n)*n` The properties follow directly: - **Length is preserved.** `vt` and `vn` are perpendicular, so `|r|^2 = |vt|^2 + |vn|^2 = |v|^2`. - **Angle in equals angle out.** The along-the-wall part is unchanged and the normal part has the same size, so `r` makes the same angle with the wall as `v`. - **The normal's sign does not matter.** With `-n` in place of `n`, `dot(v, -n) * (-n) = dot(v, n) * n`: both sign changes cancel. - **Reflecting twice gives `v` back.** The normal part is flipped and then flipped again. ## Worked example A ball travels right with `v = (4, 0)` and hits a ramp that rises at 45 degrees. The ramp's unit normal on the ball's side is `n = (-0.7071, 0.7071)`. | quantity | value | |---|---| | `dot(v, n)` | `4 * (-0.7071) = -2.828` | | `-2 * dot(v, n)` | `5.657` | | `5.657 * n` | `(-4, 4)` | | `r = v + (-4, 4)` | `(0, 4)` | The ball leaves straight up at the same speed of 4: a 45-degree surface turns horizontal motion vertical, as the angle-in-equals-angle-out rule predicts. ## Traps that ship as bugs - **A normal that is not unit length.** If `n` has length 2, the subtracted term `2*dot(v, n)*n` is four times too large, because `n` appears twice. Normalise `n`, or use the general form `r = v - 2 * dot(v, n) / dot(n, n) * n`. - **Reflecting a ball that is already leaving.** With `n` pointing toward the ball's side, `dot(v, n) < 0` means approaching and `dot(v, n) > 0` means moving away. A ball that is still overlapping the wall after one bounce, if reflected again, is sent back into the wall, and it jitters there. Reflect only while `dot(v, n) < 0`. - **The wrong one of two normals.** The perpendicular of `b - a` points to one side of the wall. The reflection itself does not care, but the approaching test above does, so orient `n` toward the ball, for example by flipping it when `dot(ballPos - a, n) < 0`. - **Mixing the two direction conventions.** Shading usually reflects a vector `L` that points *away* from the surface toward a light; its mirror direction is `2*dot(n, L)*n - L`. That is the same formula applied to `-L` with the result negated. Using one form with the other convention produces a mirror direction that points into the surface. - **Expecting a real bounce from pure geometry.** A game bounce usually loses energy: the normal part is scaled by a restitution factor and the along-the-wall part is reduced by friction. Those factors belong to collision response; the split into normal and tangential parts is the same.

  • What do you do if the wall's normal is not guaranteed to be unit length?
    Either normalise it once when the wall is built, which is cheapest for static geometry, or use the general form `v - 2 * dot(v, n) / dot(n, n) * n`, which divides out the squared length. Leaving it unnormalised scales the subtracted term by the normal's squared length, so the bounce direction and speed are both wrong.
  • Why does a ball sometimes stick to a wall and jitter after bouncing?
    Usually because the reflection runs every frame the ball overlaps the wall, regardless of direction. The first reflection sends it away, but if it is still overlapping on the next frame, reflecting again sends it back in. Guard with `dot(v, n) < 0`, with `n` facing the ball, so only an approaching ball is reflected; pushing it out of the overlap is collision response.
  • How does the same split model a bounce that loses energy?
    Keep the decomposition into the normal part and the along-the-wall part, then scale them separately: the normal part is reversed and multiplied by a restitution factor between 0 and 1, and the along-the-wall part is reduced by friction. With both factors at 1 it collapses back to the mirror reflection. Choosing those factors is collision response; the vector split is the same.

A ball off a mirror-smooth cushion behaves like light in a mirror: the part of the motion running along the surface carries on untouched, the part heading into it is turned round at the same size, so it leaves at the angle it arrived.

  • Negates the whole incoming vector, which is only right for a head-on hit.
  • Uses v - dot(v, n)*n and calls it the reflection, dropping the factor of two.
  • Believes the normal's length does not matter because only its direction is used.
  • Thinks flipping the normal from n to -n reverses the reflected vector.
  • Reflects every frame the ball overlaps the wall, whatever its direction of travel.