local function B(B)if B.Magnitude==0 then return Vector3.zero end return B.Unit end local function G(G,R,p)local r=(G-p).Magnitude local c=B(p-G)local j=r<=R return j,G+c*R,c end return function(B,R,p,r)debug.profilebegin("\083\112\104\101\114\101 \084\101\115\116\105\110\103")B=B.Position R=math.min(R.X,R.Y,R.Z)*.5 local c,j,y=G(B,R,p)if c then return c,j,y end local W=(j-p).Magnitude c=W<r debug.profileend()return c,j,y end
--!native
local function SafeUnit(v3)
	if v3.Magnitude == 0 then
		return Vector3.zero
	end

	return v3.Unit
end

local function ClosestPointFunc(position, radius, point)
	local distance = (position - point).Magnitude
	local normal = SafeUnit(point - position)
	local is_inside = (distance <= radius)

	return is_inside, position + (normal * radius), normal
end

return function(Sphere0Point, Sphere0Radius, Sphere1Point, Sphere1Radius)
	debug.profilebegin("Sphere Testing")
	Sphere0Point = Sphere0Point.Position
	Sphere0Radius = math.min(Sphere0Radius.X, Sphere0Radius.Y, Sphere0Radius.Z) * 0.5

	local IsInside, ClosestPoint, Normal = ClosestPointFunc(Sphere0Point, Sphere0Radius, Sphere1Point)

	if IsInside then
		return IsInside, ClosestPoint, Normal
	end

	local DistanceToCp = (ClosestPoint - Sphere1Point).Magnitude

	IsInside = (DistanceToCp < Sphere1Radius)
	debug.profileend()
	return IsInside, ClosestPoint, Normal
end
