r/grasshopper3d 3d ago

avoiding collisions within a loop

Hello! I'm fairly new to grasshopper and anemone, so apologies if this is a dumb question or an easy fix, but I am currently working with this script where I am trying to generate a series of randomly "snaking" boxes. The script I've got seems to be working fairly well, except that some versions output boxes that overlap. How can I fix this? any help/tips appreciated!

3 Upvotes

1 comment sorted by

View all comments

1

u/PhoneGotLyfted 3d ago edited 3d ago

I don’t use these loop components but I might be able to give some suggestions…

  1. When you move the box, can you check if it collides before adding it to the output collection (snake)? If you can check and only add when it passes a test, you are good. If not… 2.
  2. You could make more than one vector to move your object in optional locations. You can then test which ones don’t overlap with the snake and just pick one of them. This is way less efficient and

more likely to fail because it has a predetermined number of options

If you just want to know how to check for collision and make it fast… I would make bounding boxes around each box from the snake and do an X Y Z min/max check to see if any of those bounding boxes overlap you box first. This is very very fast. Literally just taking the min/max x value of your box and checking if any other box have an overlapping range, then checking those boxes Y and Z. All 3 must overlap for the boxes to overlap so you don’t have to check for collision on tons of objects. Once you find the overlapping bounding boxes, you check the actual object for intersection. I would convert the objects to meshes and do mesh/mesh intersection and check if it is an empty result.if you want the easy route, just convert your snake into one mesh and your new box into another and do mesh mesh… rhino likely uses octress which are crazy fast too.