#### Archived

This topic is now archived and is closed to further replies.

# Ray-Triangle Intersection

## Recommended Posts

e    122
Can anyone please me how to test if a ray and a triangle (or any kind of convex polygon) intersect. Thanks. -e

#### Share this post

##### Share on other sites
rowbot    122
First get the point of intersection between the ray and the plane of the polygon. You can do this by substituting the paramtric equations for x,y and z of the ray into the plane equation and then solving for t.

Then you have to check if that point is inside all of the edges of the polygon. One way is to create planes though each of the edges and perpendicular to the polygon plane. If a point on the polygon is on the opposite side of that plane to the intersection point then they ray does not intersect it.

I''d give you code and equations but I havent got them on me at the moment. Try searching these forums or the web.

ro

#### Share this post

##### Share on other sites
Nate    122
Check out this page, http://www.swin.edu.au/astronomy/pbourke/geometry/, there is some good info there. Also, I put the code that I have been using for ray triangle intersection here, http://nate.scuzzy.net/common/. The files you should look at are util3d.cpp and util3d.h. Hope this helps.

Nate Miller
http://nate.scuzzy.net

#### Share this post

##### Share on other sites
Guest Anonymous Poster
I have a similar problem.

I''m trying to find the intersection of a vector and a plane, given three points that comprise the plane. I''ll check out your links, but if this is a different kind of problem any advice is appreciated.
Thanks.

darrennix@msn.com

#### Share this post

##### Share on other sites
Guest Anonymous Poster
And if it helps any, here is the code to test whether a point is in a triangle..

It''s in 2D because I wrote a simple app to test the theory. Basically, the point is in the triangle if the point''s angle from each triangle edge is less than the angle of that triangle edge.

call: ptinangle for A,B,C; B,C,A; and C,A,B

bool ptinangle(POINT v, POINT v0, POINT v1, POINT v2)
{
double
fTheta = acos(((v.x-v0.x)*(v2.x-v0.x)+(v.y-v0.y)*(v2.y-v0.y))/(mag(v-v0)*mag(v2-v0))),
fAngle = acos(((v1.x-v0.x)*(v2.x-v0.x)+(v1.y-v0.y)*(v2.y-v0.y))/(mag(v1-v0)*mag(v2-v0)));

if(fTheta < fAngle) return true;
return false;
}

Hope this helps

#### Share this post

##### Share on other sites
e    122
Hey, thanks all for responding.

Rowbot: Yeah, I am using that algo. right now, but I was wondering if there was another one (possibly faster).

Nate: Thanks for the link. I''ll check it out.

Anonymous: Your problem can be solved the same way. Just calculate the plane using the three points you have and then find the intersection of the ray and the plane with this code.

Eqtn 1:

o = origin of ray
d = unit vector (normalized) direction of ray

t = -(A * o.x + B * o.y + C * o.z + D) \(A * d.x + B * d.y + C * d.z)

If t is negative, the ray and plane never intersect. If the denomiator of the equation is zero, the ray and plane are parallel.

Then, to find the intersection point:

i = intersection point

i.x = o.x + d.x * t
i.y = o.y + d.y * t
i.z = o.z + d.z * t

Hope this helps.

-e