# ray - triangle intersection

This topic is 4209 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

see the question below please :) [Edited by - me_here_me on July 15, 2007 9:16:34 AM]

##### Share on other sites
hi,
I want to find out if a particular ray intersects a triangle? If it does, i want to retrieve the distance from the origin of thee ray to the point of intersection. I think that following maths does the job:

[SOURCE]/*O -> origin of rayD -> direction of rayV0 -> first vertex of the triangleV1 -> second vertex of the triangleV2 -> third vertex of the triangleA point on a triangle is defined as: {u and v are barycentric coordinates}T(u,v) = (1-u-v)V0 + uV1 + vV2R(T) = O+tDR(T) -> is the equation of the rayt -> distance the ray has traveled .... this is not coming up correctlyintersection occurs at:R(T) = T(u,v)O+tD = (1-u-v)V0 + uV1 + vV2                    _   _[-D, V1-V0, V2-V1] |  t  |                   |  u  |  =  O-V0                   |_ v _|E1 = V1-V0E2 = V2-V1              _   _[-D, E1, E2] |  t  |             |  u  |  =  O-V0             |_ v _| _   _                     _            _  |  t  |         1         |   |T,E1,E2|  ||  u  | = --------------- |   |-D,T,E2|  ||_ v _|     |D,E1,E2|     |_  |-D,E1,T| _| _   _                     _              _  |  t  |         1         |   (T x E1).E2  ||  u  | = --------------- |   (D x E2).T   ||_ v _|     (D x E2).E1   |_  (T x E1).D  _|*/[/SOURCE]

solving these should give me "t" containing the distance from the start of the ray to the point of intersection, but it does not :( Can you point of some mistake or improvement here

[Edited by - me_here_me on July 15, 2007 11:14:25 AM]

##### Share on other sites
The way I do that is like this:

1)Calculate intersection point with the triangle's plane with the ray, if no intersection, no intersection with triangle obviously.
2)Test whether intersection point is in the triangle or not, obviously if its not, no intersection. If it is, you have the intersection, and then you can just calculate the distance from origin to intersection point:

Triangle defined:
Vertices [V1,V2,V3]
Auxilary pre-calculations:
Plane Normal [N]
Plane constant [D]
Edge vectors [U = V2-V1, V = V3-V1]
V•V [vdotv]
U•U [udotu]
V•U [vdotu]
1/(vdotu*vdotu - udotu*vdotv) [denom]

Ray defined:
Origin

Direction [V]
Auxiliary:
Length [L]

float3 : 3vector (x,y,z), * = dot product (f3*f3), scale (f3*f)

float DistanceTriangleRay(const Triangle & tri, const Ray & ray){    //intersect with plane    float pt = (-tri.D-tri.N*ray.P)/(tri.N*ray.V)    if(pt<0||pt>1) return -1.f;    float3 P = ray.P + ray.V*pt;    //test for containment    float3 W = P-tri.V1;    float vdotw = tri.V*W;    float udotw = tri.U*W;    float s = (tri.vdotu*vdotw - tri.vdotv*udotw)*tri.denom;    float t = (tri.vdotu*udotw - tri.udotu*vdotw)*tri.denom;    if((s<0||s>1)||(t<0||t>1)||(s+t<0||s+t>1)) return -1.f;    //return distance from origin of ray to intersection point    return pt*ray.L;}

##### Share on other sites

I will try to do it as you suggested and lets see how it goes :)

regards

##### Share on other sites
can you please tell me about the following terms that are not very clear to me

How to set Length [L] for the ray in advance? what length is it?

best wishes

##### Share on other sites
L for the ray, is the length of the vector part.

im assuming that the ray has a non-unit direction, such that the 'end' of the ray is at t = 1, i assume it is for some sort of physics simulation thingy.

if not, and you are treating the ray as infinite, you should change the line:

if(pt<0||pt>1) return -1.f;
to
if(pt<0) return -1.f;

...

if you're ray direction IS a unit-vector, then you can forget completely about the L variable, and remove any mention of it (the only time im using it is for distance, if its a unit vector L = 1, and such multiplying the 'pt' variable by it is redundant)

##### Share on other sites
:)

thanks for the help :)

regards

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 11
• 15
• 11
• 11
• ### Forum Statistics

• Total Topics
634149
• Total Posts
3015834
×