How to detect when a part is COMPLETELY inside a region?

I have parts of different sizes, different orientations.
I use the mouse to drag these outside parts into a box/region.
The outside parts of the box are in red and should change to green only when they are COMPLETELY inside the box:

In the example above you can see that one part is green (completely inside the box), a second is red (completely outside), and a third part is also red (almost all inside, but not completely inside).

All parts inside the box are ANCHORED and should not use physics (collide, touch).

How could I do that?

1 Like

This function can determine if a point is inside a brick:

function isInsideBrick(position, brick)
	local v3 = brick.CFrame:PointToObjectSpace(position)
	return (math.abs(v3.X) <= brick.Size.X / 2)
		and (math.abs(v3.Y) <= brick.Size.Y / 2)
		and (math.abs(v3.Z) <= brick.Size.Z / 2)

To see if an entire brick is inside the region you would check for every corner of the brick.

function isCompletelyInside(part, region)
	local size = part.Size / 2
	local corners = {
		part.CFrame *, size.Y, size.Z),
		part.CFrame *, size.Y, -size.Z),
		part.CFrame *, -size.Y, size.Z),
		part.CFrame *, -size.Y, -size.Z),
		part.CFrame *, size.Y, size.Z),
		part.CFrame *, size.Y, -size.Z),
		part.CFrame *, -size.Y, size.Z),
		part.CFrame *, -size.Y, -size.Z)
	for _, cf in ipairs(corners) do
		-- if any corner is outside, stop
		if (not isInsideBrick(cf.Position, region)) then
			return false
	return true

That’s perfect!
I’m happy to see that there are people who use their experience and intelligence to support others!
Thank you very much! :smiley:


Yo, it works! I have been looking for something like this, thank you (: