# Mesh.dispose() and intersectsMesh()

**URL:** https://forum.babylonjs.com/t/mesh-dispose-and-intersectsmesh/16025
**Category:** Questions
**Created:** [November 25, 2020, 6:01pm UTC](https://forum.babylonjs.com/t/mesh-dispose-and-intersectsmesh/16025 "2020-11-25T18:01:03Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)
#### Post date: [November 25, 2020, 6:01pm UTC](https://forum.babylonjs.com/t/mesh-dispose-and-intersectsmesh/16025/1 "2020-11-25T18:01:03Z")

</div>

Here is original playground from Documentation showing mesh intersections - [Babylon.js Playground](https://playground.babylonjs.com/#KQV9SA)

Let dispose all meshes except baloons:

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/8/845ff9ba2e73c7f85d2fa950de93de4c1523f825.png)

[https://playground.babylonjs.com/#KQV9SA#135](https://playground.babylonjs.com/#KQV9SA#135)

Still baloons change their colors when intersecting the place of the disposed mesh.

So the question is: how to properly dispose meshes in this case in order they wouldnt show intersections again after their disposal?

---

<div class="post-metadata">

### Author: ![msDestiny14](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/msdestiny14/32/9572_2.png) [@msDestiny14](https://forum.babylonjs.com/u/msDestiny14)
#### Post date: [November 25, 2020, 9:29pm UTC](https://forum.babylonjs.com/t/mesh-dispose-and-intersectsmesh/16025/2 "2020-11-25T21:29:06Z")

</div>

Looking around and I’m scratching my head on this myself. My solution to this was to simply keep track of meshes you are deleting. I think just because you delete the mesh doesn’t mean the object’s memory (aka the position and physics) is gone.

[https://playground.babylonjs.com/#KQV9SA#136](https://playground.babylonjs.com/#KQV9SA#136)

I then set the deleted object to null and do a check before the collision detection function. This seems a little hacky to me so is there a better way to do this? @sebavan

---

<div class="post-metadata">

### Author: ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)
#### Post date: [November 25, 2020, 9:34pm UTC](https://forum.babylonjs.com/t/mesh-dispose-and-intersectsmesh/16025/3 "2020-11-25T21:34:57Z")

</div>

When intersecting only the bounding info and couple more are check to speed it up, in your case, you need to manage the list on your side adding extra checks and so on would not fit all use cases and would in the end bloat the engine. Internally in action manager for instance, we check the mesh against the other ones of the scene, and ondispose remove them from the scene which is exactly the same.

---

<div class="post-metadata">

### Author: ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)
#### Post date: [November 25, 2020, 10:07pm UTC](https://forum.babylonjs.com/t/mesh-dispose-and-intersectsmesh/16025/4 "2020-11-25T22:07:00Z")

</div>

As Inspector shows, there are no meshes to intersect. Seems that bounding info is not deleted after mesh disposal. Is there some way to delete this bounding info (or maybe there is some function to update something regarding bounding info parameters)?

---

<div class="post-metadata">

### Author: ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)
#### Post date: [November 25, 2020, 10:11pm UTC](https://forum.babylonjs.com/t/mesh-dispose-and-intersectsmesh/16025/5 "2020-11-25T22:11:46Z")

</div>

But you still have a mesh even if not part of the scene actually you should not call intersect on disposed meshes. removing the bounding info or other hacks would fail as the intersect function will fail to find what it needs. I sometimes use “dummy” mesh outside a scene for intersection only and in this case we would be stuck.

As intersect is quite perf intensive the best option is to not call it.

---

<div class="post-metadata">

### Author: ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)
#### Post date: [November 25, 2020, 10:18pm UTC](https://forum.babylonjs.com/t/mesh-dispose-and-intersectsmesh/16025/6 "2020-11-25T22:18:25Z")

</div>

What will be the best function in this case if intersects function is so dangerous for performance?

---

<div class="post-metadata">

### Author: ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)
#### Post date: [November 25, 2020, 10:19pm UTC](https://forum.babylonjs.com/t/mesh-dispose-and-intersectsmesh/16025/7 "2020-11-25T22:19:54Z")

</div>

It is not dangerous but useless to call on disposed meshes as it will incur an unneeded perf hit.

Why not only calling it on what you need ?

---

<div class="post-metadata">

### Author: ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)
#### Post date: [November 26, 2020, 2:33pm UTC](https://forum.babylonjs.com/t/mesh-dispose-and-intersectsmesh/16025/8 "2020-11-26T14:33:27Z")

</div>

Thank you for your answers!  
So, for my application I decided not to dispose meshes but hide them (with some condition) for later reuse.  
I mark @msDestiny14 answer as the solution to my question 🙂
