# Is there a way to check if a part is entirely in a Region3?

I know that there is :FindPartsInRegion3(), but this is more specific. Here is a poorly illustrated example of what I mean.

Figures 2 and 3 would both be in the array. Is there an easy way to figure out if the part is in a state like in Figure 3 and not like Figure 2?

2 Likes

The only way off the top of my head would be to just put another Region3 next to it and see if itâ€™s in that region. But that can be really tolling on a server. You will probably have to do some math with the center point and the corners and determine if theyâ€™re all within the bounds. If itâ€™s grid based, i.e. the part will always be going straight into a face of the region, thatâ€™s easier because all you have to do is determine if the partâ€™s lower position bounds (position - size/2) is greater than the regionâ€™s lower bounds and the upper bounds are lower than the regionâ€™s upper bounds.

Thanks for the tips!

1 Like
``````workspace.corner1.Parent = game.ReplicatedStorage
workspace.corner2.Parent = game.ReplicatedStorage

local Point1 = game.ReplicatedStorage.corner1.Position
local Point2 = game.ReplicatedStorage.corner2.Position
local Region = Region3.new(Point1,Point2)
for _,v in next, game.Workspace:FindPartsInRegion3(Region,nil,math.huge) do
print(v.Name)
if (v.Position.X-(v.Size.X/2)) >= Point1.X and (v.Position.X+(v.Size.X/2)) <= Point2.X and (v.Position.Z-(v.Size.Z/2)) >= Point1.Z and (v.Position.Z+(v.Size.Z/2)) <= Point2.Z and (v.Position.Y-(v.Size.Y/2)) >= Point1.Y and (v.Position.Y+(v.Size.Y/2)) <= Point2.Y then
print("yes")
end
end
``````

The corner1 and corner2 just served as a representation for me, can replace them with any Vector3 Positions and does exactly what you want judging by your illustrated example

Edit:

I modified the code a bit as I forgot to include Y axis checking; as others have mentioned, this doesnâ€™t properly support part rotation.

11 Likes

Thanks for the help! Much appreciated

1 Like

Doesnâ€™t look like it supports part rotation to me, and is 2D. What about my adjustment below? Sorry if it doesnâ€™t work, I canâ€™t run ROBLOX on my work computer so I gotta hope I didnâ€™t message up

``````local gMaxX = Math.max(Point1.X, Point2.X) -- X
local gMinX = Math.min(Point1.X, Point2.X)
local gMaxY = Math.max(Point1.Y, Point2.Y) -- Y
local gMinY = Math.min(Point1.Y, Point2.Y)
local gMaxZ = Math.max(Point1.Z, Point2.Z) -- Z
local gMinZ = Math.min(Point1.Z, Point2.Z)

-- ... for blah in blah do

-- Find all corners of the part
local c1 = v.CFrame * CFrame.new(v.Size.X / 2, v.Size.Y / 2, v.Size.Z / 2)
local c2 = v.CFrame * CFrame.new(-v.Size.X / 2, v.Size.Y / 2, v.Size.Z / 2)
local c3 = v.CFrame * CFrame.new(-v.Size.X / 2, -v.Size.Y / 2, v.Size.Z / 2)
local c4 = v.CFrame * CFrame.new(-v.Size.X / 2, -v.Size.Y / 2, -v.Size.Z / 2)
local c5 = v.CFrame * CFrame.new(v.Size.X / 2, -v.Size.Y / 2, -v.Size.Z / 2)
local c6 = v.CFrame * CFrame.new(v.Size.X / 2, v.Size.Y / 2, -v.Size.Z / 2)
local c7 = v.CFrame * CFrame.new(v.Size.X / 2, -v.Size.Y / 2, v.Size.Z / 2)
local c8 = v.CFrame * CFrame.new(-v.Size.X / 2, v.Size.Y / 2, -v.Size.Z / 2)

-- Find bounds of the part from the corners
local maxX = Math.max(c1.X, c2.X, c3.X, c4.X, c5.X, c6.X, c7.X, c8.X)
local minX = Math.min(c1.X, c2.X, c3.X, c4.X, c5.X, c6.X, c7.X, c8.X)
local maxY = Math.max(c1.Y, c2.Y, c3.Y, c4.Y, c5.Y, c6.Y, c7.Y, c8.Y)
local minY = Math.min(c1.Y, c2.Y, c3.Y, c4.Y, c5.Y, c6.Y, c7.Y, c8.Y)
local maxZ = Math.max(c1.Z, c2.Z, c3.Z, c4.Z, c5.Z, c6.Z, c7.Z, c8.Z)
local minZ = Math.min(c1.Z, c2.Z, c3.Z, c4.Z, c5.Z, c6.Z, c7.Z, c8.Z)

-- Note >= for inclusive. Could change to > for within. Same with <=.
if minX >= gMaxX and maxX <= gMaxX and minY >= gMaxY and maxY <= gMaxY and minZ >= gMaxZ and maxZ <= gMaxZ then
print("yes")
``````

Mine is going to be pretty slow as you add more parts because of all the CFrame operations. Iâ€™d grab a pen and paper and math it out if you want it to be quicker.

1 Like

I second this, using the corners of the part is an effective method of doing this.

It doesnâ€™t.