Animation nodes possible to fill object with others- idea

I’m not quite sure what animation nodes can do yet. I’m trying to think of a fast way to fill a volume with objects. Rigid body simulation you say? Yeah, it’s Really slow if you need thousands. Current progress is I have a particle system emitting in a volume all at once. Zero gravity.
Then I’m using AN to instance the object, give them all random rotations since it seems the only info from particles data is location and if it’s Alive or other. Created a loop input to adjust rotation random. Remember, I’m not animating here. I am Just trying to get the object filled. I assume it wouldn’t be difficult to also randomize the scale just a bit as well using the same method.

So I add a Rigid body active to the filling object, and a passive to a copy of the volume which I turn inside out. The objects that overlap cause a bit of an explosion, and they are limited by the outside volume. Perfect. Only…it’s still a rigid body simulation, so Slow. But it’s at least Much faster than the methods I’ve seen on youtube where you make a funnel and just have a ton of duplicates from above.

So this got me thinking:
Is there a way, with animation nodes, to sort of create your Own simulation? Assuming the volume isn’t complex, then you could create objects one at a time and run them through a loop where the object moves in set, user defined, increments until it touches the volume surface, then step back one and stop. Repeat at user defined angle changes through 360 deg. Then repeat again until it touches the objects already positioned.

Basically it’s just shooting objects out doing a Very basic simulation with no bounciness or calculations considering velocity. I don’t Think there is a way to calculate if two objects have touched, but you should be able to figure that out if the objects and the volume are both solid because you can check if is inside? or is there a boolean result?

I know this method isn’t perfect, but it’s a lot better than trying the method I’m using because what I’m currently looking at, I would need approximately 9000 objects inside. And as soon as I get the volume Close to filling, there are too many initially overlapping pieces so it takes Forever (if at all) to initially pull apart. But using this other method it would fill outside in, so if you just need the appearance of an object filled, you can stop after 3 or 4 layers.

I guess it will end up taking more time to solve this with animation nodes than doing it with dynamics. I’ve done some dynamic simulations with more than 4000 pieces and it wasn’t that slow. In your case I would fake it or set it up in several steps.

First drop like 500 pieces or somerhing that feels quick enough. Freeze and join all objects to one mesh. Now you can drop 500 more on top of the first 500(that now is only a collision mesh) and then freeze and join. Then you repeat until you have filled the object.

I hope this make some sense.

Yes, that does make good sense actually. And thanks for the reply. I’ve not been getting replies lately.

I tried the molecular add-on as well, and holy Smokes! That is pretty darn slick. If you have sphere, or near sphere objects, then that is Definitely the way to go. The guy’s site is down, and I can’t find documentation or anything, but the best I can tell it only works with a spherical volume…which would make sense, since it’s called molecular.
I tried it with a crudely modelled coffee bean and it worked better than I was getting with simulation, and much faster. But I quick modelled something that resembled penne pasta and it didn’t do so great mixed with some suzzannes to try and push it a bit.
After simulating, I quick went into edit mode and added a sphere along with the penne without resimulating and looked back at the particles. Sure enough, it’s using a spherical volume to simulate.
If there’s a way around that, it could Definitely save some time.

In an upcoming project, if I can’t get away with just photoshopping an image (that’s likely how I’ll do it) then I’ll probably need to fill something opaque with coffee beans. So really I just need to see the top. But I’d want to do that with maybe a dozen different beans so that it’s nicely randomized. I can’t think of a quick way to get a grid of those to start simulating with. Although, it’s really not that difficult to make something seem random by just having all 12 in a row, duplicating those end to end a few times. Duplicating Those and rotating them 180, do that a few times and you’ve got yourself a pretty good mix of pieces.
I’m looking at other future projects as well though. Even if a simulation takes a couple hours, if I can just open the scene, replace the pieces, and replace the container and hit go then that’s a lot of time saved.

It’s not the bounciness, or the mass, or the friction that makes rigid body physics slow. It is the collision. All the other stuff gets calculated super fast. But collision is a permutative problem. Adding more collision bodies-- like 4000?-- isn’t 4000 times slower than 1 collision body. It is vastly slower. (Too much slower for me to bothered to calculate exactly how much…)

Yet despite all that, collision detection is the thing that rigid body physics is optimized for. It’s just a hard problem.

I don’t think you’re going to fill an object faster using animation nodes than you are using rigid body physics. Rigid body physics is the way to do it. If rigid body physics is running too slowly for you, then try to improve the speed of your rigid body physics.

