local function V(V)if V.Magnitude==0 then return Vector3.zero end return V.Unit end local function K(K,s,P)local g=K:pointToObjectSpace(P)local o,p,I=s.x,s.y,s.z local y,e,S=g.x,g.y,g.z local t=math.clamp(y,-o*.5,o*.5)local f=math.clamp(e,-p*.5,p*.5)local N=math.clamp(S,-I*.5,I*.5)if not(t==y and(f==e and N==S))then local s=K*Vector3.new(t,f,N)local g=V(P-s)return false,s,g end local W=y-o*.5 local R=e-p*.5 local m=S-I*.5 local F=-y-o*.5 local T=-e-p*.5 local z=-S-I*.5 local n=math.max(W,R,m,F,T,z)if n==W then local V=K*Vector3.new(o*.5,e,S)return true,V,K.XVector elseif n==R then local V=K*Vector3.new(y,p*.5,S)return true,V,K.YVector elseif n==m then local V=K*Vector3.new(y,e,I*.5)return true,V,K.ZVector elseif n==F then local V=K*Vector3.new(-o*.5,e,S)return true,V,-K.XVector elseif n==T then local V=K*Vector3.new(y,-p*.5,S)return true,V,-K.YVector elseif n==z then local V=K*Vector3.new(y,e,-I*.5)return true,V,-K.ZVector end warn("\067\076\079\083\069\083\084 \080\079\073\078\084 \079\078 \066\079\088 \070\065\073\076")return false,K.Position,Vector3.zero end return function(V,s,P,g)do end local o,p,I=K(V,s,P)if o then return o,p,I end local y=(p-P).Magnitude o=y<g do end return o,p,I end
local function SafeUnit(v3)
	if v3.Magnitude == 0 then
		return Vector3.zero
	end

	return v3.Unit
end

local function ClosestPointFunc(cframe, size, point)
	local rel = cframe:pointToObjectSpace(point)
	local sx, sy, sz = size.x, size.y, size.z
	local rx, ry, rz = rel.x, rel.y, rel.z

	-- constrain to within the box
	local cx = math.clamp(rx, -sx * 0.5, sx * 0.5)
	local cy = math.clamp(ry, -sy * 0.5, sy * 0.5)
	local cz = math.clamp(rz, -sz * 0.5, sz * 0.5)

	if not (cx == rx and cy == ry and cz == rz) then
		local closestPoint = cframe * Vector3.new(cx, cy, cz)
		local normal = SafeUnit(point - closestPoint)
		return false, closestPoint, normal
	end

	-- else, they are intersecting, find the surface the point is closest to

	local posX = rx - sx * 0.5
	local posY = ry - sy * 0.5
	local posZ = rz - sz * 0.5
	local negX = -rx - sx * 0.5
	local negY = -ry - sy * 0.5
	local negZ = -rz - sz * 0.5

	local max = math.max(posX, posY, posZ, negX, negY, negZ)
	if max == posX then
		local closestPoint = cframe * Vector3.new(sx * 0.5, ry, rz)
		return true, closestPoint, cframe.XVector
	elseif max == posY then
		local closestPoint = cframe * Vector3.new(rx, sy * 0.5, rz)
		return true, closestPoint, cframe.YVector
	elseif max == posZ then
		local closestPoint = cframe * Vector3.new(rx, ry, sz * 0.5)
		return true, closestPoint, cframe.ZVector
	elseif max == negX then
		local closestPoint = cframe * Vector3.new(-sx * 0.5, ry, rz)
		return true, closestPoint, -cframe.XVector
	elseif max == negY then
		local closestPoint = cframe * Vector3.new(rx, -sy * 0.5, rz)
		return true, closestPoint, -cframe.YVector
	elseif max == negZ then
		local closestPoint = cframe * Vector3.new(rx, ry, -sz * 0.5)
		return true, closestPoint, -cframe.ZVector
	end

	-- Shouldnt reach
	warn("CLOSEST POINT ON BOX FAIL")
	return false, cframe.Position, Vector3.zero
end

return function(BoxCFrame, BoxSize, Point, Radius) -- Sphere vs Box
do end	
local IsInside, ClosestPoint, Normal = ClosestPointFunc(BoxCFrame, BoxSize, Point)

	if IsInside then
		return IsInside, ClosestPoint, Normal
	end

	local DistanceToCp = (ClosestPoint - Point).Magnitude

	IsInside = (DistanceToCp < Radius)
do end	
return IsInside, ClosestPoint, Normal
end
