HELP! With Moller-Trumbore ray/triangle intersection code, please!

Started by
1 comment, last by playmesumch00ns 20 years ago
I''m having trouble with the Moller-Trumbore ray/triangle alogirithm. It''s not giving me correct results, so I thought I''d try it out in a simple demo with a single triangle, ( (-1, -1, 2 ), (1, -1, 2), (0, 1, 2) ) and a ray fired from the origin (0, 0, 0), up the z-axis (0, 0, 1). Well the results I''m getting tell me that the ray is parallel to the plane of the triangle (i.e. the determinant is 0). Can someone please tell me what the stupid mistake I''m making is! V3f v0( -1, -1, 2 ); V3f v1( 1, -1, 2 ); V3f v2( 0, 1, 2 ); V3f O( 0, 0, 0 ); V3f D( 0, 0, 1 ); V3f e1 = v1 - v0; V3f e2 = v2 - v0; V3f P = D * e2; float det = e1 ^ P; float t, u, v; if ( det < 0.0000001 && det > -0.0000001 ) { cout << "Ray is in plane of polygon" << endl; return -1; } float invdet = 1/det; V3f T = ( O - v0 ); u = (T^P) * invdet; if ( u < 0.0 || u > 1.0 ) { cout << "u is outof bounds" << endl; return -2; } V3f Q = T*e1; v = (D^Q) * invdet; if ( v < 0.0 || u + v > 1.0 ) { cout << "v is out of bounds." << endl; return -3; } t = (e2^Q) * invdet; cout << "Ray intersects triangle at t = " << t << endl;
Advertisement
In your code, e1 = v1-v0 = (2,0,0) and e2 = v2-v0 = (1,2,0), so your 3x3 matrix is

(2 1 0)
(0 2 0) = M = (e1 e2 D)
(0 0 1)

and det(M) = 4. The determinant is given by D * (e1 x e2). (x is the cross product).

You computed e1 ^ (D*e2). Is ^ power? This doesn''t make much sense... You need to compute e1 x e2 (gives you a 3-vec), then D * the result.

Lutz
Thanks! You made me think to double check my operators... not my math library you see... very confusing operators for dot and cross products. Perhaps I should use the functions instead....

This topic is closed to new replies.

Advertisement