Advertisement

Line - Triangle Intersection Tests

Started by October 19, 2001 04:21 PM
2 comments, last by jollyjeffers 23 years, 3 months ago
Hi, I''ve been working on a lightmap generator for my game engine, and I need to ray-trace from a light to a pixel to detect any shadows... So I have a line, in 3D, defined by O[X,Y,Z] and P[X,Y,Z] and a list of triangles defined by their vertices P[0-2] I''ve been trying to use the fairly standard equation as shown in the "Game Programming Gems 1" collision detection gem: aX+bY+cZ+d=0 a=Normal.x b=Normal.y c=Normal.z d=-DotProduct(Normal,Vertex[0]) t=-(A*P.X + B*P.Y + C*P.Z + D) / (A*(O.X-P.X) + B*(O.Y-P.Y) + C*(O.Z-P.Z)) point of intersection = O + t(P-O) BUT, I seem to be getting some rather wacky results - the point of intersection comes out as being a point not on the triangle, and not on the plane - fairly randomly along the line instead... CAN someone check the above formulae, and/or point me to some tutorial/article that covers an alternative? Many thanks in advance Jack;

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

the formula is correct...
try this

px0 is the first point of the vector
s is the direction of the vector
Po,P1,P2 the point of the triangle and R is a vector holding the result

void RayPlaneIntr( fVect Px0,fVect S,
fVect P0 ,
fVect P1,
fVect P2,
fVect R )
{

fVect N,N1;
qVect Q;
float t,d;
N[0]=P1[0]-P0[0];
N[1]=P1[1]-P0[1];
N[2]=P1[2]-P0[2];
N1[0]=P2[0]-P0[0];
N1[1]=P2[1]-P0[1];
N1[2]=P2[2]-P0[2];
Q[0]= N[1]*N1[2]-N1[1]*N[2];
Q[1]= N[2]*N1[0]- N[0]*N1[2];
Q[2]=N1[1]*N[0] - N[1]*N1[0];
Q[3]=-Q[0]*P0[0]-Q[1]*P0[1]-Q[2]*P0[2];
d=(Q[0]*S[0]+Q[1]*S[1]+Q[2]*S[2]);
if ( d==0 ) d+=(float)EPSILON;
t=-((Q[0]*Px0[0]+Q[1]*Px0[1]+Q[2]*Px0[2]+Q[3])/(d));
R[0]=Px0[0]+t*S[0];
R[1]=Px0[1]+t*S[1];
R[2]=Px0[2]+t*S[2];
}


Advertisement
Thanks for that - I shall have a good look at it later.

However, what is "EPSILON" - I assume it''s a mathematical constant of some kind; but what? sorry if it''s a stupid question, but just wanted to check =)

thanks
Jack;

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Ah yes, EPSILON is a very small number sort of 0.0000001


This topic is closed to new replies.

Advertisement