Ray-Triangle Intersection: Geometric Solution
Reading time: 16 mins.
Ray-triangle Intersection: Geometric Solution

Figure 1: The intersection of a ray and a triangle. The triangle lies in a plane. The value is the distance from the ray origin to the intersection point.
In the previous paragraphs, we learned how to calculate a plane's normal. Next, we need to determine the position of point (in some illustrations, we also used "Phit"), the point where the ray intersects the plane.
Step 1: Finding P
We know that is somewhere on the ray defined by its origin and its direction . We used in the previous lesson, but we will use in this lesson to avoid confusion with the term from the plane equation. The parametric equation of the ray is (equation 1):
Where is the distance from the ray origin to . To find , we must find (refer to Figure 1). What else do we know? We have already computed the plane's normal and the plane equation (2), which is (refer to the chapter on ray-plane intersection from the previous lesson for more details):
Where , , and are the components (or coordinates) of the normal to the plane (), and is the distance from the origin to the plane. If this is still unclear, imagine a sheet of paper passing through the origin, which you rotate around the origin. Once you are satisfied with the plane’s orientation, move it some distance away from the origin in the direction perpendicular to your sheet of paper. The variables , , and represent the coordinates of any point on this plane.
Knowing the plane's normal and that the triangle's vertices , , and lie in the plane, it is possible to compute . Let's choose for this purpose (you can choose or as well):
float D = -dotProduct(N, v0);
// Or, if you want to compute the dot product directly:
float D = -(N.x * v0.x + N.y * v0.y + N.z * v0.z);We also know that point , the intersection point of the ray and the plane, lies within the plane. Consequently, we can substitute (equation 2) for in equation 1 and solve for (equation 3):
float t = -(dot(N, orig) + D) / dot(N, dir);With computed, we can now calculate the position of :
Vec3f Phit = orig + t * dir;Wow! Cool, that was kind of easy, but remember that this only gives us the point where the ray intersects the plane in which the triangle lies. The intersection point itself could still be outside the triangle's boundary. So, we are not done yet. We still need to test whether the intersection point is inside the triangle (or lies on one of the triangle's edges or vertices). But before we get to that, there are two important cases we need to consider first.
The Ray And The Triangle Are Parallel
If the ray and the plane are parallel, they will not intersect (refer to Figure 2). For robustness, we must handle this case should it occur. This situation is straightforward to identify: if the triangle and the ray are parallel, then the triangle's normal and the ray's direction are perpendicular.

Figure 2: Several situations can occur. The ray can intersect the triangle, miss it, or be parallel to it. No intersection is possible when the ray is parallel to the triangle. This situation occurs when the normal of the triangle and the ray direction are perpendicular (and the dot product of these two vectors is 0).
We know that the dot product of two perpendicular vectors is 0. Referring to the denominator of equation 3 (the term below the line), we compute the dot product between the triangle's normal and the ray direction . Our code must be robust to prevent a potential division by 0. When this term equals 0, the ray is parallel to the triangle, indicating no intersection. Hence, before calculating , we first evaluate ; if the result is 0, the function will return false, indicating no intersection.
The Triangle is "Behind" the Ray

Figure 3: If a triangle is "behind" the ray, it shouldn't be considered for an intersection. Whenever the value of computed with equation 3 is less than 0, the intersection point lies behind the ray's origin and should be discarded. In that case, there is no intersection.
Until now, we have assumed the triangle is always in front of the ray hitting the triangle. But what if the ray is located "behind" the triangle (having passed the triangle) while maintaining its direction? By the way, don’t confuse this with the case where the ray hits the triangle from the back. So, we have two cases here:
- Case 1: The ray can hit the triangle from either the front or the back. In this case, the ray is either in front of or behind the triangle, but the ray's direction is always aiming at the target.
- Case 2: The second case is like an archer standing either in front of or behind the target but always shooting the arrow forward. In the virtual world, an archer standing behind the target and shooting an arrow forward could still hit the target. How cool is that?

