# Ray-Triangle Intersection: does the direction of the triangle normal matter?

Found an article here about ray triangle intersection: https://www.scratchapixel.com/lessons/3d-basic-rendering/ray-tracing-rendering-a-triangle/ray-triangle-intersection-geometric-solution

It states that D of plane Ax+By+Cz+D = 0 can be found as triangleNormal.Dot(triangleVertex). But depending on the sign of the normal, D would have a different value. Doesn't that make a difference as to which side of the origin the plane lays? Doesn't the normal of the triangle always need to face the origin for this equation to be correct?

Remember that A, B, and C come from the normal too. So negating the normal flips the sign of all four terms, making:

\begin{align} (-A) x + (- B) y + (- C) z + (- D) &= 0\\ (-1)(Ax + By + Cz + D) &= 0\\ (-1) \times (-1)(Ax + By + Cz + D) &= (-1) \times 0\\ Ax + By + Cz + D &= 0 \end{align}

So you can see, negating the normal still gives us an equivalent equation, with all the same solutions as the original. Whether we start with $$\\vec n = (A, B, C)\$$ or $$\\vec n = (-A, -B, -C)\$$ does not alter the plane equation in any way that matters.

The direction of the normal can matter for lighting, or whether you consider the triangle "front facing" for your purposes, but does not change anything about the math for detecting the intersection.

The resource you're using contains an error, though. if $$\ \vec n = (A, B, C)\$$ is the plane normal, then $$\D = - \vec n \cdot \vec p\$$ for a point $$\\vec p\$$ in the plane. The article you linked neglects that minus sign.

Another way of thinking of this: if your normal is a unit vector, then $$\D\$$ is a signed distance from the plane to the origin, along the direction of the normal. If the normal points toward the origin, this signed distance is measured in the same direction as the normal, and gives a positive value. If the normal points away from the origin, then this distance is measured in the direction opposite the normal, and gives a negative value.

Meanwhile, $$\Ax + By + Cz\$$ measures a signed distance from the origin to the plane perpendicular to $$\(A, B, C)\$$ containing the point $$\(x, y, z)\$$.

Since the "from" and "to" are flipped between these two parts, the two values will have the same magnitude but opposite sign for any $$\(x, y, z)\$$ on the plane, cancelling out to the zero on the right hand side.

(If the normal is not a unit vector, the same argument applies, except now we're dealing with a scaled distance - like we measured it with a different choice of units)

• My question is more about how to find a correct value for D. When D is calculated as triangleNormal.Dot(triangleVertex), you need to have the correct sign for the triangleNormal, otherwise you will not get D but -D But the article says just compute the normal of the triangle. But the triangle can be oriented in two ways, regardless of where the plane is =/ May 27, 2021 at 22:55
• If $\vec n = (A, B, C)$ then $D = - \vec n \cdot \vec v$ for any vertex $\vec v$ on the triangle (or any point in the plane, more generally). To see how this works, substitute $\vec v$ into the equation: $Ax + By + Cz + D = (A, B, C) \cdot \vec v - \vec n \cdot \vec v = \vec n \cdot \vec v - \vec n \cdot \vec v = 0$ May 27, 2021 at 22:59
• Yes, but D != n.dot(v). However the article just computes the normal of the triangle as N. Isn't that just wrong? May 27, 2021 at 23:02
• The article just has a typo and missed the minus sign. Nothing exciting or complicated. They might have been thinking of the related convention Ax + Bx + Cz = D, for which their formula for D is correct. Bringing D to the other side of the equation naturally changes its sign. May 27, 2021 at 23:04
• Not saying it's exciting but I'm confused. It will compute the normal as edge1.Cross(edge2). Now that isn't always (A,B,C), nor always -(A,B,C), I think. It depends on the way the triangle is faced, no? May 27, 2021 at 23:07