Limiting a part from crossing a border

Hello, I recently made a scale tool for a plot building system I am making.

The scale tool works fine and all, but now I need to make it so the part can not be scale passed the borders of the plot. (Or the perimeter of the plot)

The plot is a part that the player builds on top of.

So far I’ve been given this by someone else who tried to help me, but it did not work.

topCorner = p.CFrame.p + Vector3.new(p.Size.X/2,0,p.Size.Z2)
bottomCorner = p.CFrame.p -   Vector3.new(p.Size.X/2,0,p.Size.Z2)

    local biggerY = math.max(topCorner.Y,bottomCorner.Y)

    local smallerY = math.min(topCorner.Y,bottomCorner.Y)

    local biggerX = math.max(topCorner.X,bottomCorner.X)

    local smallerX = math.min(topCorner.X,bottomCorner.X)

    if point.X < biggerX and point.X > smallerX and point.Y < biggerY and point.Y > smallerY then

    -- is in between points

    end

I do not fully understand it, but I get generally what its trying to do, but it did not work.

Also: I took the part being scaled for the point.X and point.Y like part.CFrame.X

1 Like

If it is a box without a rotation, not a mesh or something, what you are saying is possible with a basic mathematical calculation. Let’s say Plot is the part player builds on top as you said, and Part is the thing player scales.

local TopOfPlot = Plot.Position.Y + Plot.Size.Y
local BottomOfPart = Part.Position.Y - Part.Size.Y
if BottomOfPart > TopOfPlot then
    --Do scaling process
end

If you made a nice building system with meshes, other parts and with rotations etc. the first thing that comes to my mind is Region3. There may be other ways, and if so, I encourage other people to show it, but for now I’ll show how to do this with Region3.
Basically, a Region3 is a box in 3D space. It requires a minimum and maximum position to create and you can use it to see if there are parts colliding with this box.

local SizeHalf = Plot.Size/2
local Reg3 = Region3.new(Plot.Position - SizeHalf, Plot.Position + SizeHalf) --Creates a Region3 that is sized of the plot, with rotation of 0,0,0
local AllPartsInReg3 = game.Workspace:FindPartsInRegion3(Reg3) --This will get max. 20 parts BY DEFAULT, enough for you, but check required parameters to see how to increase limit.
--After, you can see within a for loop if the part you're scaling is in this table we just got, named AllPartsInReg3

If you have plots with different rotations tho, you might need EgoMoose’s Rotated Region3.
Wishing you good luck with your project, I hope I could help :slightly_smiling_face:

Also you might need the sources:
Region3
FindPartsInRegion3

2 Likes

Wait arn’t mesh and other parts have a corner either way? So the first example could work?

Also could you explain the first example and how it works?

I meant MeshParts. Of course they have a corner, but different than a box. You can reach to a box’s corners’ with calculation easily, but that’s not possible for a complex MeshPart or a Wedge. Also when you rotate those, you still can calculate the bottom, but it gets harder. Region3 is pretty easier to use.

1 Like

People told me Region3 is meant for terrain. Also, what about the example I provided? Does it mean anything?

Region3 is not meant for terrain or mesh parts. FindPartInRegion3 is a utility method that allows you to find parts in a 3d region of the workspace that had no rotation applied to it.

(There are also a few other similar utility methods using region3)

The way I solve the original problem in my games is either simply making the model red and not allowing actual building if the position of the modal is outside the region (model.x < minX, ect.) Or by clamping the modal position with math.max(minX, math.min(maxX, model.X)).

1 Like

Can a part work if it is rotated within the region? (Not sticking out), also would I have to make the region extend up for ever depending on how high they make there builds? Also what is the point of minimum and maxium like minX and maxX?

Example you have provided probably returns corners of a non-rotated part. Even if it is rotated, it returns non-rotated forms’ corners as I read so far.

Region3 can be used for Terrain, but not only for terrain as @TheNickmaster21 said.
You can use Region3 for your issue too.

If you want to calculate corners of a box, CFrame calculation is needed too. Here is an example:

Part = game.Workspace.Part

