Archived

This topic is now archived and is closed to further replies.

AyaKoshigaya

Line and Triangle Intersection

Recommended Posts

Hi, does anyone know how I can find out if a Line interects a Triangle??? I''ve found the code on Gametutorials.com, and it worked very well, but it''s very very slow... so, does anyone know how I can test if a Triangle and Line intersection can be, before I use the slow method to find it out exactly??? I''ve thougt on something like using BoundingBoxes or something.. any idea? Au''revoir, Aya~

Share this post


Link to post
Share on other sites

bool NPhysics::LineFaceIntersectionTest(const NMath::Vector3f &_rLineStartPoint,const NMath::Vector3f &_rLineEndPoint,const NMath::Vector3f _aVertices[],const NMath::Vector3f &_rNormal)
{
float OriginDistance = -NMath::DotProduct(_aVertices[0],_rNormal);

if ((NMath::DotProduct(_rNormal,_rLineStartPoint) + OriginDistance) * (NMath::DotProduct(_rNormal,_rLineEndPoint) + OriginDistance) >= 0)
return false;

NMath::Vector3f Point;
NMath::Vector3f LineDirection = _rLineEndPoint - _rLineStartPoint;

LineDirection.Normalize();

float Numerator = -NMath::DotProduct(_rNormal,_rLineStartPoint) - OriginDistance;
float Denominator = NMath::DotProduct(_rNormal,LineDirection);

Denominator == 0.0f ? Point = _rLineStartPoint : Point = _rLineStartPoint + LineDirection * (Numerator / Denominator);

if (!PointInsideTriangle(Point,_aVertices))
return false;

return true;
}


Heh, thats mine.. based on the one found at gametutorials.com =(

Share this post


Link to post
Share on other sites