My Google-fu has only turned up one result that returns the point of intersection between a ray and a triangle: http://geomalgorithms.com/a06-_intersect-2.html
And I am having absolutely no luck compiling it. I have done this for the points/vectors:
typedef struct {
float x, y, z;
} Vector;
typedef struct {
float x, y, z;
} Point;
typedef struct {
Vector P0, P1;
} Ray;
typedef struct {
Point V0, V1, V2;
} Triangle;
And then it throws errors about this code:
u = T.V1 - T.V0;
v = T.V2 - T.V0;
n = u * v;
"No match for operator - (operand types are Point and Point)"
So then I rewrote all of the code to be like this:
u.x=T.V1.x-T.V0.x;
u.y=T.V1.y-T.V0.y;
u.z=T.V1.z-T.V0.z;
v.x=T.V2.x-T.V0.x;
v.y=T.V2.y-T.V0.y;
v.z=T.V2.z-T.V0.z;
n.x=u.x*v.x;
n.y=u.y*v.y;
n.z=u.z*v.z;
But it still says the ray { 3, 1, -3 } { 3, -1, -3 } is not inside the triangle { -10, 0, -10 } { 10, 0, 10 } { 10, 0, -10 }. I checked this in Blender, though, and the ray indeed intersects the triangle.
Can anyone please point out what I'm doing incorrectly, or link to another page with a function that will return the exact point where a line segment intersects a triangle? I would rather do this in C than have to link some vector library. Thank you!
operator+
,operator-
, etc. – Cory KramerT
is obviously aTriangle
. – TonyKhere
. – Sourav Ghosh