# some advice on muller trumbore code for ray-triangle intersection.

This topic is 3833 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hello, this is the ray triangle intersection code based on the muller - trumbore paper i read-
bool intersect_triangle(vector *o, vector *d, vector *v0, vector *v1,
vector *v2, double *t, double *u, double *v)
{
vector edge1, edge2, pvec, qvec, tvec;
double det, inv_det;

vector_sub(v1, v0, &edge1);
vector_sub(v2, v0, &edge2);
vector_cross(d, &edge2, &pvec);
det = vector_dot(&edge1, &pvec);

if (det < DBL_EPSILON)
{
return (false);
}

vector_sub(o, v0, &tvec);
*u = vector_dot(&tvec, &pvec);

if (*u < 0.0 || *u > det)
{
return (false);
}

vector_cross(&tvec, &edge1, &qvec);
*v = vector_dot(d, &qvec);

if (*v < 0.0 || *u + *v > det)
{
return (false);
}

*t = vector_dot(&edge2, &qvec);
inv_det = 1.0 / det;
*t *= inv_det;

if (*t < -DBL_EPSILON)
{
return (false);
}

*u *= inv_det;
*v *= inv_det;

return (true);

}
One thing I noticed is that if the ray origin lie on the object itself, then the triangle on which the origin lies is reported as the intersected triangle and the distance to triangle is 0. I think this is undesirable in case of multiple reflections i.e. when ray gets reflected of many surfaces on the object. In that case, *t < - DBL_EPSILON statement is the culprit and I'm not sure why they had this condition in the code. Any particular reasons ? If I change it to *t <= 0.0, the code is alright.

##### Share on other sites
The DBL_EPSILON test is used (here, and plenty of other places)...
to catch problems with / from Floating Point precision.
Often, due to precision loss, complex calculations which should evaluate to 0.000000....
actually result in a "small" postive or negative answer -0.000000000000000001 say.

The DBL_EPSILON value should normally be used to 'account' for this problem and it value should be set something "appropriate".

In your case.. I would recommend...

Leave the DBL_EPSILON test in, but also store a reference to the last face hit / the "source face" for the new ray.

That way, you will detect the "source face" as a hit....
but you can ignore that hit, because you know a ray starting from "Face X" can't hit "Face X" again without hitting something else first...

and then you don't risk missing "real" hits, by not accounting for the FP Precision problem.

HTH

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 9
• 9
• 34
• 16
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015656
×