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

How can I check if part is in bounds?

Asked by 9 years ago

I'm trying to write a script that will check if a part is within certain 3 dimensional bounds.

How can I do this?

Could you just provide some examples?

Also, the bounds will form many different shapes.

1 answer

Log in to vote
1
Answered by 9 years ago

You'll have to think through what sort of precision you want. For instance, do you want to know if any part of the Part is overlapping some 3D volume? You'll have to check at least every corner of the Part to do that. If the 3D volume is not a rectangular prism, you'll have even more work to do. On the other hand, if you treat it like a sphere, the calculations become much easier.

If you just need "Is this part centred within certain [non-rotated] bounds", you might use:

function PointInRegion(point, minX, maxX, minY, maxY, minZ, maxZ)
    return point.X >= minX and point.X <= maxX
        and point.Y >= minY and point.Y <= maxY
        and point.Z >= minZ and point.Z <= maxZ
end

If you wanted to know if any part of your part is within the bounds, you could check all 8 corners of your Part (though you should first check to see if it's Position is even nearby, to save time). You can get the corner of a rotated part using part.CFrame:vectorToWorldSpace(part.Size/2) (that one will give you the rear/top/right corner; to get the other 7 you will have to negate each axis), ex:

function PartInRegion(part, ...) --"..." is minX, maxX, etc.
    for x = -0.5, 0.5 do
        for y = -0.5, 0.5 do
            for z = -0.5, 0.5 do
                if PointInRegion(part.CFrame:pointToWorldSpace(Vector3.new(x,y,z)*part.Size), ...) then
                    return true
                end
            end
        end
    end
    return false
end

If you just need a simple "Is this point within this part", I happen to have a function that does just that:

function PointInPart(point, part)
    point = part.CFrame:pointToObjectSpace(point)
    return math.abs(point.X) <= part.Size.X / 2
        and math.abs(point.Y) <= part.Size.Y / 2
        and math.abs(point.Z) <= part.Size.Z / 2
end

As you can see, it uses CFrame:pointToObjectSpace which is documented here. In essence, it gives you point from the perspective of CFrame.

So, if you wanted to know if one Part was inside another Part and both were rectangular prisms, you could check to see if any of the corners of one part were inside the other (but try to avoid doing making this calculation -- ie, check to see if the Parts are a fair distance apart, first)

If you need more than what I've provided, consider looking up "collision detection of complex shapes" (or whatever you need) in Google or Stack Overflow (ex this one from StackOverflow )

2
I have a feeling that OP might be considering 'Region3'. However, great explanation! You should code block that function, however. DigitalVeer 1473 — 9y
0
Thanks DigitalVeer; I was typing it up in notepad and forgot to add that. [Edit: I also added how to determine if a rotated part is in a Region3 type of space] chess123mate 5873 — 9y
Ad

Answer this question