Ray - Triangle intersection test

Started by
1 comment, last by resle 13 years ago
Hi,
I've written this variant of the Moller test, in Javascript, and I am using it within a WebGL application.

The function correctly returns whether the given ray (p,d) is intersecting or not the triangle (v0,v1,v2).
What I can't get to work, is WHERE the intersection happens.
I've tried everything... do you see any major flaw in the following code?
Thanks a lot in advance,

andrea



function check(v0, v1, v2, p, d)
{
var e1 = vec3.create();
var e2 = vec3.create();
var h = vec3.create();
var s = vec3.create();
var q = vec3.create();
var a, f, u, v;
var l = vec3.create();
var i = vec3.create();

e1[0] = v1[0] - v0[0];
e1[1] = v1[1] - v0[1];
e1[2] = v1[2] - v0[2];

e2[0] = v2[0] - v0[0];
e2[1] = v2[1] - v0[1];
e2[2] = v2[2] - v0[2];

vec3.cross(d, e2, h);
a = vec3.dot(e1, h);
if(a>-0.000001 && a < 0.00001) return null;

f = 1/a;
s[0] = p[0] - v0[0];
s[1] = p[1] - v0[1];
s[2] = p[2] - v0[2];
u = f * vec3.dot(s, h);
if (u<0 || u>1) return null;

vec3.cross(s, e1, q);
v = f * vec3.dot(d, q);
if(v<0 || (u+v)>1) return null;

t = f * vec3.dot(e2, q);
if (t>0.00001)
{
vec3.scale(d, a, l);
vec3.add(p,l,i);
return i;
}

return null;
}
..so we ate rare animals, we spent the night eating rare animals..
Advertisement
Presumably the point is given as
v0 * ( 1 - u - v ) + v1 * u + v2 * v
(using the barycentric co-ordinates on the triangle) as well as
p + d * t
(using the ray) where you want to use the latter.

In your code snippet, perhaps this line
vec3.scale(d, a, l);
should be better this line
vec3.scale(d, t, l);

Moreover, why is the if (t>0.00001) needed? IMHO the function should work well with any t.

Perhaps this
vec3.scale(d, a, l);
should be better
vec3.scale(d, t, l);



And with this, two weeks of migraines are officially ended.
I thank you from the deepest of my [insert your favourite organ]

- andrea

..so we ate rare animals, we spent the night eating rare animals..

This topic is closed to new replies.

Advertisement