Help with checking if 2 parts are overlapping

  1. What do you want to achieve? Generate a series of models randomly without them interlapping or making too much of a straight line.

  2. What is the issue? Currently my script generates every model attaching it to certain points to chain them. The issue is that the overlap with one another.

  3. What solutions have you tried so far? I’ve tried checking for the distance between the new model and the latest generated but what came out was a line, while i need to be able to generate a more square-like shape.

Here’s the generation code

function module.Generate(lastGeneration)
	local generable = game.ServerStorage.Generable:GetChildren()		--List of generable items
	local connections
	local nextConnection
	local foundConnection = 0		--Check if it is possble to generate next item
	local timeOut = 0
	local timeOutMax = 4			--Max tries before timeout, has to be equal to the max number of possible connection for any piece 
	local newPart
	
	print("Generation begun")
	
	for i = 1, #generable do		--Get all the generable parts
		print("Generable pieces", generable[i])
	end
	
	local nextPart = generable[math.random(1,#generable)]		--Pick a random part to generate next
	local lastTry = nextPart									--Last part tried
	print("Generating", nextPart)
		
		connections = lastGeneration.Folder:GetChildren()			--Get all the possible connections to the last generated piece
		nextConnection = connections[math.random(1,#connections)]		--Pick the next connection from the last generated
		generable = game.ServerStorage.Generable:GetChildren()
		
		while(foundConnection == 0) do
			
			connections = lastGeneration.Folder:GetChildren()			--Get all the possible connections to the last generated piece
			nextConnection = connections[math.random(1,#connections)]		--Pick the next connection from the last generated
			local lastConTry = nextConnection							--Last connection tried
			
			print("Checking if", nextPart, "part has a", nextConnection, "connection")
			
			if(nextConnection.name == "E" and nextPart.Folder:FindFirstChild("O") ~= nil) then			--Check if the part to connect the newPart to has a valid child (e.g if the connection is O, the newPart has to have an E connection)
				foundConnection = 1
				print("Connection found")
			end
			
			if(nextConnection.name == "S" and nextPart.Folder:FindFirstChild("N") ~= nil) then
				foundConnection = 1
				print("Connection found")
			end
			
			if(nextConnection.name == "O" and nextPart.Folder:FindFirstChild("E") ~= nil) then
				foundConnection = 1
				print("Connection found")
			end
			
			if(nextConnection.name == "N" and nextPart.Folder:FindFirstChild("S") ~= nil) then
				foundConnection = 1
				print("Connection found")
			end
			
			if(foundConnection == 0) then	--Remove unusable connections
				table.remove(connections, table.find(connections, tostring(lastConTry)))
			end
			
			wait(0.001)
			timeOut = timeOut + 1		--If no connection is found increase the timeout counter
			
			if(timeOut == timeOutMax) then		--If timeout is reached, generate another part
				table.remove(generable, table.find(generable, tostring(lastTry)))
				nextPart = generable[math.random(1,#generable)]
				print("No connections found... Retrying with", nextPart)
				timeOut = 0
			end
			
		end
	
		foundConnection = 0
		print("Connecting to", nextConnection)
		
		newPart = nextPart:Clone()		--Different variable to reference that particular part and not another with the same name

		if(nextConnection.name == "E") then		--Check where to attach the next part. If nextConnection is O(vest) then attach it to the newPart's E(ast)
			newPart.PrimaryPart = newPart.Folder.O
			newPart:PivotTo(nextConnection.CFrame)
		end
		
		if(nextConnection.name == "O") then
			newPart.PrimaryPart = newPart.Folder.E
			newPart:PivotTo(nextConnection.CFrame)
		end
		
		if(nextConnection.name == "N") then
			newPart.PrimaryPart = newPart.Folder.S	
			newPart:PivotTo(nextConnection.CFrame)
		end
		
		if(nextConnection.name == "S") then
			newPart.PrimaryPart = newPart.Folder.N
			newPart:PivotTo(nextConnection.CFrame)
		end
	
	newPart.Parent = workspace		--Generate the new part
	return newPart					--Chnage the lastPart to the new generated one
	
end

The result of generating 50 models
image
A lot of them just generated inside another.
How can i check if some are overlapping without creating a line?
So far i’ve tried check if the difference between the position of the new model and the old one was < than a very small number and :GetTouchingParts()

2 Likes

:GetTouchingParts() doesn’t seem to work, instead, you should use workspace:GetPartsInPart().

I’ve always used this function, it works the same way as :GetTouchingParts() however this one allows you to use OverlapParams, which lets you customize which parts can be detected.

2 Likes