# IntersectsMesh function not working properly?

**URL:** <https://forum.babylonjs.com/t/intersectsmesh-function-not-working-properly/48472>\
**Category:** Bugs\
**Created:** [March 3, 2024, 7:41pm UTC](https://forum.babylonjs.com/t/intersectsmesh-function-not-working-properly/48472 "2024-03-03T19:41:52Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![bvaisman](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bvaisman/32/20835_2.png) [@bvaisman](https://forum.babylonjs.com/u/bvaisman)\
**Post date:** [March 3, 2024, 7:41pm UTC](https://forum.babylonjs.com/t/intersectsmesh-function-not-working-properly/48472/1 "2024-03-03T19:41:53Z")

</div>

Hello everybody,

I have a problem when using the IntersectsMesh function. Here it is :

I want to catch all meshes of the scene which are intersected by my cone and I show the bounding box of all of these and the bounding box of the cone.

Here are two screenshots to show you

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

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

Here’s my code, quite simple :

for (let i=0;i\<scene.meshes.length;i++){

```
if (scene.meshes[i].intersectsMesh(cone_light,true) == true){
      scene.meshes[i].showBoundingBox = true
                   
}

```

}

What I don’t understand :

- Why my axes arrow are intersected because they are behind the bounding box of the cone ?
- Why the red mesh is intersected because it is above the bounding box of the cone?

The problem is exactly the same when I use intersectsMesh(cone\_light,false) .

Is it an issue or do I make a mistake ?

Thanks for your answers.

Boris

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [March 4, 2024, 11:39am UTC](https://forum.babylonjs.com/t/intersectsmesh-function-not-working-properly/48472/2 "2024-03-04T11:39:28Z")

</div>

It seems `precise=true` should do it, as the OBBs don’t intersect…

Are you able to setup a simple repro in the Playground for us to test with?

---

<div class="post-metadata">

**Author:** ![bvaisman](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bvaisman/32/20835_2.png) [@bvaisman](https://forum.babylonjs.com/u/bvaisman)\
**Post date:** [March 4, 2024, 12:02pm UTC](https://forum.babylonjs.com/t/intersectsmesh-function-not-working-properly/48472/3 "2024-03-04T12:02:14Z")

</div>

I really think there is an issue or I don’t understand anything… what is also possible 😉

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

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [March 4, 2024, 12:09pm UTC](https://forum.babylonjs.com/t/intersectsmesh-function-not-working-properly/48472/4 "2024-03-04T12:09:26Z")

</div>

The world matrices are not computed yet:

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

---

<div class="post-metadata">

**Author:** ![bvaisman](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bvaisman/32/20835_2.png) [@bvaisman](https://forum.babylonjs.com/u/bvaisman)\
**Post date:** [March 4, 2024, 12:10pm UTC](https://forum.babylonjs.com/t/intersectsmesh-function-not-working-properly/48472/5 "2024-03-04T12:10:01Z")

</div>

Ok, I think I’ve found. I did not “recompute” the WorldMatrix of the meshes before the InterSectsMesh function.

---

<div class="post-metadata">

**Author:** ![bvaisman](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bvaisman/32/20835_2.png) [@bvaisman](https://forum.babylonjs.com/u/bvaisman)\
**Post date:** [March 4, 2024, 12:11pm UTC](https://forum.babylonjs.com/t/intersectsmesh-function-not-working-properly/48472/6 "2024-03-04T12:11:04Z")

</div>

😉

Same answer. Ok, you’re right. Wound it not be possible to include the computeWorldMatrix of the two meshes in the intersectsMesh function ?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [March 4, 2024, 9:43pm UTC](https://forum.babylonjs.com/t/intersectsmesh-function-not-working-properly/48472/7 "2024-03-04T21:43:46Z")

</div>

The world matrix computation is done during the normal processing of the main loop. In the case of the PG, we are not in the loop yet, so the matrices have not been computed yet.

---

<div class="post-metadata">

**Author:** ![bvaisman](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bvaisman/32/20835_2.png) [@bvaisman](https://forum.babylonjs.com/u/bvaisman)\
**Post date:** [March 5, 2024, 8:10am UTC](https://forum.babylonjs.com/t/intersectsmesh-function-not-working-properly/48472/8 "2024-03-05T08:10:16Z")

</div>

Yes, but in my application, it does not work either. I think it is a little bit annoying that a function does work only if you code something elsewhere.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [March 5, 2024, 10:45am UTC](https://forum.babylonjs.com/t/intersectsmesh-function-not-working-properly/48472/9 "2024-03-05T10:45:51Z")

</div>

Have you tried to call `computeWorldMatrix()` in your application? We try to avoid calling `computeWorldMatrix` when not necessary, as this function is a bit heavy.

---

<div class="post-metadata">

**Author:** ![bvaisman](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bvaisman/32/20835_2.png) [@bvaisman](https://forum.babylonjs.com/u/bvaisman)\
**Post date:** [March 5, 2024, 11:04am UTC](https://forum.babylonjs.com/t/intersectsmesh-function-not-working-properly/48472/10 "2024-03-05T11:04:37Z")

</div>

Yes, I’ve made another prototype function (see below) and it works now perfectly, that’s why I think it could be probably done in the intersectsMesh function, maybe with a third boolean if you don’t want to use the computeWorldMatrix function every time.

```auto
BABYLON.AbstractMesh.prototype.intersectionMesh = function(mesh,accurate = false,submeshes = false){
    this.computeWorldMatrix(true)
    mesh.computeWorldMatrix(true)
    return this.intersectsMesh(mesh,accurate,submeshes)
}

```
