Dynamically add/remove objects from jit.phys.multiple
Hello 👋
I am trying to re-create the op-1 tombola sequencer using jit.phys.
I have created the sides of the tombola just fine and have a jit.phys.multiple creating the balls inside.
My issue now is adding / removing the balls from the phys.multiple.
I don't seem to be able to change the dimensions of the position matrix without resetting the other objects in the phys.multiple.
I thought I might be able to use jit.matrixset with jit.concat to add a cell to the end of the current position (fig 1) but this still reset the velocities of the other objects.

So basically the end goal is to be able to add an object to jit.phys.multiple and also to be able to specify the index of an object to remove, both of these actions without effecting the other objects in the jit.phys.multiple.
Thanks for your help!
Hey Joel,
one solution could be to use jit.submatrix:
you choose in advance the maximum number of objects you want to have, and then to add a new one you just increase the dim of jit.submatrix
an alternative approach, somewhat similar to what federico suggests is to instantiate all bodies "offscreen" and then use the sendbody_reset message to add them to the scene (with all forces zeroed out)
Thank you both for your suggestions!
Regarding Federico's approach, I wouldn't want to have the excess phys.body objects in the scene even if they are not being rendered as they would interfere with the collisions of the other balls and report unwanted collision detections.
I did consider having all the objects just sitting off screen and then resetting them to the centre when required, but I suppose I was hoping for a more elegant solution here that would not involve hard-coding the amount of objects. I was also hoping to save on computational overhead by not including unused objects in the scene.
I also need to be able to remove a specific object from the world once it collides with the world_box, this would appear to be possible using the sendbody method however we then still have the issue of rendering unused objects.
At the moment this only seems possible using jit.phys.body in a poly~, but I would greatly appreciate any other suggestions for making this possible with phys.multiple 🙏
One way to have this without having the actual objects in the scene, would be to use JS.
You could have an array of objects with physbodys attached, and add more of them dynamically to the scene.
This would be, for me personally, a much easier approach than using poly
That's an interesting proposal Federico!
I've been trying my hardest to steer clear from JS but it seems like it may be my only option here 😅
I don't suppose it would possible to create the same functionality in Gen?
If not, would you happen to have any tutorials or examples of how you would go about that in JS, I wouldn't know where to begin 😬
in additions to Federico's js tutorials there are the phys.world.collisions.js and phys.ragdoll.launch (search the file browser) example patches that demonstrate jit.phys from js.
Thanks again for you help both, for now I have settled on a 'solution' similar to Rob's suggestion.
I have made sure that the balls are not colliding with anything and are asleep when they are not in use (do either of you know what sleep actually does at a lower level? I can only assume it reduces load when they're not in use).
I'll happily take any suggestions to improve my implementation!
For anyone wondering in the future, using a poly~ turned out to be the best solution here.
With this implementation I managed to greatly simplify the midi routing as each note just went to each poly and then collisions are dealt with on a per body basis (colliding with the wall resets the ball, collisions with anything else triggers the note).
It also allows easy scaling of the maximum number of balls as you can just change the number of voices in the poly~.