I am using the Möller-Trumbore method as part of my (still very basic) collision detection system.
The information I'm craving about is the distance from the ray origin and intersection point. The intersection point would be fine too obviously.
I probably wrongly assumed t
in the code below is actually the distance from the ray origin to the intersection point. If it actually is, then I don't know what's wrong with my code.
I have got a simulation with a mesh and "bullets", sometimes firing a collision acurately, and sometimes not (bullets passing thru mesh, or firing a colision before it actually should hit).
bool rayTriangleIntersect(const XMFLOAT3 &orig, const XMFLOAT3 &dir, const XMFLOAT3 &v0, const XMFLOAT3 &v1, const XMFLOAT3 &v2, float &t, float &u, float &v) {
// http://www.scratchapixel.com/lessons/3d-basic-rendering/ray-tracing-rendering-a-triangle/moller-trumbore-ray-triangle-intersection
float kEpsilon = 0.000001f;
XMFLOAT3 v0v1 = v1 - v0;
XMFLOAT3 v0v2 = v2 - v0;
XMFLOAT3 pvec = XMFLOAT3Cross(dir, v0v2);
float det = XMFLOAT3Dot(v0v1, pvec);
if (det < kEpsilon) return false;
float invDet = 1.0f / det;
XMFLOAT3 tvec = orig - v0;
u = XMFLOAT3Dot(tvec, pvec) * invDet;
if (u < 0 || u > 1) return false;
XMFLOAT3 qvec = XMFLOAT3Cross(tvec, v0v1);
v = XMFLOAT3Dot(dir, qvec) * invDet;
if (v < 0 || u + v > 1) return false;
t = XMFLOAT3Dot(v0v2, qvec) * invDet;
return true;
}
Is t
really the distance im looking for ? If not how do I get it, or the intersection point. I've heard of baricentric coordinates in a triangle related to u
and v
- is it the way to go ? If yes I'd be thankful for an example or link related to the Möller-Trumbore method.
Thanks!