How to create collision spheres that do not overlap.

Inside an actor, I want to have a “for each loop” that creates a collision sphere for each item in the loop.
Checks if the sphere is overlapping a different sphere and if so delete and try again.

The goal is to have a number of collision spheres that do not overlap.

I have this code:

But it is giving tons of errors:

Blueprint Runtime Error: “Accessed None trying to read property CallFunc_Array_Get_Item”. Node: Branch Graph: UnitMove Function: Unit Move Blueprint: BP_UnitController

And

Blueprint Runtime Error: “Attempted to access NODE_AddSphereComponent-0_181 via property CallFunc_Array_Get_Item, but NODE_AddSphereComponent-0_181 is not valid (pending kill or garbage)”. Node: Branch Graph: UnitMove Function: Unit Move Blueprint: BP_UnitController

While still getting overlapped spheres.

Do you know how I can do it?

There’s something very ‘whacky’ going on in that code, to do with the loops, but the main way to avoid overlapping objects, is to keep an array of the positions used, and run through it each time checking for nearby objects.

Just write a function to check if the point you’re about to use is near one you’ve already used. Then, you can just consult it before placing one.

I was able to create this result:

Using this blueprint:

Where the “LowestDistanceToAllInArray” function looked like this:

2 Likes

Thank you, it works very well. Added a clear Current Locations to the beginning and I’m now trying to figure out how to avoid an infinite loop if the overlaped positions don’t have space to be created.

With the changes outlined in red I got no infinite loop problems even if the spheres don’t fit in to the radius of the “GetRandomPointInNavigableRadius”

Result with trying to place 25 spheres of radius 25 into a area with a radius of 100:
image

2 Likes

That’s the result that I arrived at thank you for your time!

1 Like