Original Post
Hey guys, please help me out someone and dont bug me because of my lack of math knowledge
. I just cant get polygon line collision to work properly:
I''m testing the edges of a triangle against the polys that build up my view frustum. The problem is, when all edges are within the frustum, a collision is still registered. It seems all code i''ve tried so far checks if the endless ray the line describes hits the poly, not the line itself.
So I checked with D3DXPlaneIntersectLine() if the edge hits the plane the poly describes, top of the code. And its NOT working! If all lines are inside the frustum the function returns a collision. Is this funtion also only checking the ray against the plane?
Well, I''ll post along the code of my current function, I already wasted some nights on this, so please, help me out until I buy my linear algebra book...
MLE_API bool mleLineHitsTriangle2(const D3DXVECTOR3& pvLineA,
const D3DXVECTOR3& pvLineB,
const D3DXVECTOR3& pvTriA,
const D3DXVECTOR3& pvTriB,
const D3DXVECTOR3& pvTriC,
D3DXVECTOR3* pvOut) // =NULL
{
D3DXVECTOR3 vOut;
D3DXPLANE Plane;
// die ebene des dreiecks berechnen
D3DXPlaneFromPoints(&Plane, &pvTriA, &pvTriB, &pvTriC);
// wenn sie die ebene nicht schneidet, kann sie das dreieck nicht schneiden
if (D3DXPlaneIntersectLine(&vOut, &Plane, &pvLineA, &pvLineB) == NULL)
return false;
// der richtungsvektor der linie
D3DXVECTOR3 vDir = pvLineB - pvLineA;
// Find vectors for two edges sharing vert0
D3DXVECTOR3 edge1 = pvTriB - pvTriA;
D3DXVECTOR3 edge2 = pvTriC - pvTriA;
// Begin calculating determinant - also used to calculate U parameter
D3DXVECTOR3 pvec;
D3DXVec3Cross(&pvec, &vDir, &edge2);
// If determinant is near zero, ray lies in plane of triangle
float det = D3DXVec3Dot(&edge1, &pvec);
D3DXVECTOR3 tvec;
if(det > 0)
{
tvec = pvLineA - pvTriA;
}
else
{
tvec = pvTriA - pvLineA;
det = -det;
}
if( det < 0.0001f )
return false;
// Calculate U parameter and test bounds
vOut.x = D3DXVec3Dot(&tvec, &pvec);
if(vOut.x < 0.0f || vOut.x > det)
return false;
// Prepare to test V parameter
D3DXVECTOR3 qvec;
D3DXVec3Cross(&qvec, &tvec, &edge1);
// Calculate V parameter and test bounds
vOut.y = D3DXVec3Dot(&vDir, &qvec);
if(vOut.y < 0.0f || vOut.x + vOut.y > det)
return false;
// Calculate t, scale parameters, ray intersects triangle
vOut.z = D3DXVec3Dot(&edge2, &qvec);
float fInvDet = 1.0f / det;
vOut.z *= fInvDet;
vOut.x *= fInvDet;
vOut.y *= fInvDet;
if (pvOut != NULL)
*pvOut = vOut;
return true;
}
its got mixed german and english comments cause its mainly taken from the DX ''Pick'' sample