Skip to main content
GameDev.net gamedev.net
🔒 Locked

Ray/triangle test. Has this already been done?

Started by wforl Aug 4, 2010 at 7:35 AM 5 replies 2k views
Original Post
wforl
wforl
So, I came up with the following, and was wondering if had already been done? I believe it to work, although someone may find it doesnt work for all cases :)

Here is what Im doing;
Image and video hosting by TinyPic

1. First test if ray hits the plane
2. if so, then test the dot between p and each corner, to a vector perpendicular to that corners edge



int RayTriangleIntersection2( const fsRay& r, const fsVec3& a, const fsVec3& b, const fsVec3& c, fsVec3& P, float &t ){	fsPlane plane( a, b, c );	if( (t = RayPlaneIntersection( plane, r )) > 0.0f )	{		P = r.GetOrigin() + ( t*r.GetDirection() );		if( Dot(Cross( c-a, plane.GetNormal() ), P-a ) > 0.0f )		{			if( Dot(Cross( a-b, plane.GetNormal() ), P-b ) > 0.0f )			{				if( Dot(Cross( b-c, plane.GetNormal() ), P-c ) > 0.0f )				{					return 1;				}			}		}	}	return 0;}
szecs
szecs
Your method works in all cases (in fact, it works with any convex polygons).

I use a very similar stuff:

For convex polygons I use:
for( int i = 0; i < sides; i++ ){		scalar = dot(cross(vertex-P,vertex[(i+1)%sides]-P),normal_of_plane);	if( scalar > 0)		return false;}return true;
Zakwayda
Zakwayda
Quote:
So, I came up with the following, and was wondering if had already been done?
Yes, that's a standard method for testing for intersection between a ray and a triangle or other convex polygon. (It's not the only method, nor is it always the best choice, but it is standard.)
quasar3d
quasar3d
I don't know what he used, but you can do it using Inkscape, which is free, and in fact a really cool program:)
wforl
wforl
Quote:

Yes, that's a standard method for testing for intersection between a ray and a triangle or other convex polygon. (It's not the only method, nor is it always the best choice, but it is standard.)


ah, ok. I did have a quick search and a lot of the methods I found used Barycentric Coordinates and Cramers rule to solve for the matrix equation. Could you point me to a source that uses the approach I took?

Quote:

By the way, what do you dudes use for making those nice sketches? Is it freeware?


That was just a quick doodle in photoshop
Zakwayda
Zakwayda
Quote:
Could you point me to a source that uses the approach I took?
Not off the top of my head. If you want to look for one though, try Googling or searching the forum archives here on GDNet for (e.g.) 'triangle side planes' or 'polygon side planes'.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.