#How to check if 2 part intersects but not overlap

1 messages · Page 1 of 1 (latest)

worldly talon
#

Ye so if a part completly covers another part i want to ignore that

radiant zephyr
#

is the part a cube?

#

simple check the 8 corners and makes sure all 8 corners are inside the other part

ocean gazelle
radiant zephyr
#

sure

ocean gazelle
radiant zephyr
#
local function PartInPart(partA, partB)
    local edges = {
        partA.CFrame * Vector3.new(-partA.Size.X / 2, partA.Size.Y / 2, -partA.Size.Z / 2)
        partA.CFrame * Vector3.new(partA.Size.X / 2, partA.Size.Y / 2, -partA.Size.Z / 2)
        partA.CFrame * Vector3.new(-partA.Size.X / 2, partA.Size.Y / 2, partA.Size.Z / 2)
        partA.CFrame * Vector3.new(partA.Size.X / 2, partA.Size.Y / 2, partA.Size.Z / 2)
        partA.CFrame * Vector3.new(-partA.Size.X / 2, -partA.Size.Y / 2, -partA.Size.Z / 2)
        partA.CFrame * Vector3.new(partA.Size.X / 2, -partA.Size.Y / 2, -partA.Size.Z / 2)
        partA.CFrame * Vector3.new(-partA.Size.X / 2, -partA.Size.Y / 2, partA.Size.Z / 2)
        partA.CFrame * Vector3.new(partA.Size.X / 2, -partA.Size.Y / 2, partA.Size.Z / 2)
    }
    for i, position in edges do
            local objectPosition = partB.CFrame:PointToObjectSpace(position)
            if math.abs(objectPosition.X) > brick.Size.X / 2 then return false end
            if math.abs(objectPosition.Y) > brick.Size.Y / 2 then return false end
            if math.abs(objectPosition.Z) > brick.Size.Z / 2 then return false end
    end
    return true
end
#

you could optimize this function a bit and I'm not sure if there is more smart way of doing it

tulip salmon
#

@worldly talon bro can u explain me?

#

i think i can help u

radiant zephyr
#

unfortunately Roblox's new documentation no longer tells you but it transforms in object space

#

it returns a vector3

#
-- OLD scroll down for new code
local function PartInPart(partA, partB)
    local edges = {
        partA.CFrame:PointToWorldSpace(Vector3.new(-partA.Size.X / 2, partA.Size.Y / 2, -partA.Size.Z / 2))
        partA.CFrame:PointToWorldSpace(Vector3.new(partA.Size.X / 2, partA.Size.Y / 2, -partA.Size.Z / 2))
        partA.CFrame:PointToWorldSpace(Vector3.new(-partA.Size.X / 2, partA.Size.Y / 2, partA.Size.Z / 2))
        partA.CFrame:PointToWorldSpace(Vector3.new(partA.Size.X / 2, partA.Size.Y / 2, partA.Size.Z / 2))
        partA.CFrame:PointToWorldSpace(Vector3.new(-partA.Size.X / 2, -partA.Size.Y / 2, -partA.Size.Z / 2))
        partA.CFrame:PointToWorldSpace(Vector3.new(partA.Size.X / 2, -partA.Size.Y / 2, -partA.Size.Z / 2))
        partA.CFrame:PointToWorldSpace(Vector3.new(-partA.Size.X / 2, -partA.Size.Y / 2, partA.Size.Z / 2))
        partA.CFrame:PointToWorldSpace(Vector3.new(partA.Size.X / 2, -partA.Size.Y / 2, partA.Size.Z / 2))
    }
    for i, position in edges do
        local objectPosition = partB.CFrame:PointToObjectSpace(position)
        if math.abs(objectPosition.X) > brick.Size.X / 2 then return false end
        if math.abs(objectPosition.Y) > brick.Size.Y / 2 then return false end
        if math.abs(objectPosition.Z) > brick.Size.Z / 2 then return false end
    end
    return true
end
#

this should give the same result

tulip salmon
#

local function PartInPart(partA, partB)

for a = 1,2 do
for b = 1,2 do
for c = 1,2 do
edges[#edges + 1] = partA.CFrame:PointToWorldSpace(Vector3.new( math.pow(-1,a) * partA.Size.X / 2, math.pow(-1,b) * partA.Size.Y / 2, math.pow(-1,c) * partA.Size.Z / 2))
end
end
end
for i, position in edges do
local objectPosition = partB.CFrame:PointToObjectSpace(position)
if math.abs(objectPosition.X) > brick.Size.X / 2 then return false end
if math.abs(objectPosition.Y) > brick.Size.Y / 2 then return false end
if math.abs(objectPosition.Z) > brick.Size.Z / 2 then return false end
end
return true
end

#

i made it smaller

radiant zephyr
# tulip salmon local function PartInPart(partA, partB) for a = 1,2 do for b = 1,2 do ...

if you do it like this then you don't need the edges table

local function PartInPart(partA, partB)
  for x = -0.5, 0.5 do
    for y = -0.5, 0.5 do
      for z = -0.5, 0.5 do
        local worldPosition = partA.CFrame * Vector3.new(partA.Size.X * x, partA.Size.Y * y, partA.Size.Z * z)
        local objectPosition = partB.CFrame:PointToObjectSpace(worldPosition)
        if math.abs(objectPosition.X) > brick.Size.X / 2 then return false end
        if math.abs(objectPosition.Y) > brick.Size.Y / 2 then return false end
        if math.abs(objectPosition.Z) > brick.Size.Z / 2 then return false end
      end
    end
  end
  return true
end
tulip salmon
#

yeah lol

radiant zephyr
tulip salmon
#

i found that idea first

#

but

#

bro

radiant zephyr
tulip salmon
#

but

#

u need to tell there if not 0

radiant zephyr
#

let me test

#

it works

tulip salmon
#

but it need to amke bug because of 0

#

oh

#

ok

#

sry

#

its goin +1

radiant zephyr
#

yer for -0.5 it goes to 0.5

#

no 0

tulip salmon
#

ya

#

i couldnt notice that

worldly talon
#

what about complex shapes

radiant zephyr
# worldly talon what about complex shapes
local part = Instance.new("Part")
part.Size = Vector3.new(0, 0, 0)
part.Anchored = true
part.Transparency = 1
part.CanCollide = false
part.CanQuery = false
part.CanTouch = false
part.Parent = workspace

local overlapParams = OverlapParams.new()
overlapParams.FilterType = Enum.RaycastFilterType.Whitelist

local function PartInPart(partA, partB)
  overlapParams.FilterDescendantsInstances = {partB}
  for x = -0.5, 0.5 do
    for y = -0.5, 0.5 do
      for z = -0.5, 0.5 do
        part.Position = partA.CFrame * Vector3.new(partA.Size.X * x, partA.Size.Y * y, partA.Size.Z * z)
        if #workspace:GetPartsInPart(part, overlapParams) == 0 then return false end
      end
    end
  end
  return true
end
ocean gazelle
#

an example of overlap params

#

whats the difference between whitelist and blacklist?