Problem with dispose SPS.particles

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

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:

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).

2 Likes

Thank you so much for helping me :pray:. sorry for my mistake to use nb.length.

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.

Everything is explained here : Expanding Solid Particle Systems | Babylon.js Documentation

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.

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

2 Likes

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

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,

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

don’t forget to set back the particles after the call to buildMesh() : 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.

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.

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))
    }

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

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

1 Like

Sorry for my mistake :sweat_smile:

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.

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

3 Likes

thanks a lot and sorry for my mistake :sweat_smile: :pray: :pray: :pray: