I’m sure that this has been gone over hundereds of times on this forum alone… but I’m going to ask anyway. I just want to make a function that checks if a single ray intersects a single triangle. I currently have a function that is pretty much a replica of the first MT algorithm, and it almost seems to work. To test the function I am rendering a triangle and a line, with the arrow keys controling the position of the line. When the function says there was an intersection, the line is red, if it says there wasnt the line is green. It works along one side of the triangle, and then it stops working about 1/2 of the way in to the center of the triangle on the other two sides. And if i switch the order that I feed the vertices to the function in, say from intersection(o,d,v1,v2,v3) to intersection(o,d,v2,v3,v1) wierd stuff happens. I wish I could be of more help in helping me, but I can find the dot product and cross product, but I really dont know what any of it means. This is the function that i’m using now…
bool intersect(Vert origin, Vert direction, Vert v1, Vert v2, Vert v3)
{
Vert edge1, edge2,pvec;
float det;
edge1 = v2-v1;
edge2 = v3-v1;
pvec = CROSS(direction,edge2);
det = DOT(edge1,pvec);
if(det<EPSILON)
return false;
Vert tvec;
tvec = origin - v1;
float u,v;
u = DOT(tvec,pvec);
Vert qvec;
qvec = CROSS(tvec,edge1);
v = DOT(d,qvec);
if(u < 0.0f||u > det)
return false;
if(v < 0.0f||u+v >det)
return false;
return true;
}