Skip to content

Instantly share code, notes, and snippets.

@joreg
Created April 30, 2014 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joreg/fb6df2174d95b82bdb21 to your computer and use it in GitHub Desktop.
Save joreg/fb6df2174d95b82bdb21 to your computer and use it in GitHub Desktop.
source of Intersect (EX9.Geometry Quad)
for index := FromSlice to ToSlice do
begin
//get transform matrices
QuadTransform := FQuadTransformManager.ModelMatrix[index];
LineTransform := FLineTransformManager.ModelMatrix[index];
//set quad and line (quad from (-0.5, -0.5, 0) to (0.5, 0.5, 0), line on z-axis )
PQuad := TD3DXVector3.Create(0, 0, 0);
nQuad := TD3DXVector3.Create(0, 0, 1);
PLine1 := TD3DXVector3.Create(0, 0, -0.5);
PLine2 := TD3DXVector3.Create(0, 0, 0.5);
//transform line
D3DXVec3TransformCoord(PLine1, PLine1, LineTransform);
D3DXVec3TransformCoord(PLine2, PLine2, LineTransform);
//transform line into object space of the quad
InvQuadTransform := TD3DXMatrix.Create( 0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 1);
D3DXMatrixInverse(InvQuadTransform, nil, QuadTransform);
D3DXVec3TransformCoord(PLine1, PLine1, InvQuadTransform);
D3DXVec3TransformCoord(PLine2, PLine2, InvQuadTransform);
//get line direction vector
D3DXVec3Subtract(rLine, PLine2, Pline1);
//check attitude of line and plane
denom := nQuad.x*rLine.x + nQuad.y*rLine.y + nQuad.z*rLine.z;
//if NOT parallel do calculations
if denom <> 0 then
begin
FIntersectsPlanePin.Value[index] := 1;
//get intersection position on line
LineAlpha := (nQuad.x*(PQuad.x - PLine1.x) + nQuad.y*(PQuad.y - PLine1.y) +
nQuad.z*(PQuad.z - PLine1.z) ) / denom;
//calculate intersection point
IntPoint := TD3DXVector3.Create( PLine1.x + LineAlpha*rLine.x, PLine1.y + LineAlpha*rLine.y,
PLine1.z + LineAlpha*rLine.z);
FLineAlphaPin.Value[index] := LineAlpha - 0.5;
FQuadAlphaPin.Value[index] := IntPoint.x;
FQuadBetaPin.Value[index] := IntPoint.y;
//check if intersection is on quad
FIntersectsQuadPin.AsBool[index] := (IntPoint.x <= 0.5) and (IntPoint.x >= -0.5) and
(IntPoint.y <= 0.5) and (IntPoint.y >= -0.5);
//check if intersection is on line segment
FIntersectsLineSegmentPin.AsBool[index] := FIntersectsQuadPin.AsBool[index] and (LineAlpha <= 1) and (LineAlpha >= 0);
//transform intersection point into world space
D3DXVec3TransformCoord(IntPoint, IntPoint, QuadTransform);
FIntersectionPoint.setV3(index, IntPoint)
end
//nothing to do if parallel
else
begin
FIntersectsPlanePin.Value[index] := 0;
FIntersectsLineSegmentPin.Value[index] := 0;
FIntersectsQuadPin.Value[index] := 0;
FLineAlphaPin.Value[index] := 0;
FQuadAlphaPin.Value[index] := 0;
FQuadBetaPin.Value[index] := 0;
end;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment