Ray Triangle Intersection - cmu462/Scotty3D GitHub Wiki
Given a triangle defined by points p0, p1 and p2 and a ray given by its origin o and direction d, the barycentrics of the hit point as well as the t-value of the hit can be obtained by solving the system:
Where:
This system can be solved by Cramer's Rule, yielding:
In the above, |a b c| denotes the determinant of the 3x3 with column vectors a,b, c.
Note that since the determinant is given by: , you can rewrite the above as:
Of which you should notice a few common subexpressions that, if exploited in an implementation, make computation of t, u, and v substantially more efficient.
A few final notes and thoughts:
If the denominator dot((e1 x d), e2) is zero, what does that mean about the relationship of the ray and the triangle? Can a triangle with this area be hit by a ray?
Given u and v, how do you know if the ray hits the triangle?
Don't forget that the intersection point on the ray should be within the ray's min_t
and max_t
bounds.