# How does ray.intersectsTriangle work?

**URL:** <https://forum.babylonjs.com/t/how-does-ray-intersectstriangle-work/61399>\
**Category:** Questions\
**Tags:** ray\
**Created:** [November 12, 2025, 6:29pm UTC](https://forum.babylonjs.com/t/how-does-ray-intersectstriangle-work/61399 "2025-11-12T18:29:22Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe\_Kerr](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/joe_kerr/32/39017_2.png) [@Joe\_Kerr](https://forum.babylonjs.com/u/Joe_Kerr)\
**Post date:** [November 12, 2025, 6:29pm UTC](https://forum.babylonjs.com/t/how-does-ray-intersectstriangle-work/61399/1 "2025-11-12T18:29:22Z")

</div>

Hi everyone,

Please see this playground: [Babylon.js Playground](https://playground.babylonjs.com/#OIKM0N) (or take this one without context; just the ray/triangle: [Babylon.js Playground](https://playground.babylonjs.com/#F7CHCE)).

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](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._

---

<div class="post-metadata">

**Author:** ![Tricotou](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/tricotou/32/5844_2.png) [@Tricotou](https://forum.babylonjs.com/u/Tricotou)\
**Post date:** [November 12, 2025, 7:04pm UTC](https://forum.babylonjs.com/t/how-does-ray-intersectstriangle-work/61399/2 "2025-11-12T19:04:35Z")

</div>

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 :

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/c/2/c2a704ad38802110def9657256b8352733cb1cf1.png)

How do we know ? Because origins or rays are **inside** projection of A & B in the ray plane :

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/6/d/6d4c056b4636a9027201c78f95bc34fe1a07a338.png)  
(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 :

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/7/f/7f3c7133fddaf431ca510b84b9146992610ebcb6.png)

Problem in this case, is that A & B project like so :

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/7/6/767d933bb7b4a8e8dc2e050b9aea9486391f4a8a.png)

Lets draw in blue the bounding box of the segment AB, in the ray projection :

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/a/3/a33e7296553019cd434ecd1848611fdd63c37760.png)

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 :

> **[Babylon.js Playground](https://playground.babylonjs.com/#RFM0VW)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL and WebGPU 3D scenes

Here 5 will hit, 3 won’t hit

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [November 12, 2025, 7:04pm UTC](https://forum.babylonjs.com/t/how-does-ray-intersectstriangle-work/61399/3 "2025-11-12T19:04:50Z")

</div>

I think we may have a bug..Checking

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [November 12, 2025, 7:07pm UTC](https://forum.babylonjs.com/t/how-does-ray-intersectstriangle-work/61399/4 "2025-11-12T19:07:29Z")

</div>

Yes we have one bug:  
[Fix ray length check by deltakosh · Pull Request #17443 · BabylonJS/Babylon.js](https://github.com/BabylonJS/Babylon.js/pull/17443)

---

<div class="post-metadata">

**Author:** ![Tricotou](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/tricotou/32/5844_2.png) [@Tricotou](https://forum.babylonjs.com/u/Tricotou)\
**Post date:** [November 12, 2025, 7:09pm UTC](https://forum.babylonjs.com/t/how-does-ray-intersectstriangle-work/61399/5 "2025-11-12T19:09:08Z")

</div>

@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…

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [November 12, 2025, 7:09pm UTC](https://forum.babylonjs.com/t/how-does-ray-intersectstriangle-work/61399/6 "2025-11-12T19:09:42Z")

</div>

I think so. Negative distance means we are behind assuming the ray is oriented.

---

<div class="post-metadata">

**Author:** ![Tricotou](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/tricotou/32/5844_2.png) [@Tricotou](https://forum.babylonjs.com/u/Tricotou)\
**Post date:** [November 12, 2025, 7:25pm UTC](https://forum.babylonjs.com/t/how-does-ray-intersectstriangle-work/61399/7 "2025-11-12T19:25:04Z")

</div>

This fix will indeed have the ray not hitting, but what about this case ? (Still, distance \< 0, isn’t it ? But it should hit)

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/3/4/344149c9ce1192a1ac33aa9818e238f406127e14.png)

It seems that with this fix, any ray within the bounding box (distance \< 0 ) will be seen as not hitting 🤔

---

<div class="post-metadata">

**Author:** ![Joe\_Kerr](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/joe_kerr/32/39017_2.png) [@Joe\_Kerr](https://forum.babylonjs.com/u/Joe_Kerr)\
**Post date:** [November 12, 2025, 7:28pm UTC](https://forum.babylonjs.com/t/how-does-ray-intersectstriangle-work/61399/8 "2025-11-12T19:28:55Z")

</div>

Thanks @Tricotou for the explanation! 😃

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [November 12, 2025, 7:36pm UTC](https://forum.babylonjs.com/t/how-does-ray-intersectstriangle-work/61399/9 "2025-11-12T19:36:32Z")

</div>

distance is not \< 0 here right? distance is from start

---

<div class="post-metadata">

**Author:** ![Tricotou](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/tricotou/32/5844_2.png) [@Tricotou](https://forum.babylonjs.com/u/Tricotou)\
**Post date:** [November 13, 2025, 8:11am UTC](https://forum.babylonjs.com/t/how-does-ray-intersectstriangle-work/61399/10 "2025-11-13T08:11:37Z")

</div>

Ah yes you’re right ! 😁
