I have multiple rigidbodies and I would like to randomize their positions and rotations in a mildly tight space without having them intersect. Is there some clever way to achieve something like that?
For example, in Houdini a rigid body solver has a “ Solve on first frame” option. You can feed it a bunch of overlapping geometry and it will instantly push all the pieces out until they stop intersecting – all on frame 1 before anything animates. An approach like this would be ideal, but I’ll take anything.
As far as I know, there is nothing like that built into Godot. You could build it yourself, but how you do it depends on a couple of open questions.
Are the bodies exactly the same, or have different shapes? Are these simple shapes like a box, or a sphere, or something more complex?
If they’re all the same, you could calculate it mathematically and adjust the positions accordingly.
If they’re all different, you could first check the shapes and then calculate it.
Alternatively, you can use the built in physics engine to simulate physics for a couple of frames at the beginning of the scene at runtime.
Or you can simulate it before you even run the scene with the help of a tool script. And that’s probably the solution I would go for if I had to.
Here’s a similar problem, but with 2D that I solved with a simple simulation in a loop. Your project is a bit more complex, but the principle is the same.
I see. In this case I’m starting to ask myself if it’s even worth it. I’m making a puzzle with rigidbody pieces and I thought about randomizing their positions for every player. But maybe it’s really not that important, I don’t think anybody is going to notice.
I think the approach demonstrated in your code is solid. I could treat my pieces as spheres and similarly loop over them and make them push each other away. Thanks