Archived

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

NotTaxes

Lazy, just plain lazy

Recommended Posts

I remember doing 3d co-ordinate geometry years ago at school, but I can''t for the life of me remember how to work out the point of intersection between a line and a plane if the plane is defined by three vertices. You guessed it, I need to do accurate hit-detection on a polygon. Just thought I might excite someone by wording it slightly differently :-) Anyone close to the ground? I have plenty of talent and vision, I just don''''t give a damn.

Share this post


Link to post
Share on other sites
This is the way I do it:

    

bool LineIntersectTriangle(D3DXVECTOR3 *pLineStart, D3DXVECTOR3 *pLineDirection, D3DXVECTOR3 *pTriPoint0, D3DXVECTOR3 *pTriPoint1, D3DXVECTOR3 *pTriPoint2)
{
D3DXVECTOR3 Edge1, Edge2, tVector, pVector, qVector;
float Determinant, InvDeterminant, t, u, v;

Edge1=*pTriPoint1-*pTriPoint0; // Find vectors for two edges sharing pTriPoint0.

Edge2=*pTriPoint2-*pTriPoint0;

D3DXVec3Cross(&pVector, pLineDirection, &Edge2); // Begin calculating determinant - also used to calculate U parameter.

Determinant=D3DXVec3Dot(&Edge1, &pVector);

if(Determinant<EPSILON) return(false); // No intersection with plane of triangle (with culling), return false.


tVector=*pLineStart-*pTriPoint0; // Calculate the distance from TriPoint0 to line start.

u=D3DXVec3Dot(&tVector, &pVector); // Calculate U.


if(u<0.0f || u>Determinant) return(false); // Test U. No intersection, return false.


D3DXVec3Cross(&qVector, &tVector, &Edge1); // Prepare to test V.

v=D3DXVec3Dot(pLineDirection, &qVector); // Calculate V.


if(v<0.0f || (u+v)>Determinant) return(false); // No intersection, return false.


// If we get to this point, the RAY intersects the triangle. Note I wrote ray and not line.

t=D3DXVec3Dot(&Edge2, &qVector);
InvDeterminant=1.0f/Determinant;
t*=InvDeterminant;

if(t>=1.0f) return(false); // t has to be 1 or greater for the line to intersect the triangle.


*pLineDirection=(*pLineDirection)*t; // Truncate line direction to the intersection point.


return(true);
}



For some reason, most of the formatting of the above code seems to have disappeared. Anyway, hope it helps.



Edited by - justin nixon on June 12, 2001 1:41:40 PM

Share this post


Link to post
Share on other sites
Thanks a mil Justine. That was considerably easier than wading through my old text books... I got as far as ''Line intersects plane'' when you replied. You have saved me so much time

Cheers,



I have plenty of talent and vision, I just don''''t give a damn.

Share this post


Link to post
Share on other sites
Sorry. My hands are caffeined out and probably shook at an inappropriate moment.

I have plenty of talent and vision, I just don''''t give a damn.

Share this post


Link to post
Share on other sites