Possible bug in mathutils.geometry.intersect_ray_tri ?

I’m trying to figure out why a script I wrote that uses intersect_ray_tri isn’t yielding the results I expect. Here is a little code to reproduce the issue:

import mathutils

# a triangle "overhead"... +z being up
v1 = mathutils.Vector((0, 1, 1))
v2 = mathutils.Vector((1, -1, 1))
v3 = mathutils.Vector((-1, -1, 1))

origin = mathutils.Vector((0, 0, 0))

# makes sense that this should report an intersection, since it's going up through the triangle
ray = mathutils.Vector((0, 0, 2))
print(mathutils.geometry.intersect_ray_tri(v1, v2, v3, ray, origin))

# but why does this report an intersection?
ray = mathutils.Vector((0, 0, -2))
print(mathutils.geometry.intersect_ray_tri(v1, v2, v3, ray, origin))

This is using blender’s coordinate system of z = up, x = right, y = out. The triangle composed of v1, v2, and v3 is “overhead”, and the first intersection test is a ray shooting straight up through it. However, the second test is a ray going the complete opposite direction, yet it also has an intersection.

It seems that intersect_ray_tri is treating the ray as an infinite line… the magnitude of the ray doesn’t seem to matter, and neither does the direction (one way or the opposite way).

Can anybody shed some light on this? Thanks

A ray is different from a line segment. You can specify them both with 2 points in space, a line segment connects two points (in a straight way) however a ray lies on that same line but proceeds indefinitely. So you actually created two identical rays, running along the z-axis.

You might want to check out mathutils.geometry.intersect_line_plane() and combine the two. A plane is kind of similar to a ray, it lies on a triangle (moves through 3 points) but also proceeds indefinitely.