Area detection isn't working properly

Hi guys, I am currently testing some ‘area’ detection using the part corners to detect if it’s inside/out other parts corners.

But when rotating the box part it doesn’t works anymore.


Code;
Ppart is the moving part, transparent is the box

local x2 = {};

local Ppart = workspace.furniture;
local Box = workspace.box;

while wait(.1) do

	local x = {}


	local CornerX1 = Ppart.CFrame * CFrame.new(Ppart.Size.X/2,Ppart.Size.Y/2,Ppart.Size.Z/2)
	local CornerX2 = Ppart.CFrame * CFrame.new(Ppart.Size.X/2,Ppart.Size.Y/2,-Ppart.Size.Z/2)
	local CornerX3 = Ppart.CFrame * CFrame.new(-Ppart.Size.X/2,Ppart.Size.Y/2,Ppart.Size.Z/2)
	local CornerX4 = Ppart.CFrame * CFrame.new(-Ppart.Size.X/2,Ppart.Size.Y/2,-Ppart.Size.Z/2)

	local CornerTopX1 = Ppart.CFrame * CFrame.new(Ppart.Size.X/2,-Ppart.Size.Y/2,Ppart.Size.Z/2)
	local CornerTopX2 = Ppart.CFrame * CFrame.new(Ppart.Size.X/2,-Ppart.Size.Y/2,-Ppart.Size.Z/2)
	local CornerTopX3 = Ppart.CFrame * CFrame.new(-Ppart.Size.X/2,-Ppart.Size.Y/2,Ppart.Size.Z/2)
	local CornerTopX4 = Ppart.CFrame * CFrame.new(-Ppart.Size.X/2,-Ppart.Size.Y/2,-Ppart.Size.Z/2)

	table.insert(x,CornerX1)
	table.insert(x,CornerX2)
	table.insert(x,CornerX3)
	table.insert(x,CornerX4)

	workspace.ShotPart:ClearAllChildren()

	for i,v in pairs(x) do
		local part = Instance.new("Part")
		part.Size = Vector3.new(.5,.5,.5)
		part.Material = Enum.Material.Neon;
		part.BrickColor = BrickColor.Green()
		part.CFrame = v;
		part.Shape = Enum.PartType.Ball;
		part.Parent = workspace.ShotPart;
		part.Anchored = true;
		part.Name = v == CornerX1 and 'cornerX1' or v == CornerX2 and 'CornerX2' or v == CornerX3 and 'CornerX3' or v == CornerX4 and 'CornerX4'
	end
	
	local BoxX1 = Box.CFrame * CFrame.new(Box.Size.X/2,Box.Size.Y/2,Box.Size.Z/2)
	local BoxX2 = Box.CFrame * CFrame.new(Box.Size.X/2,Box.Size.Y/2,-Box.Size.Z/2)
	local BoxX3 = Box.CFrame * CFrame.new(-Box.Size.X/2,Box.Size.Y/2,Box.Size.Z/2)
	local BoxX4 = Box.CFrame * CFrame.new(-Box.Size.X/2,Box.Size.Y/2,-Box.Size.Z/2)

	local BoxTopX1 = Box.CFrame * CFrame.new(Box.Size.X/2,-Box.Size.Y/2,Box.Size.Z/2)
	local BoxTopX2 = Box.CFrame * CFrame.new(Box.Size.X/2,-Box.Size.Y/2,-Box.Size.Z/2)
	local BoxTopX3 = Box.CFrame * CFrame.new(-Box.Size.X/2,-Box.Size.Y/2,Box.Size.Z/2)
	local BoxTopX4 = Box.CFrame * CFrame.new(-Box.Size.X/2,-Box.Size.Y/2,-Box.Size.Z/2)

	local Xbool = (CornerX1.Position.X <= BoxX1.Position.X and CornerX1.Position.X >= BoxX4.Position.X) and (CornerX3.Position.X >= BoxX3.Position.X and CornerX3.Position.X <= BoxX2.Position.X) and (CornerX2.Position.X <= BoxX2.Position.X and CornerX2.Position.X >= BoxX4.Position.X) and (CornerX4.Position.X >= BoxX4.Position.X and  CornerX4.Position.X <= BoxX2.Position.X)	
	local Zbool = (CornerX1.Position.Z <= BoxX1.Position.Z and CornerX1.Position.Z >= BoxX4.Position.Z) and (CornerX3.Position.Z <= BoxX3.Position.Z and CornerX3.Position.Z >= BoxX4.Position.Z) and (CornerX2.Position.Z >= BoxX2.Position.Z and CornerX2.Position.Z <= BoxX3.Position.Z) and (CornerX4.Position.Z >= BoxX4.Position.Z and CornerX4.Position.Z <= BoxX3.Position.Z)
	
	if (Xbool and Zbool) then
		Ppart.BrickColor = BrickColor.Green();
	else
		Ppart.BrickColor = BrickColor.Red()
	end
	
	
