I’m trying to do a collision check using extents and faces to check if a moving red part, regardless of its orientation is inside the white part however it isn’t working as the red part is constantly moving to a set “snapping” cframe.
local function IsPointInVolume(point: Vector3, volumeCenter: CFrame, volumeSize: Vector3): boolean
local volumeSpacePoint = volumeCenter:PointToObjectSpace(point)
return volumeSpacePoint.X >= -volumeSize.X/2
and volumeSpacePoint.X <= volumeSize.X/2
and volumeSpacePoint.Y >= -volumeSize.Y/2
and volumeSpacePoint.Y <= volumeSize.Y/2
and volumeSpacePoint.Z >= -volumeSize.Z/2
and volumeSpacePoint.Z <= volumeSize.Z/2
end
-- part is red part
-- base is white part
local function checkIfPartIsInBounds(part, base)
local partPos = part.Position
local partSize = part.Size
local basePos = base.Position
local baseSize = base.Size
local minX = basePos.X - baseSize.X/2
local maxX = basePos.X + baseSize.X/2
local minZ = basePos.Z - baseSize.Z/2
local maxZ = basePos.Z + baseSize.Z/2
if partPos.X - partSize.X/2 >= minX and partPos.X + partSize.X/2 <= maxX and
partPos.Z - partSize.Z/2 >= minZ and partPos.Z + partSize.Z/2 <= maxZ then
return true
else
return false
end
end
local function IsPartInBoundry (part: Part, boundryPart: Part): boolean
local offset = boundryPart.CFrame:Inverse() * part.CFrame
local x,y,z = -boundryPart.Size.X/2,-boundryPart.Size.Y/2,-boundryPart.Size.Z/2
local nVector = Vector3.new(x,y,z)
local v1 = nVector <= offset.Position
local x1,y1,z1 = boundryPart.Size.X/2,boundryPart.Size.Y/2,boundryPart.Size.Z/2
local nVector1 = Vector3.new(x1,y1,z1)
local v2 = nVector1 <= offset.Position
return v1 and v2
end
return offset.X >= -size.X/2 and offset.X <= size.X/2 and offset.Y >= -size.Y/2 and offset.Y <= size.Y/2 and offset.Z >= -size.Z/2 and offset.Z <= size.Z/2
You can try using game.Workspace:GetPartsInPart() to detect if the red part is inside of the white part:
local function checkIfPartIsInBounds(part, base)
local params = OverlapParams.new()
params.FilterDescendantsInstances = {base}
local touchingbase = game.Workspace:GetPartsInPart(base, params)
for i, hit in pairs(touchingbase) do
if hit == part then
return true
end
end
return false
end
I tried that but it sometimes returns false positives when the red part is just slightly touching the white part
for example, I don’t want this to be detected as collision
local function IsPartInBoundry (part: Part, boundryPart: Part): boolean
local offset = boundryPart.CFrame:Inverse() * part.CFrame
local size = boundryPart.Size
return offset.X >= -size.X/2 and offset.X <= size.X/2 and offset.Y >= -size.Y/2 and offset.Y <= size.Y/2 and offset.Z >= -size.Z/2 and offset.Z <= size.Z/2
end
local function _checkObstacle()
for _,v in workspace._objectsFromPlayer:FindFirstChild(Player.Name):GetDescendants() do
if v:IsA("BasePart") then
if IsPartInBoundry(model.PrimaryPart,v) then
return true,v
end
end
end
return false
end
local function N(Part1, Part2)
local Offset = Part1.CFrame:Inverse() * Part2.CFrame
return Offset.X <= Part1.Size.X/2 and Offset.X >= -Part1.Size.X/2 and Offset.Y <= Part1.Size.Y/2 and Offset.Y >= -Part1.Size.Y/2 and Offset.Z <= Part1.Size.Z/2 and Offset.Z >= -Part1.Size.Z/2
end
while task.wait() do
print(N(game.Workspace.Part, game.Workspace.P))
end
You can try this instead. Just edit shrinkBy to the number of studs you want for the red part to overlap until it is detected as overlapping.
local shrinkBy = 0.1
local function checkIfPartIsInBounds(part, base)
local params = OverlapParams.new()
params.FilterDescendantsInstances = {base}
local newSize = Vector3.new(base.Size.X - (shrinkBy * 2), base.Size.Y - (shrinkBy * 2), base.Size.Z - (shrinkBy * 2))
local touchingbase = game.Workspace:GetPartBoundsInBox(base.CFrame, newSize, params)
for i, hit in pairs(touchingbase) do
if hit == part then
return true
end
end
return false
end
This works, mostly, it does detect collision but sometimes it returns false and true in the same time.
Increasing shrinkBy seemed to work but I get warning spammed with GetPartBoundsInBox: Clamping out-of-bounds extents to 0-30000.000000