How to optimize rigid body physics? Like I said, bounciness, mass, friction-- none of those things improve speed. Instead, work on things that improve collision detection. The first thing to do is to avoid “mesh” collision. The second thing to do is to avoid “convex hull” collision. (The best collision is probably sphere, with box following closely behind, but other things like capsule isn’t bad.) The second thing to do is to limit the extent of your rigid body physics by using collision collections. The third thing to do is to make as many of your rigid body objects into “passive” simulations as you possibly can. The fourth thing to do is to reduce the “steps per second” in properties/scene/rigid body world.

Ha, I like your order because All of the first things that make things slow are necessary for doing something like filling a glass container with pasta pieces. The glass container itself has to be passive mesh (hey, at least it’s passive right?) and all the pasta pieces have to be at Least convex hull. Something like bowtie pasta, or macaroni, would probably need to be mesh, granted you could use a simplified model and replace it later.

For my example I’ll be doing coffee beans. So really I can probably get away with using pill and having it close enough. I didn’t realize you can’t change the orientation. You just have to rotate your object in edit mode? Seems silly, but that works.
I’m going to just Create a bunch of objects from 8-12 bean shapes. Then go back and replace the really basic shape I make with better models afterward.

I’ll try and see if pill works well. Then if it’s not good enough, I’ll switch the bottom layers to passive and do another layer as convex hull, if needed. If it’s just a few pieces that look odd, then I’d say screw it and just manually reposition.

I’m still keeping my eye out for a quick “fool proof” way to fill a shape with another shape randomly without them intersecting.

I don’t entirely agree with saying that bounciness, mass, etc not effecting the simulation though, because if the first objects just fall and stop, then there is less interaction to calculate. You’ll have less collisions in the air right? It would be awesome if they built in a threshold for switching an object to passive if it’s movement/rotation change is within a certain threshold. That would probably speed up filling up objects dramatically since it would be somewhat automating what you’ve described. And if you turn off bounce, the first layer would become passive as soon as it has 3 points touching the bottom of your container. High friction, no bounce would speed that up because as soon as it got down there it would switch to passive.

I just wanted to provide an update in case anyone else was looking for this as well. I still haven’t found a great solution, but some things that are better for sure.
First, one thing that tutorials never tell you is Why they tell you a size. The simulations are always Huge right? Like, “Hey, lets make a bunch of 2 meter balls and have them bounce around in this 700meter wide room.”

I’ve tried a Bunch of things to get correctly sized objects to fall into containers and I always run into issues. I’ve even turned gravity way down, made sure the mass of each object is accurate. Nothing. Things bounce, explode, fly through, etc.
Take the Exact same scene, and just use meters for the units instead of millimeters, scaling everything up, and just returning gravity back to normal? And everything is fine.

So my guess is Something, here and there, doesn’t scale correctly. So just scale to meters as your unit, then scale it down later. So if you your object is 4mm, then make it 4meters. Yup, that’s 1000 times bigger. So quite annoying. My Guess is it wants most objects to be within a certain range also. So if your objects are 12 inches, and you’re throwing things into a 5ft dumpster, perhaps you’d want to scale it closer to only 300%, if it’s even needed. That might be within the range.

I used molecular to get pretty dang close. And that’s pretty fast. When you’re finished, you can just convert the particles, delete the emitter, and your animation is baked. Easy. AND you get to keep instances.
If you run an actual simulation with instances, I couldn’t figure out how to freeze things, because it wouldn’t let me apply scale and transforms to the objects because they were instances. That was frustrating. And I wanted to use instances because, well, lots of those objects are the same. But I also wanted to run the simulation on geometry that was simplified a bit. For some reason, convex didn’t seem Any faster than model, and also caused things to fly all around. It’s a coffee bean, sort of. It was a Very crudely shaped coffee bean out of 12 faces. Definitely already convex. I tried to make it as fast as possible.

So the reason I was using particles was to easily update and recreate everything.
I still like the idea of using a particle system to start, and having animation nodes replicate the particles. That worked pretty well, and you can get some random scale, rotation, and position. And either you can do a volume and create a grid all at one frame, or you can have it just continually pump out objects from a collection. And you can still run a rigid body simulation. So I may try that one out again.

If I had time I would have also tried out blender alpha, which right now has a method for using multiple objects to define the collision cage, but not sure that would really benefit something like a coffee bean much. I think you’d get a pretty darn good simulation by using a box in the middle, and a sphere at either end though. But that’s rather specific to this use case. I’ll probably fill things with other objects later, so want to know reasonably efficient ways to accomplish it. There’s nothing worse that thinking something will take about 2 hours to do, then not even finish in a full day.

