I have created a random distribution of spheres inside a cube with Geometry Nodes, as shown in the image below. My problem is that I don't know how to specify the number of spheres with their radius inside the cube, without them overlapping each other and the walls of the cube.

How can I set these parameters (number and radius of the spheres) so that they do not overlap?



1 Answer 1



Spheres inside cube no overlap

Changing radius:

Changing radius

Changing quality (higher is slower): changing quality

Geometry Nodes:

Geometry Nodes

Geometry Nodes inputs

Since the min distance problem can be solved by just plugging the sphere diameter in the Distance Min input from the Distribute Points on Faces node, what we need to do is just generate planes that are away from the cube surface by a value equal to the sphere's radius.

planes for point distribution

We also check if any of the cube's dimensions is smaller than the sphere's diameter. If so, we don't output any sphere.


The idea is to use the node Distribute Points on Faces to generate points in a cube volume, the reason for using this node is that it has an option for minimum distance when in Poisson-disk mode (I believe it does that by generating the points and then deleting the invalid ones), which we can use to keep spheres from intersecting.

Since the node Distribute Points on Faces only distributes on faces, we can simulate a volume with many layers of planes inside the volume. With a bigger layer count, more possible positions are available to points, and thus points tend to be more closer to each other, but always obeying the minimum distance.

to eliminate the need to check and delete spheres intersecting with the bounding cube, the layers can be created with edges far from the cube bounds by the desired radius, that way spheres will at most touch the bounding cube.

node tree

Detailed Steps

First a vertical line is created centered to the bounding cube, it's length is $w - 2r$, where $w$ is the bounding cube's size, and $r$ is the desired radius for the spheres.

generated line

Then, planes with dimensions equal to $w - r$ are instanced equally spaced along the line. Spheres generated on them will be more closer to each other as the number of planes increase.

instanced planes

On the generated planes, points are distributed with a minimum distance of $2r$. (points of all planes are considered when generating them, so the minimum distance also applies between points of different layers).

After that just instance spheres with radius $r$ on the distributed points;

I also check if a sphere of the desired radius can fit in the bounding cube before outputting the geometry, since a single sphere is still created by this method when the radius value meet that condition.

single sphere bigger than bounds is generated by the node tree node tree that outputs no geometry when radius is too big for fitting in the cube

  • 1
    $\begingroup$ Oh, interesting trick, well done! $\endgroup$
    – quellenform
    May 3, 2022 at 15:15
  • $\begingroup$ This is amazing, thank you very much. In your case, How can I control the number of spheres with their radii (ie it's me that I set the number displayed). For the case of cylinders, can we generate cylinders without overlap or the cylinder is defined by its radius and its height (the distance of non-overlap is defined by the distance between two bases of two close cylinders. $\endgroup$
    – saded
    May 3, 2022 at 17:50
  • 1
    $\begingroup$ @saded I may be misunderstanding you (Sometimes my english fails me), but in case I understood: the sphere radius is controlled by a custom input named radius in the Geometry nodes, this method doesn't tightly pack the spheres, but you can increase the quality input to get closer. Will add gifs to the answer. Now for cylinders, there's probably a way to do, and is probably more easier to tightly pack them, but the way to do will be different from this one and I don't know if it needs to be in a separate question. $\endgroup$
    – Hulifier
    May 3, 2022 at 18:18
  • $\begingroup$ Thank you very much for the pictures shared. Regarding the random distribution of cylinders, I will add a new question. $\endgroup$
    – saded
    May 3, 2022 at 19:00
  • $\begingroup$ Thanks Hulifier for the interesting explanation, Regarding the bar of Radius, Quality and Density Max, is there a mathematical relationship between them? I want to plot also the Radial Distribution Function for Hard Spheres curve g(r) (demonstrations.wolfram.com/…). $\endgroup$
    – saded
    Aug 26, 2023 at 11:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .