Jump to content
  • Advertisement
Sign in to follow this  
me_here_me

ray - triangle intersection

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
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 ray
D -> direction of ray

V0 -> first vertex of the triangle
V1 -> second vertex of the triangle
V2 -> third vertex of the triangle

A point on a triangle is defined as: {u and v are barycentric coordinates}
T(u,v) = (1-u-v)V0 + uV1 + vV2

R(T) = O+tD
R(T) -> is the equation of the ray
t -> distance the ray has traveled .... this is not coming up correctly


intersection 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-V0
E2 = 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

thanks in advance

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!