We’re concerned about case 2 here. When the archer is behind the target, shooting the arrow forward, the arrow typically shouldn't hit the target—it should land somewhere in front of the archer and be lost forever. Similarly, in our case, the triangle should not be visible if the ray originates behind it and is traveling away from it. However, Equation 3 can still yield a valid result in this scenario. Fortunately, this situation is easy to detect: when the ray starts behind the triangle, the value of is negative, meaning the hit point lies in the opposite direction of the ray’s travel (the hit point would be "behind" the archer). Failing to account for this could mistakenly include the triangle in the final image, which is undesirable.
Again, don’t confuse this with the case where the ray hits the triangle from the back—that’s a perfectly valid scenario, like seeing someone whether you’re looking at them from the front or the back. But if you’ve turned your back to someone, you wouldn’t expect to see them, right?
Conclusion: We must check the sign of before confirming the intersection as valid. If is less than 0, the triangle is behind the ray's origin relative to its direction, meaning it is not visible (the "turned your back" scenario), and we should return false for no intersection. On the other hand, if is greater than 0 (you're looking at the person, whether it's their face or back), the triangle is "visible" to the ray, and we can proceed to the next step.
Step 2: Is Inside or Outside the Triangle?
Now that we have identified the point , where the ray intersects the triangle's plane, we need to determine whether is inside the triangle (indicating that the ray intersects the triangle) or outside it (indicating that the ray misses the triangle). Figure 2 illustrates these possibilities.

