Created
September 12, 2018 02:20
-
-
Save suzuke/8d2d3775347512a96f7df0a30f5f7570 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm | |
private bool RayIntersectsTriangle(ISupportMappable support, ref TSMatrix orientation, ref TSMatrix invOrientation, | |
ref TSVector position, ref TSVector origin, ref TSVector direction, out FP fraction, out TSVector normal) | |
{ | |
FP EPSILON = FP.EN8; | |
fraction = FP.Zero; | |
normal = TSVector.zero; | |
TriangleMeshShape inTriangle = support as TriangleMeshShape; | |
TSVector[] vertices = inTriangle.Vertices; | |
TSVector vertex0 = vertices[0]; | |
TSVector vertex1 = vertices[1]; | |
TSVector vertex2 = vertices[2]; | |
TSVector edge1, edge2, h, s, q; | |
FP a, f, u, v; | |
edge1 = inTriangle.edge1; | |
edge2 = inTriangle.edge2; | |
h = TSVector.Cross(direction, edge2); | |
a = TSVector.Dot(edge1, h); | |
if (a > -EPSILON && a < EPSILON) | |
return false; | |
f = 1 / a; | |
s = origin - vertex0; | |
u = f * (TSVector.Dot(s, h)); | |
if (u < FP.Zero || u > FP.One) | |
return false; | |
q = TSVector.Cross(s, edge1); | |
v = f * TSVector.Dot(direction, q); | |
if (v < FP.Zero || u + v > FP.One) | |
return false; | |
// At this stage we can compute t to find out where the intersection point is on the line. | |
fraction = f * TSVector.Dot(edge2, q); | |
if (fraction > EPSILON) // ray intersection | |
{ | |
return true; | |
} | |
else // This means that there is a line intersection but not a ray intersection. | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment