skip to content

How does the Moller-Trumbore algorithm intersect a ray with a triangle, and what are the barycentric coordinates it returns used for?

level: middleimportance: should knowfreq 50%

answer

  1. solve for t, u and v together
  2. edges from one vertex
  3. Cramer's rule with cross products
  4. u, v and their sum bounded

basics

~20 s

Moller-Trumbore solves o + td = (1-u-v)v0 + uv1 + vv2 for t, u and v by Cramer's rule, exiting early once u, v or u + v leave [0, 1]. The barycentrics interpolate vertex data such as UVs and normals at the hit.

solid answer

~50 s

With edges `e1 = v1 - v0` and `e2 = v2 - v0`, a hit satisfies `o + t*d = v0 + u*e1 + v*e2`, a 3x3 linear system. I compute `p = cross(d, e2)` and `det = dot(e1, p)`; a near-zero `det` means the ray is parallel to the triangle. Then `s = o - v0`, `u = dot(s, p) / det`, and I reject if `u` is outside [0, 1]; `q = cross(s, e1)`, `v = dot(d, q) / det`, rejecting if `v < 0` or `u + v > 1`; finally `t = dot(e2, q) / det`, accepted if positive and within range. No plane equation is stored, and each early-out skips later work. The barycentric weights `(1 - u - v, u, v)` interpolate vertex attributes at the hit: texture coordinates to place a decal, the smoothed normal for an impact effect, or a per-vertex value marking a hit zone.

code

pseudocode · 20 lines
pseudocode
function rayTriangle(o, d, v0, v1, v2, tMax):
    e1 = v1 - v0
    e2 = v2 - v0
    p = cross(d, e2)
    det = dot(e1, p)
    if abs(det) < EPS:
        return MISS                 // parallel to the plane
    invDet = 1 / det
    s = o - v0
    u = dot(s, p) * invDet
    if u < 0 or u > 1:
        return MISS
    q = cross(s, e1)
    v = dot(d, q) * invDet
    if v < 0 or u + v > 1:
        return MISS
    t = dot(e2, q) * invDet
    if t < EPS or t > tMax:
        return MISS
    return HIT(t, u, v)              // weights (1 - u - v, u, v)

go deeper

for a junior

Recall what barycentric coordinates are: three non-negative weights summing to one that locate a point inside a triangle.

for a middle

Walk through the algorithm's steps, the early-outs on u, v and u + v, and how the barycentrics interpolate texture coordinates and normals at the hit.

for a senior

Explain the determinant's sign for one-sided tests, tolerance choices, and why rays through shared edges can slip between triangles, with the watertight alternative.

for a principal

Weigh where exact triangle tests are worth their cost against cheaper proxy shapes, given how many rays per frame the game fires and what precision gameplay needs.

## The equation being solved A **triangle** with vertices `v0`, `v1`, `v2` contains exactly the points `(1 - u - v)*v0 + u*v1 + v*v2` with `u >= 0`, `v >= 0` and `u + v <= 1`. The three weights `(w, u, v)` with `w = 1 - u - v` are the point's **barycentric coordinates**: non-negative weights that sum to one and say how much each vertex contributes. A **ray** is `o + t*d`. The ray hits the triangle where the two descriptions agree. Rewriting with the edges `e1 = v1 - v0` and `e2 = v2 - v0` gives: `o + t*d = v0 + u*e1 + v*e2`, or `-t*d + u*e1 + v*e2 = o - v0` That is three equations (x, y, z) in three unknowns `t`, `u`, `v`. Moller-Trumbore solves it directly, without first building the triangle's plane. ## Solving with Cramer's rule Cramer's rule writes each unknown as a ratio of determinants, and a 3x3 determinant of three column vectors is a **scalar triple product**, `dot(a, cross(b, c))`. Rearranging those triple products lets several cross products be shared: 1. Compute `e1 = v1 - v0` and `e2 = v2 - v0`. 2. Compute `p = cross(d, e2)` and `det = dot(e1, p)`. If `|det|` is below a small tolerance, the ray is parallel to the triangle's plane: report a miss. 3. Compute `invDet = 1 / det` and `s = o - v0`. 4. Compute `u = dot(s, p) * invDet`. If `u < 0` or `u > 1`, miss. 5. Compute `q = cross(s, e1)` and `v = dot(d, q) * invDet`. If `v < 0` or `u + v > 1`, miss. 6. Compute `t = dot(e2, q) * invDet`. Accept the hit if `t` is above a small tolerance and within the ray's maximum length. Each rejection happens as soon as its value is known, which matters because a ray is usually tested against far more triangles it misses than ones it hits. ## What the determinant's sign says `det = dot(e1, cross(d, e2))` equals `-dot(d, cross(e1, e2))`. The vector `cross(e1, e2)` is the triangle's geometric normal; in right-handed coordinates it points toward a viewer who sees the vertices counter-clockwise. So: - `det > 0`: the ray travels against the normal and hits the front face. - `det < 0`: the ray hits the back face. - `det` near zero: the ray is parallel to the plane. A **two-sided** test rejects only near-zero `|det|`. A **one-sided** test, matching back-face culling, rejects every `det` below the tolerance, which also skips the division for back faces. ## What the barycentric coordinates are for The test returns `u` and `v` for free, and they turn a hit into useful data: - **Interpolating vertex attributes:** any per-vertex value `A` at the hit is `w*A0 + u*A1 + v*A2`. Texture coordinates locate where a blood or bullet-hole decal lands on the character's texture. - **Smoothed normals:** interpolating the vertex normals gives the shading normal at the hit, which looks better for an impact effect than the flat face normal. - **Hit zones:** interpolating a per-vertex value, or taking the vertex with the largest weight, maps the hit to a body region or a material. - **The hit position:** `o + t*d` or `w*v0 + u*v1 + v*v2`; they agree up to rounding. Barycentric coordinates are weights, not texture coordinates; confusing the two is a common slip. ## Practical notes | Concern | What to do | |---|---| | Tolerances | Use a small tolerance on `det` and on `t`, scaled to the scene's units, so a ray leaving a surface does not hit the triangle it started on | | Shared edges | Rounding can let a ray through the exact edge between two triangles miss both; watertight ray-triangle algorithms exist for when that matters | | Many triangles | Test bounding volumes first; a ray against every triangle of a large mesh is far too slow | | Degenerate triangles | Zero-area triangles give `det` near zero and are rejected as parallel | The acceleration structures that decide which triangles to test belong with ray tracing and culling; this algorithm is the test at the bottom of all of them.

  • How would you make the test ignore back faces?
    Replace the `abs(det) < EPS` check with `det < EPS`. With `det = -dot(d, cross(e1, e2))`, a ray hitting the front face has a positive determinant, so this rejects back faces and near-parallel rays in one comparison, before any division.
  • Why can a hitscan ray slip between two adjacent triangles?
    A ray passing exactly through a shared edge produces `u`, `v` or `u + v` right at a boundary. Rounding can push the value just outside the range for both triangles, so both reject it. Small inclusive tolerances hide most cases; watertight ray-triangle formulations guarantee one of the two triangles reports the hit.
  • How do you get the texture coordinate at the hit point?
    Weight the three vertices' texture coordinates by the barycentrics: `uv = (1 - u - v)*uv0 + u*uv1 + v*uv2`. The same weighting works for any per-vertex attribute, such as colours, normals or bone weights.

  • The returned u and v are the hit point's texture coordinates.
  • A negative t means the ray hit the triangle's back face.
  • Moller-Trumbore needs the triangle's plane equation stored in advance.
  • If det is negative the ray has missed the triangle.
  • u and v each within [0, 1] is enough to confirm a hit.