The main thing that scaling it up does is reduce the force of gravity. That means that forces are smaller relative to the size of objects, and so velocities are smaller relative to the size of objects. The way rigid body physics work is that it samples the state at intervals, calculates collision, and then fixes things out of intersection. What happens if you sample at a point where two objects intersect heavily? Things fly apart at high velocity. What happens if you don’t sample often enough? Things pass through each other. It’s not reality, it’s Blender RB physics, which operate according to a different set of rules-- a set of rules chosen largely for speed of calculation. The solution is to increase physics steps, reduce forces, and/or increase sizes. Or, to just cheat it with constraints or something. (Not for thousands of objects though.)

That’s basically what capsule collision is. (Well, a cylinder with a sphere at either end.)

Well that’s what I thought, so I just scale gravity way down. I only needed the end state of a filled object, so I don’t care how slow things are. The way molecular works is basically that cheat actually. Since you can’t change the steps for particles, molecular takes care of it. The default of 4 steps just temporarily increases the entire timeline by 4x. Then it converts it all back afterward. But molecular was where the scale had big issues. When objects “started” collided, things wildly explode. Which makes sense.
Also, he IS doing constraints for thousands of objects, quite well actually. He’s using a distance search for building and breaking constraints. It’s really quite smart. You could tell he had to cheat in several places to get things working as well as they do. I really hope that project isn’t abandoned. His site continues to be completely down.

So the thing I had issues with capsule was the size. A coffee bean is rather flat-ish. The box scales well to fit the bounding box of your object in any orientation, but the other modes only scale uniformly. If sphere would scale to use a best-fit method to scale non-uniformly then it would be good enough for sure. I can’t figure out how to modify those in any way though. Is there a secret way to sort of position/edit the shape it uses? Even if it’s just uniform scale adjusting, I’d like to see the piece and decide how it is positioned myself.

For things like this, I’ve actually decided to just use the drop-it add-on. It’s Pretty darn close to exactly what I need. Create a bunch of objects, randomize their position…drop it. For a container to be filled, just angle the container around a bunch of times and repeat until you have pretty good coverage.
It would be amazing if there was a good way to do a physics simulation on that after your done to just slowly push them all apart from each other to help sort of settle them. But really, for opaque objects only the outer objects matter, so you can just kind of manually adjust them.

Thanks for the reply on the scale thing. It would be awesome if someone did a video tutorial just showing how scale effects the numbers, and how to “fix” them. I put that in quotes, because Sure, you could just throw a Bunch of steps, super low gravity, etc and it’ll likely work Eventually. Well, you might hit a memory limit right away if you go crazy though.

I’ll probably go back to the animation nodes method I was using after I figure out a good scale though. It worked quite well and was pretty darn fast. It would be even faster if I could switch an object to passive after a certain amount of time, or after a certain amount of movement or something like that though.

Mostly, no. The shapes are actual mathematical definitions-- sphere collision is at the surface of all points at a given distance from the origin. That’s why they’re faster. To check sphere collision, it can just expand the other body by the radius of the sphere, and then check for if the sphere’s center lies inside this volume. Which is a much simpler problem than volume intersection.

If you just want to scale, it’s the mesh’s bounding box that determines the size. Spheres can only be scaled uniformly, but cubes have three axes of freedom (they’re actually rectangular prisms), capsules and cylinders have two axes of freedom (length and radius). Scale in edit mode, or apply scale afterwards. Consider using a wireframe view: it’ll show you the collision shape in wire, but it might be inside the mesh.

Thanks for the help. It’s obviously an ongoing process, learning the ins and outs of all the things in blender. And by the time you think you have One thing figured out…it’s updated and they changed it. lol
I like advancement of course, but the changes can Definitely be daunting and often making tutorials Quickly fairly useless if the person isn’t great at designing them. And as you likely know, most aren’t. Useful information of course, but often times it’s 30 minutes of junk and 1 minute of useful information.

If I figure out some great new thing, I’ll probably ask about it here and hopefully post a file also so people can say “why the heck did you do This thing when you could have used this other thing that is much more flexible and appropriate for what you’re doing?” Always good feedback. Although, sometimes it’s…“you shouldn’t do that.” which isn’t helpful, given no alternative. But you’ve been very helpful. Thanks

1 Like

Have you looked into using addons? This one seems like it could help you.–dynamic-pile-generator?ref=2

Oh, well geez. Yeah, that looks like the kind of tool I am looking for. Thanks