Adding single shapes to SPS cause odd problem

So I changed my approached for creating a 3d cube (Related to my other post here, Help creating complex GUI)

I am trying to do the following:

  1. a cube moves in direction X each frame
  2. if cube crosses threshold, create another cube next to it, with no space in between.
  3. move both cubes in same direction (aka parenting)

Very weird, it kind of worked with only 3 blocks, but once I put 4, or 5+, it goes into a infinite loop, or places blocks in wrong places.
I had this working perfectly with 3 cubes, but any more, and things start to go haywire, and I am not able to debug it. Is there some way to easier debug a SPS?

https://playground.babylonjs.com/#GSB0ZC#1 (use the “A” button to do a frame at a time)

pinging @jerome

Could you please simplify the code and could you try to isolate the problem so we could check if there’s a bug ?
You’ve added some new properties to the particle object and you’re testing a condition of your own in your loop, so we can’t easily check if it’s a problem related to your logic or a bug in the SPS.

Let me rather experiment some more.
I see now that trying call a SPS update within a SPS update is a sure way to cause infinite loops, and also a bit unnecessary.
Side note, how “expensive” is it to add/remove particles?
Is it better to just make them before and position them accordingly, or is it ok to rebuild the mesh on each SPS update call?

Edit: Is there any way to use the when using Addshape to set the parentId value using something like the positionFunction? Using the current positionFunction function, I need to create blocks one by one to achieve this.
**Most likely I will need to switch to using a storage…

Each time you call sps.buildMesh() the SPS is rebuilt, meaning the particle additions or removals take actually effect only on this call. Some of the underlying buffers are destroyed and recreated, so this could eventually lead to some GC extra activity if you call it each frame.
If you don’t call it each frame, this side effect will be quite unlikeky noticeable.
The “cost” of rebuilding the SPS is directly related to the final geometry as it iterates over every particle vertex. In your case (dozen of boxes), it’s negligible. I suppose nothing would actually be noticeable under 10K vertices or so.

Unfortunately you can’t set the parentId in the call to AddShape() for now. I’ll check in the next months in this could be added regarding the global consistency.