end


Try local parts = workspace:GetPartsInPart(part)

This should return an array (table) of parts that intersect with the geometry of the part given in arguments.

This function works with non-collide, transparent objects as well which works perfectly in your case. The function will detect when ANY part is even barely intersecting the transparent part.

This function also has filtering, so you can pass OverlapParams in the second argument of the function:

local params = OverlapParams.new()
params.FilterType = Enum.RaycastFilterType.Whitelist -- whitelist means we ONLY scan for the targeted parts
params.FilterDescendantsInstances = {part1, part2, part3} -- targeted parts listed here
params.MaxParts = 50

while wait() do
	local parts = workspace:GetPartsInPart(workspace.Part)
	print(parts)
end

You can of course make it a blacklist instead if you want every part in the workspace to be scanned EXCEPT the targeted parts.

As for changing the brickcolor, as you included in the source code, just iterate on the array we receive from the function:

for _, part in pairs(parts) do -- for every part intersecting do this:
    part.BrickColor = BrickColor.Green()
    wait()
end

Hope this helped! :slight_smile:

Hii, yeah, but what I want is to make sure ALL corners are inside the Box.

If you want to detect if each corner is in the box I recommend using the code listed in an older post found here:

This post answers your question.

I already detect the corners inside the box, but my problem is with rotation, so when the box rotates the part doesn’t gets detected properly

can someone help me? :smiley:

I want to make sure corners of a part is inside another (even if the other is rotated and so on)

local innerPart = workspace.InnerPart
local outerPart = workspace.OuterPart

while task.wait(1) do
	local innerCorner1 = innerPart.CFrame * CFrame.new(Vector3.new(innerPart.Size.X/2, 0, innerPart.Size.Z/2))
	local innerCorner2 = innerPart.CFrame * CFrame.new(Vector3.new(-innerPart.Size.X/2, 0, -innerPart.Size.Z/2))
	local innerCorner3 = innerPart.CFrame * CFrame.new(Vector3.new(-innerPart.Size.X/2, 0, innerPart.Size.Z/2))
	local innerCorner4 = innerPart.CFrame * CFrame.new(Vector3.new(innerPart.Size.X/2, 0, -innerPart.Size.Z/2))
	
	local outerCorner1 = outerPart.CFrame * CFrame.new(Vector3.new(outerPart.Size.X/2, 0, outerPart.Size.Z/2))
	local outerCorner2 = outerPart.CFrame * CFrame.new(Vector3.new(-outerPart.Size.X/2, 0, -outerPart.Size.Z/2))
	local outerCorner3 = outerPart.CFrame * CFrame.new(Vector3.new(-outerPart.Size.X/2, 0, outerPart.Size.Z/2))
	local outerCorner4 = outerPart.CFrame * CFrame.new(Vector3.new(outerPart.Size.X/2, 0, -outerPart.Size.Z/2))
	
	if math.abs(innerCorner1.Position.X) > math.abs(outerCorner1.Position.X) or math.abs(innerCorner1.Position.Y) > math.abs(outerCorner1.Position.Y) or math.abs(innerCorner1.Position.Z) > math.abs(outerCorner1.Position.Z) then
		innerPart.Color = Color3.new(1, 0, 0)
	elseif math.abs(innerCorner2.Position.X) > math.abs(outerCorner2.Position.X) or math.abs(innerCorner2.Position.Y) > math.abs(outerCorner2.Position.Y) or math.abs(innerCorner2.Position.Z) > math.abs(outerCorner2.Position.Z) then
		innerPart.Color = Color3.new(1, 0, 0)
	elseif math.abs(innerCorner3.Position.X) > math.abs(outerCorner3.Position.X) or math.abs(innerCorner3.Position.Y) > math.abs(outerCorner3.Position.Y) or math.abs(innerCorner3.Position.Z) > math.abs(outerCorner3.Position.Z) then
		innerPart.Color = Color3.new(1, 0, 0)
	elseif math.abs(innerCorner4.Position.X) > math.abs(outerCorner4.Position.X) or math.abs(innerCorner4.Position.Y) > math.abs(outerCorner4.Position.Y) or math.abs(innerCorner4.Position.Z) > math.abs(outerCorner4.Position.Z) then
		innerPart.Color = Color3.new(1, 0, 0)
	else
		innerPart.Color = Color3.new(0, 1, 0)
	end
end

Demonstration:
https://streamable.com/3lecfg

Model file:
model.rbxm (3.3 KB)

heyy thanks for helping, but I came across a issue, Rotating the outer part breaks it


Hi guys anyone knows how to help me? :thinking:
I need to know if a part is fully inside other, even when rotated