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

Get "min" and "max" Vector3 Values from a Region3 Object?

Asked by
maxbd 44
5 years ago
Edited 5 years ago

How would you go about getting the min and max Vector3 values from the existing properties of a Region3 data-type value?

So, I've recently stumbled across this question. We can create Region3 objects by passing a min Vector3 object and a max Vector3 objects to Region3.new() which represent the position of the two corners opposite of each other of the Region3 object that is being created.

Only constructor of Region3:
Region3.new(Vector3 min, Vector3 max)

However, a Region3 data-type object only contains the properties CFrame and size, where size is represented as a Vector3 object. So I thought that maybe by using the position of the Region's center given in the CFrame and the size property given we would be able to find two opposite corners of the Region3 (the min and max Vector3 values)???

local region = Region3.new(Vector3.new(0, 0, -3), Vector3.new(4, 4, 4))
local res = 4
region = region:ExpandToGrid(res)

local terrain = game:WaitForChild("Terrain")
terrain:FillRegion(region, 4, Enum.Material.Water)

^ To further elaborate, if we take that code from https://developer.roblox.com/en-us/api-reference/datatype/Region3, is there a way to get the min Vector3 value, Vector3.new(0, 0, -3) and the max Vector3 value, Vector3.new(4, 4, 4), back from the information we have within the region Region3 object?

I thought there would be some way to do this with math, but I am absolutely terrible with math. If anyone has like an equation or something for this, do you think you could explain the way it works to me so I can understand? I've tried researching this question, but I've found basically nothing helpful. :/

Thank you in advance! :)

1 answer

Log in to vote
0
Answered by 2 years ago

Region3 accepts two arguments in its constructor. Min and Max. Think of these two vectors as a corner of a part. This part would be that region3. We can get corners of a part by multiplying the CFrame by the size of the part. Region3 offers us the CFrame and the Size properties, so we can solve Min like CFrame * (Size / -2), and Max like CFrame * (Size / 2). I've created a function that would do this for you here.

local function GetMinMax(Region: Region3): (Vector3, Vector3)
    local Region_CFrame: CFrame = Region.CFrame
    local Region_Size: Vector3 = Region.Size
    return Region_CFrame * (Region_Size / -2), Region_CFrame * (Region_Size / 2)
end
Ad

Answer this question