CVector3 LP1
CVector3 TP1
float Dist1 = (LP1-TP1).Dot( Normal );
bool CheckLineTri( CVector3 TP1, CVector3 TP2, CVector3 TP3, CVector3 LP1, CVector3 LP2, CVector3 &HitPos)
{
CVector3 Normal, IntersectPos;
// Find Triangle Normal
Normal = CrossProduct( TP2 - TP1, TP3 - TP1 );
Normal.Normalize(); // not really needed
// Find distance from LP1 and LP2 to the plane defined by the triangle
float Dist1 = (LP1-TP1).Dot( Normal );
float Dist2 = (LP2-TP1).Dot( Normal );
if ( (Dist1 * Dist2) >= 0.0f) return false; // line doesn't cross the triangle.
if ( Dist1 == Dist2) return false;// line and plane are parallel
// Find point on the line that intersects with the plane
IntersectPos = LP1 + (LP2-LP1) * ( -Dist1/(Dist2-Dist1) );
// Find if the interesection point lies inside the triangle by testing it against all edges
CVector3 vTest;
vTest = CrossProduct( Normal, TP2-TP1 );
if ( Dot( vTest, IntersectPos-TP1) < 0.0f ) return false;
vTest = CrossProduct( Normal, TP3-TP2 );
if ( Dot( vTest, IntersectPos-TP2) < 0.0f ) return false;
vTest = CrossProduct( Normal, TP1-TP3 );
if ( Dot( vTest, IntersectPos-TP1) < 0.0f ) return false;
HitPos = IntersectPos;
return true;
}
Substract the vector and then multiply with the dot product of normal, or substract and then dot product and put the result to normal?
dist1 = (LP1-TP1) * Normal // "*" == dot product