Checking If A Part is Fully Inside A Hitbox

I wrote a function to check, but I’m wondering if there’s a better way to check if a part is fully inside another part.

Both the part and hitbox can be in any orientation, position, and size.

For context the function will be run onRenderStepped

function checkInBounds(part, hitbox)
	local pointsToCheckInBounds =
		{
			CFrame.new(-part.Size.X/2,part.Size.Y/2,-part.Size.Z/2),
			CFrame.new(part.Size.X/2,-part.Size.Y/2,-part.Size.Z/2),
			CFrame.new(part.Size.X/2,part.Size.Y/2,part.Size.Z/2),
			CFrame.new(-part.Size.X/2,-part.Size.Y/2,part.Size.Z/2)
		}
	
	for i, pointCFrameLocalToPart in pointsToCheckInBounds do
		local pointCFrameLocalToHitbox = hitbox.CFrame:ToObjectSpace(part.CFrame:ToWorldSpace(pointCFrameLocalToPart))
		if math.abs(pointCFrameLocalToHitbox.X) > hitbox.Size.X/2 or math.abs(pointCFrameLocalToHitbox.Z) > hitbox.Size.Z/2 or math.abs(pointCFrameLocalToHitbox.Y) > hitbox.Size.Y/2 then
			return false
		end
	end
	
	return true
end

if the hitbox is a part, then…

this method makes this a lot more easy. A lot more readable.

I’d want to check if the entire part is inside the hitbox though not if they are overlapping

Your code looks like it should work for all orientations and positions.

If your hitbox is brick-shaped you could make something more general by creating “check parts” completely around the part and doing logic like: is within main part and not inside the surrounding “check parts” (using Workspace:GetPartsInPart).

If you don’t need a more general case though, your solution is probably more performance friendly and in general perfectly acceptable solution.

1 Like

oh ok then your method might just be one of the only ways

1 Like