local CF = Part.CFrame
local Pos = Part.Position
local SizeHalf = Part.Size/2
local look = CF.lookVector
local right = CF.rightVector
local up = CF.upVector
local Corners = {}
for x = -1,1,2 do
    for y = -1,1,2 do
        for z = -1,1,2 do
            table.insert(Corners, Pos + (right*x*SizeHalf.X) + (up*y*SizeHalf.Y) + (look*z*SizeHalf.Z))
        end
    end
end

for a,b in pairs(Corners) do
    print(b)
end

What I’m doing in this code is to multiply half of size’s X, Y and Z with part’s lookVector, upVector and RightVector which is one-stud sized vector showing where parts’ surfaces are looking, and of course loops’ variables since we want negative and positive sides too.

2 Likes

The geometry gets more complex if you are trying to keep all corners inside of a region. EgoMoose has an article somewhere (just Google it) on finding parts and collisions with 3d regions.

MinX is the bottom of the region and boundary and maxX is the top of the region.

Si thiis gives me all the parts in the region and puts in a table and i use a for loop to check if its stays in the table?

There are a plethora of methods you can undertake to achieve this. Most commonly, or perhaps the most convenient method, is to make use of Region3. You can read up on how to use Region3 to detect parts in a set region here.

Region3int16 is used for Terrain, whereas Region3 is used for parts. The most common use case is to detect if a part intersects a set region, you can read more about that in the aforementioned wiki link above.

It gives corners of a part, not of a region. I sent the code to calculate corners but I highly suggest Region3 mate.

1 Like

I really don’t care if parts are sticking out a little, but if I just rotate it in the middle of the plot or something, will it still be recognized by the findPartsInRegion?

Also the code provided by Super

local CF = Part.CFrame
local Pos = Part.Position
local SizeHalf = Part.Size/2
local look = CF.lookVector
local right = CF.rightVector
local up = CF.upVector
local Corners = {}
for x = -1,1,2 do
    for y = -1,1,2 do
        for z = -1,1,2 do
            table.insert(Corners, Pos + (right*x*SizeHalf.X) + (up*y*SizeHalf.Y) + (look*z*SizeHalf.Z))
        end
    end
end

for a,b in pairs(Corners) do
    print(b)
end

How exactly can I use this with region3?

You won’t use the code with Region3, Region3 and FindPartsInRegion3 already finds parts in the region without any heavy calculations. I sent it just to show you how to calculate corners :expressionless:

1 Like

So I don’t need to use that. Just use the region3 and find parrts and it will work? And each time it scales I just make sure its still in the region? With the findPartsInRegion give me an array?

EDIT: Wait if a part is scaled just 1 stud past the plot, but most of the art is still within the region, won’t it still let the user scale epast the region?

You need to make sure it’s NOT in the region, that’s the point we’re calculating it with plot. And yeah that’s how it would work :smiley:

EDIT: Yeah, even if 0.01 pixel of the part is in the region, it still finds it. It checks for collision I guess? Not sure how it works but it works :smiley:

1 Like

As suggested by @TheNickmaster21, use EgoMoose’s article about Rotated Region3.

Make sure you understand the prerequisites listed in the article too; this is absolutely the best solution to your problem.

SORRY I get it, I have to see if the user scales it out of the region, if they do, then its out of the plot and to reverse the scale.

Right?

But if so, how I check if they scaled it out of the region? Because what if the user scales only 1 stud out of the region but the rest of the part is still in the region. Would it be able to know that small peice of the part is not within the region?

OOOOH YOU WANT FOR SIDES TOO!
Dang. Well, for that, you need to place Region3’s for SIDES of the plot too. Here’s a picture of where you need to create Region3’s. Also the plot itself needs to be Region3 too. The middle, where player can build is empty.
https://gyazo.com/b8a82275555ca9e1e1b9b02a02be0483.png

1 Like

So, I just basicly check if the part is found within any of those 4 regions, and if it is found, then a peice of the part is sticking out the region?

edit: also what do you mean there has to be a region3 in the plot itself too, did you not add that part in the photo?