# Remove objects that intersect

**URL:** <https://discourse.threejs.org/t/remove-objects-that-intersect/45115>\
**Category:** Questions\
**Tags:** intersection, box3\
**Created:** [November 25, 2022, 2:53am UTC](https://discourse.threejs.org/t/remove-objects-that-intersect/45115 "2022-11-25T02:53:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Teymoor](https://avatars.discourse-cdn.com/v4/letter/t/ad7895/32.png) [@Teymoor](https://discourse.threejs.org/u/Teymoor)\
**Post date:** [November 25, 2022, 2:53am UTC](https://discourse.threejs.org/t/remove-objects-that-intersect/45115/1 "2022-11-25T02:53:07Z")

</div>

How can I remove the cubes that intersect with the model?  
[example](https://codepen.io/Teymoor/pen/rNKvypr)

![sdfsd](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/8/f/8f229b31ecda6af370044e838ecdf97a934ace46.jpeg)

---

<div class="post-metadata">

**Author:** ![tfoller](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@tfoller](https://discourse.threejs.org/u/tfoller)\
**Post date:** [November 25, 2022, 7:52am UTC](https://discourse.threejs.org/t/remove-objects-that-intersect/45115/2 "2022-11-25T07:52:11Z")

</div>

Removing instances from **InstancedMesh** is not a trivial task.  
You need to store all matrices you apply in **setMatrixAt** in a separate array.

If you want to remove instances, you need to remove the matrices from the array and re-apply the array to the instance mesh. Don’t forget to change **.count** property as well.

---

<div class="post-metadata">

**Author:** ![Teymoor](https://avatars.discourse-cdn.com/v4/letter/t/ad7895/32.png) [@Teymoor](https://discourse.threejs.org/u/Teymoor)\
**Post date:** [November 25, 2022, 12:42pm UTC](https://discourse.threejs.org/t/remove-objects-that-intersect/45115/4 "2022-11-25T12:42:24Z")

</div>

Can you give an example or a reference of this solution because I couldn’t find anything about it to understand how to do this

---

<div class="post-metadata">

**Author:** ![Teymoor](https://avatars.discourse-cdn.com/v4/letter/t/ad7895/32.png) [@Teymoor](https://discourse.threejs.org/u/Teymoor)\
**Post date:** [November 25, 2022, 12:55pm UTC](https://discourse.threejs.org/t/remove-objects-that-intersect/45115/5 "2022-11-25T12:55:46Z")

</div>

Can I store intersecting objects in a separate array?

---

<div class="post-metadata">

**Author:** ![tfoller](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@tfoller](https://discourse.threejs.org/u/tfoller)\
**Post date:** [November 25, 2022, 3:33pm UTC](https://discourse.threejs.org/t/remove-objects-that-intersect/45115/6 "2022-11-25T15:33:34Z")

</div>

> [@Teymoor](#):
>
> Can you give an example or a reference of this solution because I couldn’t find anything about it to understand how to do this

> **[Edit fiddle - JSFiddle - Code Playground](https://jsfiddle.net/tfoller/tnoLcw4v/)**
>
> Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.

---

<div class="post-metadata">

**Author:** ![abe\_gar](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/abe_gar/32/27369_2.png) [@abe\_gar](https://discourse.threejs.org/u/abe_gar)\
**Post date:** [November 25, 2022, 4:43pm UTC](https://discourse.threejs.org/t/remove-objects-that-intersect/45115/7 "2022-11-25T16:43:07Z")

</div>

If you’re using raycaster you can get the model’s name and remove from scene instance

---

<div class="post-metadata">

**Author:** ![Teymoor](https://avatars.discourse-cdn.com/v4/letter/t/ad7895/32.png) [@Teymoor](https://discourse.threejs.org/u/Teymoor)\
**Post date:** [November 25, 2022, 10:40pm UTC](https://discourse.threejs.org/t/remove-objects-that-intersect/45115/8 "2022-11-25T22:40:52Z")

</div>

Thanks for your example. It’s very helpful,  
To be more clear  
In this example, you defined a variable that is a number. To remove instances, how can I address the instances that collide with the model?  
I mean this part:

```javascript
const remove = n => { 
	if(n >= imesh.count) return;
  // delete data from arrays
	mtx.splice(n, 1); 
  clr.splice(n, 1);
.
.
.
get('btn').addEventListener('click', () => { remove(1); render(); });

```

---

<div class="post-metadata">

**Author:** ![Teymoor](https://avatars.discourse-cdn.com/v4/letter/t/ad7895/32.png) [@Teymoor](https://discourse.threejs.org/u/Teymoor)\
**Post date:** [November 25, 2022, 10:41pm UTC](https://discourse.threejs.org/t/remove-objects-that-intersect/45115/9 "2022-11-25T22:41:39Z")

</div>

I want to remove the cubes are intersect with model, not the models

---

<div class="post-metadata">

**Author:** ![tfoller](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@tfoller](https://discourse.threejs.org/u/tfoller)\
**Post date:** [November 26, 2022, 1:31am UTC](https://discourse.threejs.org/t/remove-objects-that-intersect/45115/10 "2022-11-26T01:31:23Z")

</div>

> [@Teymoor](#):
>
> To remove instances, how can I address the instances that collide with the model?

You address them by their index in the instancedMesh, **idx** in your code.

In the example’s function **remove** it’s called **n** and the button click always removes an element with index 1 - **remove(1)**.

To remove multiple elements you need first to strore all **idx** that intersect with your model in an array and then remake **mtx** and **clr** arrays without elements with indexes stored.

As I said it’s a relatively complex program, I can only provide a simple example.
