I have the following collision detection function that checks the collision between the line defined by lstart -> lend and the triangle defined by points a,b and c with a normal of nvec. I want to know if it will work and what to put into the missing part. (it is near the start and is commented). Here is my code:
bool Collides(glCoordFloat lstart,glCoordFloat lend,glCoordFloat a,glCoordFloat b,glCoordFloat c,glCoordFloat nvec)
{
//calculate the dot product of vec and nvec
double vn=((lend.x-lstart.x)*nvec.x)+((lend.y-lstart.y)*nvec.y)+((lend.z-lstart.z)*nvec.z);
if (closeEnough(vn,0))//if this is zero than the line is parallel to the triangle!
{
//calculate the dot product of start->a point on the plane of the triangle and nvec
double dn=((lstart.x-a.x)*nvec.x)+((lstart.y-a.y)*nvec.y)+((lstart.z-a.z)*nvec.z);
if (closeEnough(dn,0))//it IS in the plane of the triangle!
{
//check for intersection of the three lines bounding the triangle and the vector
//I am stuck...
return ( )
}
else
{
//there is absolutely no way that that there is an intersection! GOOD BYE!
return false;
}
}
//ok, so it has a specific point of intersection with the plane, lets find it
double s;//s is how far along the vector the intersection is!
s=((nvec.x*(a.x-lstart.x))+(nvec.y*(a.y-lstart.y))+(nvec.z*(a.z-lstart.z)))/vn;
if (s<0.0||s>1.0)
{
//not in the plane, not in the triangle!
return false;
}
glCoordFloat i;//this is the intersection!
i.x=lstart.x+vec.x*s;
i.y=lstart.y+vec.y*s;
y.z=lstart.z+vec.z*s;
//now to check if i is in the triangle a,b,c
//calculate the dot-products:
double uu=((b.x-a.x)*(b.x-a.x))+((b.y-a.y)*(b.y-a.y))+((b.z-a.z)*(b.z-a.z));
double vv=((c.x-a.x)*(c.x-a.x))+((c.y-a.y)*(c.y-a.y))+((c.z-a.z)*(c.z-a.z));
double uv=((b.x-a.x)*(c.x-a.x))+((b.y-a.y)*(c.y-a.y))+((b.z-a.z)*(c.z-a.z));
double uw=((b.x-a.x)*(i.x-a.x))+((b.y-a.y)*(i.y-a.y))+((b.z-a.z)*(i.z-a.z));
double vw=((c.x-a.x)*(i.x-a.x))+((c.y-a.y)*(i.y-a.y))+((c.z-a.z)*(i.z-a.z));
double denom=(uv*uv)-(uu*vv);
s=((uv*vw)-(vv*uw))/denom;//why re-allocate s?
if (s<0.0||s>1.0)
{
//not in the triangle, sorry :C
return false;
}
double t=((uv*uw)-(uu*vw))/denom;
return (!(t<0.0||(s+t)>1.0));
}