Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I check if a part is touching another part?

So, I have a part that gets created like a selection box, which can be found here .

Since I'm using this as a selection box, I need to find out what parts it's touching. I don't want to use GetTouchingParts because the parts that are to be selected can't be canCollide true.

local mags = {}
for i,v in pairs(game.Workspace.Map:GetChildren()) do
	if v.Name == "Cell" then
		local mag = (v.Position-part.Position-part.Size).magnitude
		table.insert(mags,#mags+1,mag)
		print(mag)
	end
end -- I also tried taking out -part.Size

The above isn't reliable. Anybody have any ideas? Any help would be appreciated.

2 comments

0

Try basic collision detection, manually. Check if both the x or y position is colliding with the x or y position of the other brick.

0

have you thought about using a touch event?

1 answer

I actually ran around writing a bunch of code for testing if a point is inside a part using dot products only to find this chunk of code by Anaminus

local function PointInsidePart(Part, Point)
	--- Return's whether a point is inside of a part.
	-- @param Part The part to check. May also be a table with a .Size and .CFrame value in it.
	
	local PartSize = Part.Size/2
	local RelativePosition = Part.CFrame:pointToObjectSpace(Point)
	--print(RelativePosition, PartSize)
	if not (RelativePosition.X >= -PartSize.X and RelativePosition.X <= PartSize.X) then
		return false
	elseif not (RelativePosition.Y >= -PartSize.Y and RelativePosition.Y <= PartSize.Y) then
		return false
	elseif not (RelativePosition.Z >= -PartSize.Z and RelativePosition.Z <= PartSize.Z) then
		return false
	end

	return true	
end

Anyway, here is the full code with some comments for explanation on my code

local function getVertices(part)
	local pos 	= part.Position
	local size 	= part.Size
	local cf	= part.CFrame
	local vertices = {}
	
	-- get all 8 vertices
	vertices[1]  = cf * CFrame.new(size.x*0.5, size.y*0.5, size.z*0.5)
	vertices[2]  = cf * CFrame.new(size.x*-0.5, size.y*0.5, size.z*0.5)
	vertices[3]  = cf * CFrame.new(size.x*0.5, size.y*-0.5, size.z*0.5)
	vertices[4]  = cf * CFrame.new(size.x*0.5, size.y*0.5, size.z*-0.5)
	vertices[5]  = cf * CFrame.new(size.x*-0.5, size.y*-0.5, size.z*0.5)
	vertices[6]  = cf * CFrame.new(size.x*0.5, size.y*-0.5, size.z*-0.5)
	vertices[7]  = cf * CFrame.new(size.x*-0.5, size.y*0.5, size.z*-0.5)
	vertices[8]  = cf * CFrame.new(size.x*-0.5, size.y*-0.5, size.z*-0.5)
	return vertices -- return them
end

local function PointInsidePart(Part, Point) -- Credit for this goes to anaminus
	--- Return's whether a point is inside of a part.
	-- @param Part The part to check. May also be a table with a .Size and .CFrame value in it.
	
	local PartSize = Part.Size/2
	local RelativePosition = Part.CFrame:pointToObjectSpace(Point)
	--print(RelativePosition, PartSize)
	if not (RelativePosition.X >= -PartSize.X and RelativePosition.X <= PartSize.X) then
		return false
	elseif not (RelativePosition.Y >= -PartSize.Y and RelativePosition.Y <= PartSize.Y) then
		return false
	elseif not (RelativePosition.Z >= -PartSize.Z and RelativePosition.Z <= PartSize.Z) then
		return false
	end

	return true	
end


function doPartsCollide(part0, part1)
	local vertices = getVertices(part0) -- get all the vertices (corners) of our part
	for _, vertex in pairs(vertices) do -- iterate
		if PointInsidePart(part1, vertex.p) then -- if point is inside
			return true -- part collided
		end
	end
	return false -- didn't collide
end

We get all the vertices by using the size vector and the position. It's in cframes to account for rotation.

Anaminus' code is very elegant and compares the size of the part and position(of the point in object space) to determine whether or not the point is outside of the part. Returning true if it isn't.

The we get an array of the first part's vertices and iterate through it. We check if this vertex is inside the other part and return true if it is.

I haven't slept in a while, so sorry if there are any mistakes.

This discussion is preserved as it was. New answers, comments, and votes are closed.