Figure 4: and point in opposite directions.
The solution to this problem is straightforward and is called the inside-outside test. We have already encountered this term in the lesson on rasterization, where the test was applied to 2D triangles. Here, we adapt it for 3D triangles. Let's define the vectors and as shown in Figure 4. Calculating the cross product of these two vectors yields a result that points in the same direction as the z-axis and the triangle's normal (we could also use and or and for calculating the normal, but that's a detail).
Now, let's mirror across the x-axis, calling this mirrored point , with coordinates . The cross-product would result in .
- Since and point in the same direction, their dot product is positive.
- Conversely, since and point in opposite directions, their dot product is negative.
You may have noticed that when we follow the vertices in the order , we rotate clockwise, whereas in the order , we rotate counterclockwise. As you might recall from the lesson on geometry, the normal direction is defined by this vertex order. If the normal is calculated by going around the triangle vertices in one particular order, you will get if you calculate the normal by going around in the opposite direction. Keep this in mind.
This test reveals that if a point , which lies in the plane of the triangle (such as vertex or the intersection point), is on the left side of vector , then the dot product between the triangle's normal and the cross product is positive.
The key point here is understanding the role of the point in this method. In our previous example, we used to calculate the vector . However, let's now imagine that represents some arbitrary point in the plane, not necessarily vertex . The logic remains the same. We calculate the vector , and if the dot product between the triangle's normal and is positive, we know that is on the left side of the vector .
Conversely, if the dot product between the triangle's normal and is negative (as in the case where we use a point , like the mirrored vertex ), then is on the right side of .

Figure 5: When is on the left side of , the dot product is positive. When is on the right side (), is negative.
These two cases are illustrated in Figure 5. If point is inside the triangle, then the dot product between the triangle's normal and the vector will be positive, indicating that is on the left side of the imaginary line defined by , which effectively divides the triangle's plane. If point is outside the triangle, specifically in the half-plane to the right of the line defined by extending the vector , then the dot product between the normal of the triangle and the vector obtained from calculating the cross product between the vector and will be negative.
To apply the technique described for the ray-triangle intersection problem, we perform the left/right test for each triangle edge. If point is on the left side of each of the triangle's edges, where each edge is calculated as , then is inside the triangle (where is the vertex index, and for triangles, this number varies from 0 to 2). If the test fails for any edge, lies outside the triangle's boundaries. Figure 6 illustrates this process.
Another way of looking at it is to consider that each edge of the triangle, , cuts the plane on which the triangle lies into two: what's on the left of that edge and what's on its right. "Left" and "right" are defined with respect to the direction of the vector . To determine whether is on the left or right of that edge, follow these steps:
- Calculate the vector from to : .
- Calculate the vector .
- Compute the cross product between these two vectors. Let's call it .
- Calculate the dot product between , the triangle's normal, and . The sign of this dot product tells us whether is to the left or right of that edge .
- Repeat this for every edge of the triangle (this method also works for polygons with more than three edges, as long as the polygon is convex).

Figure 6: To determine if is inside the triangle, we can test if the dot product of the vector along the edge and the vector defined by the first vertex of the tested edge and is positive (meaning is on the left side of the edge). If is on the left of all three edges, then is inside the triangle.
Here is an example of the inside-outside test in pseudocode:
Vec3f v0v1 = v1 - v0;
Vec3f v1v2 = v2 - v1;
Vec3f v0v2 = v0 - v2;
Vec3f v0p = P - v0;
Vec3f v1p = P - v1;
Vec3f v2p = P - v2;
if (dotProduct(N, crossProduct(v0v1, v0p)) > 0 &&
dotProduct(N, crossProduct(v1v2, v1p)) > 0 &&
dotProduct(N, crossProduct(v0v2, v2p)) > 0) return true; // P is inside the triangleNote the similarities between this method and the method used in the rasterization lesson to determine if a pixel overlaps a (2D) triangle.
Let's write the complete ray-triangle intersection test routine code. First, we'll compute the triangle's normal, then test if the ray and the triangle are parallel. If they are parallel, the intersection test fails. If not, we compute , from which we can determine the intersection point . If the inside-out test is successful (testing if is on the left side of each of the triangle's edges), then the ray intersects the triangle, and is within the triangle's boundaries, making the test successful.
bool rayTriangleIntersect(
const Vec3f &orig, const Vec3f &dir,
const Vec3f &v0, const Vec3f &v1, const Vec3f &v2,
float &t)
{
// Compute the plane's normal
Vec3f v0v1 = v1 - v0;
Vec3f v0v2 = v2 - v0;
// No need to normalize
Vec3f N = v0v1.crossProduct(v0v2); // N
// Step 1: Finding P
// Check if the ray and plane are parallel
float NdotRayDirection = N.dotProduct(dir);
if (fabs(NdotRayDirection) < kEpsilon) // Almost 0
return false; // They are parallel, so they don't intersect!
// Compute d parameter using equation 2
float d = -N.dotProduct(v0);
// Compute t (equation 3)
t = -(N.dotProduct(orig) + d) / NdotRayDirection;
// Check if the triangle is behind the ray
if (t < 0) return false; // The triangle is behind
// Compute the intersection point using equation 1
Vec3f P = orig + t * dir;
// Step 2: Inside-Outside Test
Vec3f Ne; // Vector perpendicular to triangle's plane
// Test sidedness of P w.r.t. edge v0v1
Vec3f v0p = P - v0;
Ne = v0v1.crossProduct(v0p);
if (N.dotProduct(Ne) < 0) return false; // P is on the right side
// Test sidedness of P w.r.t. edge v2v1
Vec3f v2v1 = v2 - v1;
Vec3f v1p = P - v1;
Ne = v2v1.crossProduct(v1p);
if (N.dotProduct(Ne) < 0) return false; // P is on the right side
// Test sidedness of P w.r.t. edge v2v0
Vec3f v2v0 = v0 - v2;
Vec3f v2p = P - v2;
Ne = v2v0.crossProduct(v2p);
if (N.dotProduct(Ne) < 0) return false; // P is on the right side
return true; // The ray hits the triangle
}This "inside-outside" technique works for any convex polygon. Repeat the method used for triangles for each edge of the polygon. Compute the cross-product of the vector defined by the two edges' vertices and the vector defined by the first edge's vertex and the point. Then, compute the dot product of the resulting vector and the polygon's normal. The sign of the resulting dot product determines if the point is on the right or left side of that edge. Iterate through each edge of the polygon. There's no need to test the other edges if one fails the test.
Note, this technique can be optimized if the triangle's normal and the value from the plane equation are precomputed and stored for each triangle in the scene.
What's Next?
In this chapter, we introduced a technique for computing the ray-triangle intersection test using simple geometry. However, there's more to the ray-triangle intersection test that we haven't covered yet, such as determining whether the ray hits the triangle from the front or the back. We can also compute the barycentric coordinates of the intersection point. These coordinates are essential for tasks such as applying a texture to the triangle.