5

I want to generate a random distribution of cylinders inside a cube using geometric nodes as shown in the related figure. The case where I used programming with pyton or matlab, I find the cylinder collision problem is a difficult problem to solve by programming. In the case of nodes used in Blender, their generation is faster, as shown on this page : enter link description here, or one of the members used a great method to generate spheres inside a cube. When changing the spheres with the cylinders in this example, I found a generation problem.

How to generate these cylinders randomly inside a cube without collision? enter image description here enter image description here

saded
  • 793
  • 11
  • 22
  • Of course I forgot about the cylinder rotation, I now realize the complexity of this, not sure if I can answer, or if it's possible in blender, but i will try. – Hulifier May 03 '22 at 19:46
  • The things we would be able to do with loops in Geometry nodes... – Hulifier May 03 '22 at 19:54
  • This won't work 100% and the ends of the cylinders might be inaccurate, but in general I think you can generate cylinders with double thickness, then raycast along each cylinder with those double thickness geometry, and if you hit a cylinder with instance index lower than self, remove self. – Markus von Broady May 03 '22 at 21:43
  • The random distribution of non-overlapping cylinders depends on the use of euler angles (or spherical angle). I think there are these angles in geometry nodes. – saded May 04 '22 at 19:01
  • Yes, there is a Vector: Rotate node, but it's not clear how this helps you. – Markus von Broady May 04 '22 at 20:02
  • @saded - do you happen to have the MATLAB code you originally used? I am looking to do something similar – Sam Bottum Nov 29 '22 at 18:14

1 Answers1

3

Here's a node setup that removes overlapping Cylinders:

The Custom Group:

Unfortunately increasing the number of cylinders eventually decreases the number of resulting cylinders. The reason for that is that even if the algorithm decides to not spawn cylinder A, because it overlaps with a cylinder B, the cylinder A still is on the raycasting geometry, so the cylinder B will overlap the cylinder A and therefore it also won't spawn. So eventually there's so many cylinders it's very unlikely for a cylinder to not overlap anything:

I tested a solution I mentioned in the comment, where I connect Index attribute to the Raycast node in the Nearest Integer mode, and then compare it with current Index multiplied by the number of loop faces of the cylinder (Domain Size, need to extract single instance geometry from within the custom group as output), and then compare those two indices - if the hit index is larger, current cylinder is the one with lower index and so it can be left. However, there's a huge problem with that: raycast doesn't give you a list of hit geometry, only the first one. It's therefore possible that a Cylinder #3 will hit a Cylinder #7, therefore you will decide to leave it in, but perhaps without Cylinder #7 the ray would hit a Cylinder #1... So with many cylinders you actually end up with collisions.

Below is roughly as dense setup of cylinders as I could get without collisions:

Markus von Broady
  • 36,563
  • 3
  • 30
  • 99
  • It's a good idea, in terms of guiding cylinders, there are two types of geometry, short cylinders where these cylinders are in the cube, and long cylinders where these cylinders cut the cube (ie the height is greater than the cube's length). You have used two angles but the direction of these cylinders are the same, and you also have the value of the second angle: absolute value(sin(radians(frame2)))45. In the cylinder geometry, the vertices are not present, and also when I add set shade smooth, it cannot show the forme of cylinders. [1] [1]: https://i.stack.imgur.com/z6rdo.png – saded May 05 '22 at 11:57
  • 1
    @saded I addressed the main problem of your question, removing colliding cylinders. As for your other problems: non-constant lengths of cylinders don't affect the solution (varying radius would, for that I could come up with a different solution). As for two angles, it's a range of the random values, on the screenshot it's between 0 and 360°, in the file it's a driver that animates the cylinders. To get vertices, realize instances, and then you can use "Edge Split" modifier below geonodes to get a sharp edge. Alternatively you can Edge Split in geonodes: https://i.imgur.com/Qd4dkhR.png – Markus von Broady May 05 '22 at 13:18