如何检查 vector3 值是否在 Roblox Studio 中的 region3 值中?
how do I check if a vector3 value is in a region3 value in Roblox Studio?
我正在制作一款带有建筑系统的游戏。我试图通过在一个人的帐篷周围创建一个区域来将土地所有权添加到我的游戏中。问题是我不知道如何检查另一个玩家是否在另一个玩家的土地上放置了东西。有人有答案吗?
A Vector3 本质上只是具有 X、Y 和 Z 分量的坐标。这里假设它的原点在 (0, 0, 0).
一个Region3是一个基于最小Vector3坐标和最大Vector3坐标构建的轴对齐边界框。
如果Vector3的大小大于最小坐标且小于最大坐标,则可以认为其在Region3内。
由于 Region3 将区域的中心作为其 CFrame 属性,您可以使用将其 Size 分成两半来计算 Region3 的范围,然后检查 Vector3 是否落在该区域内。
local function isVect3InRegion3(vector, region)
-- validate input
assert(typeof(vector) == "Vector3")
assert(typeof(region) == "Region3")
-- get the dimensions of the region
local regionCenter = region.CFrame.Position
local regionHalfSize = region.Size * 0.5
-- calculate the extents of the region
local lowerLeftCorner = regionCenter - regionHalfSize
local upperRightCorner = regionCenter + regionHalfSize
-- check if the vector is inside the extents
local isBeyondLeftCorner = (vector.X >= lowerLeftCorner.X) and
(vector.Y >= lowerLeftCorner.Y) and
(vector.Z >= lowerLeftCorner.Z)
local isBeforeRightCorner = (vector.X <= upperRightCorner.X) and
(vector.Y <= upperRightCorner.Y) and
(vector.Z <= upperRightCorner.Z)
return isBeyondLeftCorner and isBeforeRightCorner
-- NOTE : comparing individual values above is less expensive than
-- return vector.Magnitude >= lowerLeftCorner.Magnitude and
-- vector.Magnitude <= upperRightCorner.Magnitude
end
我正在制作一款带有建筑系统的游戏。我试图通过在一个人的帐篷周围创建一个区域来将土地所有权添加到我的游戏中。问题是我不知道如何检查另一个玩家是否在另一个玩家的土地上放置了东西。有人有答案吗?
A Vector3 本质上只是具有 X、Y 和 Z 分量的坐标。这里假设它的原点在 (0, 0, 0).
一个Region3是一个基于最小Vector3坐标和最大Vector3坐标构建的轴对齐边界框。
如果Vector3的大小大于最小坐标且小于最大坐标,则可以认为其在Region3内。
由于 Region3 将区域的中心作为其 CFrame 属性,您可以使用将其 Size 分成两半来计算 Region3 的范围,然后检查 Vector3 是否落在该区域内。
local function isVect3InRegion3(vector, region)
-- validate input
assert(typeof(vector) == "Vector3")
assert(typeof(region) == "Region3")
-- get the dimensions of the region
local regionCenter = region.CFrame.Position
local regionHalfSize = region.Size * 0.5
-- calculate the extents of the region
local lowerLeftCorner = regionCenter - regionHalfSize
local upperRightCorner = regionCenter + regionHalfSize
-- check if the vector is inside the extents
local isBeyondLeftCorner = (vector.X >= lowerLeftCorner.X) and
(vector.Y >= lowerLeftCorner.Y) and
(vector.Z >= lowerLeftCorner.Z)
local isBeforeRightCorner = (vector.X <= upperRightCorner.X) and
(vector.Y <= upperRightCorner.Y) and
(vector.Z <= upperRightCorner.Z)
return isBeyondLeftCorner and isBeforeRightCorner
-- NOTE : comparing individual values above is less expensive than
-- return vector.Magnitude >= lowerLeftCorner.Magnitude and
-- vector.Magnitude <= upperRightCorner.Magnitude
end