Skip to content

Instantly share code, notes, and snippets.

@howmanysmall
Created October 30, 2020 16:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save howmanysmall/6d91d24d420225eb9083baf33978a5f9 to your computer and use it in GitHub Desktop.
Save howmanysmall/6d91d24d420225eb9083baf33978a5f9 to your computer and use it in GitHub Desktop.
local function GetBoundingBox(model, orientation)
if typeof(model) == "Instance" then
model = model:GetDescendants()
end
if not orientation then
orientation = CFrame.new()
end
local inf = math.huge
local minx, miny, minz = inf, inf, inf
local maxx, maxy, maxz = -inf, -inf, -inf
for _, obj in ipairs(model) do
if obj:IsA("BasePart") then
local cf = obj.CFrame
cf = orientation:ToObjectSpace(cf)
local size = obj.Size
local sx, sy, sz = size.X, size.Y, size.Z
local x, y, z, R00, R01, R02, R10, R11, R12, R20, R21, R22 = cf:GetComponents()
local wsx = 0.5 * (math.abs(R00) * sx + math.abs(R01) * sy + math.abs(R02) * sz)
local wsy = 0.5 * (math.abs(R10) * sx + math.abs(R11) * sy + math.abs(R12) * sz)
local wsz = 0.5 * (math.abs(R20) * sx + math.abs(R21) * sy + math.abs(R22) * sz)
if minx > x - wsx then
minx = x - wsx
end
if miny > y - wsy then
miny = y - wsy
end
if minz > z - wsz then
minz = z - wsz
end
if maxx < x + wsx then
maxx = x + wsx
end
if maxy < y + wsy then
maxy = y + wsy
end
if maxz < z + wsz then
maxz = z + wsz
end
end
end
local omin, omax = Vector3.new(minx, miny, minz), Vector3.new(maxx, maxy, maxz)
local omiddle = (omax+omin)/2
local wCf = orientation - orientation.Position + orientation:PointToWorldSpace(omiddle)
local size = (omax-omin)
return wCf, size
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment