Ray Triangle intersection

Started by
8 comments, last by Zakwayda 17 years, 8 months ago
Hi all, i'm having trouble with the Ray Triangle intersection test : here is what i understand , i ommit the part of the ray/plane cause it's ok. Here is what i have : P0,P1,P2 my 3 verts for the tri, P is the point of intersection and finally N the normal of the tri. i want to calculate if p is in the tri and calculate the height value of p, can someone explain me in details the process ? thanx in advance ;-)
Advertisement
Quote:Original post by nini
Hi all, i'm having trouble with the Ray Triangle intersection test :
here is what i understand , i ommit the part of the ray/plane cause it's ok.

When you have the point in the plane formed by the triangle that the ray goes through, you can do a point in triangle test. If the point is in the triangle, so is the ray.

To get the perpendicular distance between the triangle and a point P, use

d = (P - P0) · N

if d < 0, then the point P is "on the wrong side" of the plane. E.g., if d = -5, the point is 5 units "below" the triangle.

However, the "height" of the point that is both in the ray and the triangle will always be 0, and there will be many points "above" the triangle which also are in the ray, if the ray intersects the triangle, so there you will need to specify a more specific point.
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
P0,P1,P2 are the 3 verts for the tri
P is the point of intersection
R0, R1 are the 2 points of the ray

=> write P as P0 + k*(P1-P0) + l*(P2-P0)
(where k and l are floats)

=> (1) if k+l<=1 then is P a point within the triangle

=> (2) test if R0 is on one side of the plane defined by P0,P1,P2
and R1 is on the other side. if this is the case, P is a point of the line R0R1
dotProduct((R0 - P),N)*dotProduct((R1 - P),N)<=0
(positive*negative) = negative

=> if (1) and (2) are true then there is a line-triangle collision
maybe you could use this code i wrote once:
Topic
That was an interesting method, but shouldn't
Quote:=> (1) if k+l<=1 then is P a point within the triangle
be

=> (1) if k, l ≥ 0 and k + l ≤ 1 then P is a point within the triangle

?
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
You are totally right about that

The code in the topic is right though:
"kPar>=0 && lPar>=0 && kPar+lPar<=1"
If you are using DirectX, they have a intersection test in their SDK.
D3DXIntersectTri
It also calculates UV position of the intersection, so if you need speed there is some other way that would be faster.
Quote:Original post by SuperNerd
If you are using DirectX...


i was programming for directX for a while, but i decided to move on OpenGL API for portability purposes and for really learning 3D Programming, i want to understand the mathematical concept not to use some func that do that for me...and afterall directX is microsoft way of life...

thanx to all for your participation.
Back again !
okay i have my triangle detection code that works right , i know this cause i render the triangle in red...

but when it comes to interpolate height value of the collision point it seems to be buggy...

here is a drawing of what i do :

Image Hosted by ImageShack.us

the pseudocode :

calculate the dot of the area of each subtriangle T0 T1 T2 with the full triangle normal and divide it by the area of the full triangle.
Example of signed area calculus :
AT0 = dot(cross(u0,P1-P0),n);
n is the triangle normal of P0 P1 P2.

this give me the signed barycentric coordinate a,b,c...
if(a+b+c<=1 && a>=0 && a<=1 && b>=0 && b<=1)
point P is in triangle and i calculate the height value of P like this :
P.y = a*P0.y + b*P1.y + (1-a-b)*P2.y

The triangle detection is ok but the height value seems to be false...and i don't know why...
i hope somebody helps...

One quick comment. In the code:
P.y = a*P0.y + b*P1.y + (1-a-b)*P2.y
Make sure that you're matching the barycentric coordinates with the correct triangle vertices. For example, whichever of a, b, and c is derived from the area of T0 should be matched with P2 (not P0).

This topic is closed to new replies.

Advertisement