Hi everyone,
Please see this playground: Babylon.js Playground (or take this one without context; just the ray/triangle: Babylon.js Playground).
The overall code might still be wrong. I have been adapting it from AI. But the important unit is on line 33.
Given that ray and that triangle, I do not see how this ever could count as a hit? Even more so, if you increase the ray distance: it goes away from the triangle 
Best wishes
Joe
For the sake of completeness: originally I wanted a precise mesh vs mesh collision test. AI got me into the triangle issue. Anyway, I solved it using CSG (with like 3 lines of code; lol): https://playground.babylonjs.com/#OIKM0N#1
*Except that Manifold (Babylon’s CSG lib) expects the geometry to be manifold (who would have thought..). So it fails for like half of my meshes. Finally I ended up using Havok (collision callbacks). Works pretty well too and is only a bit more code.
Hey.
First of all : the ray direction (infinite line) does intersect the triangle.
Now the problem is about ray origin, dir, and length, regarding the triangle.
Let’s assume 2D to simplify :
In the drawing, both rays directions cross AB :
How do we know ? Because origins or rays are inside projection of A & B in the ray plane :
(In the 3D case, the projection is a triangle and not a segment)
But you are not in this case, you are in a particular case
You are in this case :
Problem in this case, is that A & B project like so :
Lets draw in blue the bounding box of the segment AB, in the ray projection :
Your ray is basically INSIDE the bounding box, so whatever the distance, you hit (distance < 0)
You can test it by moving a bit the origin (using dir vector) to have it outside of the bounding box :
Here 5 will hit, 3 won’t hit
2 Likes
I think we may have a bug..Checking
@Deltakosh I’m not sure how the ray.intersectsTriangle is done to be honest, but I suppose that there is indeed a distance error if the ray origin is inside the bounding box, which would make sense if it’s using bbox for perf reasons…
I think so. Negative distance means we are behind assuming the ray is oriented.
1 Like
This fix will indeed have the ray not hitting, but what about this case ? (Still, distance < 0, isn’t it ? But it should hit)
It seems that with this fix, any ray within the bounding box (distance < 0 ) will be seen as not hitting 
Thanks @Tricotou for the explanation! 
distance is not < 0 here right? distance is from start
1 Like