Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Creating a Region3 around a model?

Asked by 7 years ago

How would I go about creating a Region3 around a model? Right now, I have this, but it doesn't go around the entire model:

01local function getC(obj, t)
02    if obj:IsA("Model") or obj:IsA("Folder") then return getC(obj:GetChildren()[1], t) end
03    if obj:IsA("BasePart") then
04        if t=="X" then
05            if obj.Position.X >= 0 then
06                return obj.Position.X + (obj.Size.X/2)
07            else
08                return obj.Position.X - (obj.Size.X/2)
09            end
10        elseif t=="Y" then
11            if obj.Position.Y >= 0 then
12                return obj.Position.Y + (obj.Size.Y/2)
13            else
14                return obj.Position.Y - (obj.Size.Y/2)
15            end
View all 64 lines...

Any help is appreciated.

1 answer

Log in to vote
2
Answered by
XAXA 1569 Moderation Voter
7 years ago
Edited 7 years ago
01function GetModelAABB(model)
02    local modelMin = nil
03    local modelMax = nil
04    for _, v in pairs(model:GetChildren()) do
05        if v:IsA("BasePart") then
06            local cf = v.CFrame
07            local size = v.Size
08            local baseCf = cf * CFrame.new(-size/2)
09            for x = 0, 1 do
10                for y = 0, 1 do
11                    for z = 0, 1 do
12                        local testCorner = (baseCf * CFrame.new( size * Vector3.new(x, y, z))).p
13 
14                        if not modelMin then
15                            modelMin = testCorner
View all 39 lines...

This returns a Region3 that hugs the model.

Get each corner of each part in the model, and get the minimum/maximum in each dimension. Return a Region3 made out of these. I have not run this, but in theory this should work. Make sure model is actually a model, otherwise this will error.

EDIT: Also, parts are not examined recursively. You may want to modify this so that it does.

EDITEDIT: Tested and found a missing parenthesis. It works.

1
Thank you! If I had the reputation to upvote, I would. This was exactly what I was looking for. BreadyToCrumble 121 — 7y
0
I appreciate that you at least tried, though. XAXA 1569 — 7y
0
This is already a core function, model:GetExtentsSize(). It's not perfect, but yours is even less so. cabbler 1942 — 7y
1
@cabbler GetExtentsSize only gives me the size of the box around the model, it doesn't return the actual region. BreadyToCrumble 121 — 7y
View all comments (2 more)
0
"It's not perfect, but yours is even less so." I highly doubt that. Do you have any proof? My implementation covers the definition of an axis-aligned bounding box (it's as good as we're going to get in Roblox, anyway, without getting the vertices of a mesh). Moreover, GetExtentsSize() is dependent on the rotation of a model's PrimaryPart (which is arbitrary), and rotations are no good for Region3s XAXA 1569 — 7y
0
Excellent, thanks! I've been struggling for ages trying to line up models and non of the built in functions seem to be up to the task! Even just trying to put a 'car ON the road' is a nightmare! Spedley2142 0 — 7y
Ad

Answer this question