Detect parts touching while being anchored

so, i have a planet generation script, but the problem is, planets are spawning inside eachother. and that is not good. so far i tried using .touched events, but the planets are anchored and spawned in using .position. so it instantly moves there. any idea how i can tackle this?

local Star = Instance.new("Part")
	Star.Name = name
	Star.Shape = Enum.PartType.Ball
	Star.Anchored = true
	Star.Material = Enum.Material.Glacier
	Star.MaterialVariant = "2022 Big Stud"

	local Size = math.random(MinStarSize,MaxStarSize) -- Set Random size
	Star.Size = Vector3.new(Size,Size,Size) -- Set Random size

	local GPX = math.random(-GalaxyMaxSizeXZ,GalaxyMaxSizeXZ)
	local GPY = math.random(-GalaxyMaxSizeY,GalaxyMaxSizeY)
	local GPZ = math.random(-GalaxyMaxSizeXZ,GalaxyMaxSizeXZ)

	Star.Position = Vector3.new(GPX,GPY,GPZ)

	local Color = math.random(1,#StarColor)
	Star.Color = StarColor[Color]

	Star.Parent = game.Workspace.Universe -- Add the star into the game

	print(name.." GENERATED : POS ",Star.Position)

If it’s just a handful of planets in a vast space, you’d probably be fine with just the naive O(n²) approach of checking each planet you add against all of the planets already spawned, to make sure the sum of the the distance between their centers is greater than the sum of their radii, by however much minimum gap you want.

If you’re spawning hundreds or thousands of objects, this could get too slow, in which case you have to implement some sort of spatial partitioning, like a grid system or octree, to limit the number of comparisons you need to do to those planets already in the general vicinity of where you want to put a new one, rather than all of the planets spawned so far.

In the sparseness of outer space, rejection sampling is fine, because the chances of picking an already occupied space is low. Even still, when you do rejection sampling, it’s always a good idea to build in a timeout so it doesn’t try for too long.

Yeah I’m pretty sure that if a movement update is not sent while parts are touching, the touch event won’t be triggered. It’s a stupid interaction, but a workaround would be to use magnitude to calculate the distance between the two planets. Instead of using the .touched event, you can make it automatically detect whether a planet is within a set distance of one another. In your scenario, you could recalculate a random position to put the planet if it’s too close to a currently-existing one. Experiment with it a bit and get back to me if you need more help.

yeah that would work if it is a real space system, but sadly the game requires planets realtivly close together. so i either have to make it farther away or try to find a good ratio of amount of planets and amount of space