fast ray/triangle intersection test

Started by
36 comments, last by tcs 23 years, 11 months ago
By the way, you are not checking whether the ray is parrallel to the plane in your code.

Here is my plane/ray intersection code:
(Hoping the messageboard won''t f*k up my code...

// -------------------------------------
// CAPlane::IntersectsWithRay
// Find the intersection point of a ray and a plane;
// Returns false if the ray is parallel;
// -------------------------------------
aBool CAPlane::IntersectsWithRay( CAVector3Df& a_Intersection, const CAVector3Df& a_Origin, const CAVector3Df& a_Ray ) const
{
// Calculate the denominator;
aFloat denominator = (a_Ray.GetX() * static_cast (m_Normal.GetX())) +
(a_Ray.GetY() * static_cast (m_Normal.GetY())) +
(a_Ray.GetZ() * static_cast (m_Normal.GetZ()));

// Check for a_Ray parrallel to plane;
if ( denominator != 0 )
{
// Calculate the t value of the intersection point;
aFloat t = (static_cast (m_D) -
((a_Origin.GetX() * static_cast (m_Normal.GetX())) +
(a_Origin.GetY() * static_cast (m_Normal.GetY())) +
(a_Origin.GetZ() * static_cast (m_Normal.GetZ())) ) )
/
denominator;

// Calculate the intersection point itself;
a_Intersection = CAVector3Df( a_Origin.GetX() + t * a_Ray.GetX(),
a_Origin.GetY() + t * a_Ray.GetY(),
a_Origin.GetZ() + t * a_Ray.GetZ()
);
return true;
}
else
{
// Ray is parrallel;
return false;
}
}
____________________________Mmmm, I''ll have to think of one.
Advertisement
Besides some indention problems and the wrong static_casts everything seems ok.

static_cast should be static_cast(aFloat) where the brackets are actually those sharp ones

(english is not my native language so I don''t know what they are called.)

I''m sorry for posting three times in a row too by the way.
Anyway, see you all later.

Jaap Suter
____________________________Mmmm, I''ll have to think of one.
ok, thank you anyway !

I managed to get everything fast and reliable working. You can get my triangle class for collision detection and lightmap calculation at http://glvelocity.demonews.com/
I don''t need the parallel check cause I first test the intersections with the triangle''s hyperplane...

But now I have another problem (see my new post) ;-))

Tim
Tim--------------------------glvelocity.gamedev.netwww.gamedev.net/hosted/glvelocity
As a side note

How do you compute the arccos of a value? I am looking at doing the finding the intersection point of the plane, then summing the angles between each vert. So I have this code:

float fDot = Inter.DotProduct( Verts.Position );
float fLength = Inter.Length() * Verts.Position.Length();<br> float fCos = fDot / fLength;<br><br>Now I need to get the angle that fCos belongs to…<br><br>Any ideas <img src="smile.gif" width=15 height=15 align=middle><br><br>Thanks. </i>
try using the function acos() that is declared in math.h

- WitchLord

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Using acos() is far to slow. Go to my site ( http://glvelocity.demonews.com/ )and download my triangle class. It uses a *much* faster method for finding the intersection point and calculating if it lays inside the polygon...
Tim--------------------------glvelocity.gamedev.netwww.gamedev.net/hosted/glvelocity
im sorry, but all of the methods presented in this thread so far are incredibly slow . check out badouel''s point in poly algorithm.. three or four muls and two divisions per polygon. cant name any links off the top of my head, but there was an article in one of the graphics gems books, i''m thinking 2 or 3.

-goltrpoat


--
Float like a butterfly, bite like a crocodile.

--Float like a butterfly, bite like a crocodile.
No, this is NOT true. This alogrithm isn''t much faster, I think it''s even far slower if I rember it right...
Tim--------------------------glvelocity.gamedev.netwww.gamedev.net/hosted/glvelocity
The most important optimization you can do to the point in triangle intersection algorithm is to convert it to 2D. By doing that you can save about half of the time, perhaps even more. I didn''t do a thorough comparison of the algorithms so I might be wrong.

Let me outline my algorithm for you:

  • Find the rays intersection with the plane of the triangle
  • Convert the intersection point and triangle vertices to 2D by flattening them along the normals longest axis
  • Compute the edge normals by a simple 90 degree rotation in 2D
  • Check if the point is inside the triangle with dotproducts in 2D

    I hope I didn''t loose anyone along the way.

    - WitchLord
  • AngelCode.com - game development and more - Reference DB - game developer references
    AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

    Sounds good. So how to I rotate a 2D line?

    It seems that doing it in 2D is the way to go. Makes sense, remove one number from all calculations...

    This topic is closed to new replies.

    Advertisement