# Problem with dispose SPS.particles

**URL:** <https://forum.babylonjs.com/t/problem-with-dispose-sps-particles/17018>\
**Category:** Questions\
**Tags:** material\
**Created:** [December 30, 2020, 1:54pm UTC](https://forum.babylonjs.com/t/problem-with-dispose-sps-particles/17018 "2020-12-30T13:54:09Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [December 30, 2020, 1:54pm UTC](https://forum.babylonjs.com/t/problem-with-dispose-sps-particles/17018/1 "2020-12-30T13:54:09Z")

</div>

Hi everyone

I have some cubes and Gizmo box, and I want to deleted the cubes inside the Gizmo box.  
I use following code for my deleted button:

```
selectInButton.onPointerClickObservable.add(function () {
  //force any transformations
  boxG.computeWorldMatrix(true);

  for (let i = 0; i < nb.length; i++) {
    let inX =
      boxG.getBoundingInfo().boundingBox.minimumWorld.x <
        SPS.particles[i]._boundingInfo.boundingBox.minimumWorld.x &&
      SPS.particles[i]._boundingInfo.boundingBox.maximumWorld.x <
        boxG.getBoundingInfo().boundingBox.maximumWorld.x;

    let inY =
      boxG.getBoundingInfo().boundingBox.minimumWorld.y <
        SPS.particles[i]._boundingInfo.boundingBox.minimumWorld.y &&
      SPS.particles[i]._boundingInfo.boundingBox.maximumWorld.y <
        boxG.getBoundingInfo().boundingBox.maximumWorld.y;

    let inZ =
      boxG.getBoundingInfo().boundingBox.minimumWorld.z <
        SPS.particles[i]._boundingInfo.boundingBox.minimumWorld.z &&
      SPS.particles[i]._boundingInfo.boundingBox.maximumWorld.z <
        boxG.getBoundingInfo().boundingBox.maximumWorld.z;

    let inside = inX && inY && inZ;

    if (inside == false) {
      SPS.particles[i].dispose();
    }
  }
});

```

but the problem is when I push the delete button, the cubes didn’t deleted, but I don’t know what is the problem. is it possible help me to solve it

and here is my PG: [https://www.babylonjs-playground.com/#ZEZLAT#64](https://www.babylonjs-playground.com/#ZEZLAT#64)

---

<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:** [December 30, 2020, 3:41pm UTC](https://forum.babylonjs.com/t/problem-with-dispose-sps-particles/17018/2 "2020-12-30T15:41:03Z")

</div>

You should use `nb` and not `nb.length` for the upper bound of your loop. Also, `dispose` does not exist for a particle, try `SPS.removeParticles(indexStart, indexEnd)` instead:

> **[Babylon.js Playground](https://www.babylonjs-playground.com/#ZEZLAT%2365)**

However, I wasn’t able to remove the particles from the display with `removeParticles`, maybe @jerome will have an idea about this (I have also tested with `SPS.particles[i].isVisible = false` but it didn’t work neither).

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [December 30, 2020, 9:58pm UTC](https://forum.babylonjs.com/t/problem-with-dispose-sps-particles/17018/3 "2020-12-30T21:58:51Z")

</div>

> [@Evgeni\_Popov](#):
>
> Babylon.js Playground

Thank you so much for helping me 🙏. sorry for my mistake to use `nb.length`.

---

<div class="post-metadata">

**Author:** ![jerome](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jerome/32/22_2.png) [@jerome](https://forum.babylonjs.com/u/jerome)\
**Post date:** [December 31, 2020, 10:50am UTC](https://forum.babylonjs.com/t/problem-with-dispose-sps-particles/17018/4 "2020-12-31T10:50:22Z")

</div>

If you want to remove (or add) particles from (to) an expandable SPS, you can call `SPS.removeParticles(i,j)` as many times that you need, then you need to tell the SPS to rebuild its geometry at least once to get the final result (it’s not recomputed each to call to `removeParticles()` for performance reasons).  
Finally call again `setParticles()` to set their final positions, rotations, colors, etc.

> **[Babylon.js Playground](https://www.babylonjs-playground.com/#ZEZLAT%2365)**

Everything is explained here : [Expanding Solid Particle Systems | Babylon.js Documentation](https://doc.babylonjs.com/divingDeeper/particles/solid_particle_system/expandable_sps)

When using the property `.isVisible` just do the same : set a particle as invisible and the next call to `setParticle()` will change its rendered status.

> **[Babylon.js Playground](https://www.babylonjs-playground.com/#ZEZLAT%2366)**

Not sure my examples are what you want to really achieve, I just explain the way it works 🙂

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [December 31, 2020, 11:53am UTC](https://forum.babylonjs.com/t/problem-with-dispose-sps-particles/17018/5 "2020-12-31T11:53:29Z")

</div>

> [@jerome](#):
>
> Expanding Solid Particle Systems | Babylon.js Documentation

First of all, Thank you so much for your help. 🙏

I add the `SPS.buildMesh()` and the end of the if statement,

```
    if (inside == false) {
        SPS.removeParticles(i, i);
        num--;
        i--;
        SPS.buildMesh();
    }

```

and now its deleted all the cubes including inside and out side of the Gizmo Box, the problem is how can I deleted the cubes outside the Gizmo box, I mean I just keep the cube inside the box and deleted the cubes outside,

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

if you push the delete button then all the cubes removed.

---

<div class="post-metadata">

**Author:** ![jerome](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jerome/32/22_2.png) [@jerome](https://forum.babylonjs.com/u/jerome)\
**Post date:** [December 31, 2020, 12:15pm UTC](https://forum.babylonjs.com/t/problem-with-dispose-sps-particles/17018/6 "2020-12-31T12:15:32Z")

</div>

don’t forget to set back the particles after the call to buildMesh() : [https://www.babylonjs-playground.com/#ZEZLAT#73](https://www.babylonjs-playground.com/#ZEZLAT#73)  
I don’t know what your code is supposed to do, neither if removeParticles(i,i) is just called once or many times. Your example is quite complex.  
You can find simpler examples of how removing particles works in the documentation link I posted formerly.

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [December 31, 2020, 12:22pm UTC](https://forum.babylonjs.com/t/problem-with-dispose-sps-particles/17018/7 "2020-12-31T12:22:41Z")

</div>

I just want to get the particles boundingbox and if it was in the Gizmo box boundingbox then just removing the particles outside the Gizmo box. I find an example for removing but when I testing on my code, its remove all the particles.

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [December 31, 2020, 3:50pm UTC](https://forum.babylonjs.com/t/problem-with-dispose-sps-particles/17018/8 "2020-12-31T15:50:47Z")

</div>

and also there is another problem that, when I use `console.log("remove", SPS.removeParticles(i, i))` inside the if statement:

```
    if (inside == false) {
        SPS.removeParticles(i, i);
        num--;
        i--;
        SPS.buildMesh();
        SPS.setParticles();
        console.log("remove", SPS.removeParticles(i, i))
    }

```

> **[Babylon.js Playground](https://www.babylonjs-playground.com/#ZEZLAT%2375)**

I got an error which said that `cannot read property '_pos' of undefined`.

 ![Screen Shot 1399-10-11 at 19.20.05](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/1/19680dc8c8ff71ad3f195c29ba1c812c9a7d4a74.png)

---

<div class="post-metadata">

**Author:** ![jerome](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jerome/32/22_2.png) [@jerome](https://forum.babylonjs.com/u/jerome)\
**Post date:** [December 31, 2020, 5:15pm UTC](https://forum.babylonjs.com/t/problem-with-dispose-sps-particles/17018/9 "2020-12-31T17:15:29Z")

</div>

because you’ve removed it 😃  
Please read the doc about how the removing works, that’s quite simple.

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [December 31, 2020, 9:41pm UTC](https://forum.babylonjs.com/t/problem-with-dispose-sps-particles/17018/10 "2020-12-31T21:41:57Z")

</div>

Sorry for my mistake 😅

now on the console everything is ok, I read all the document, but I cannot understand why all the cubes remove from the screen, but as can be seen in the console , 8 cube should remain on the screen.

> **[Babylon.js Playground](https://www.babylonjs-playground.com/#ZEZLAT%2380)**

 ![Screen Shot 1399-10-12 at 01.11.23](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/7/7149a45b4da88d20c0e3eabc60a8aec2637d5a75.png)

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [January 1, 2021, 9:41am UTC](https://forum.babylonjs.com/t/problem-with-dispose-sps-particles/17018/11 "2021-01-01T09:41:04Z")

</div>

Having rebuilt your SPS you need to re-calculate the boundingbox with `SPS.refreshVisibleSize();` see line 368 - [https://www.babylonjs-playground.com/#ZEZLAT#81](https://www.babylonjs-playground.com/#ZEZLAT#81)

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [January 1, 2021, 2:34pm UTC](https://forum.babylonjs.com/t/problem-with-dispose-sps-particles/17018/12 "2021-01-01T14:34:30Z")

</div>

> [@JohnK](#):
>
> [https://www.babylonjs-playground.com/#ZEZLAT#81](https://www.babylonjs-playground.com/#ZEZLAT#81)

thanks a lot and sorry for my mistake 😅 🙏 🙏 🙏
