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

How do I check if a part is touching another part?

Asked by 8 years ago

So, I have a part that gets created like a selection box, which can be found here .

Since I'm using this as a selection box, I need to find out what parts it's touching. I don't want to use GetTouchingParts because the parts that are to be selected can't be canCollide true.

local mags = {}
for i,v in pairs(game.Workspace.Map:GetChildren()) do
    if v.Name == "Cell" then
        local mag = (v.Position-part.Position-part.Size).magnitude
        table.insert(mags,#mags+1,mag)
        print(mag)
    end
end -- I also tried taking out -part.Size

The above isn't reliable. Anybody have any ideas? Any help would be appreciated.

0
Try basic collision detection, manually. Check if both the x or y position is colliding with the x or y position of the other brick. iFireLazer 35 — 8y
0
have you thought about using a touch event? Angels_Develop 52 — 8y

1 answer

Log in to vote
0
Answered by
guges 15
8 years ago

I actually ran around writing a bunch of code for testing if a point is inside a part using dot products only to find this chunk of code by Anaminus

local function PointInsidePart(Part, Point)
    --- Return's whether a point is inside of a part.
    -- @param Part The part to check. May also be a table with a .Size and .CFrame value in it.

    local PartSize = Part.Size/2
    local RelativePosition = Part.CFrame:pointToObjectSpace(Point)
    --print(RelativePosition, PartSize)
    if not (RelativePosition.X >= -PartSize.X and RelativePosition.X <= PartSize.X) then
        return false
    elseif not (RelativePosition.Y >= -PartSize.Y and RelativePosition.Y <= PartSize.Y) then
        return false
    elseif not (RelativePosition.Z >= -PartSize.Z and RelativePosition.Z <= PartSize.Z) then
        return false
    end

    return true 
end

Anyway, here is the full code with some comments for explanation on my code

local function getVertices(part)
    local pos   = part.Position
    local size  = part.Size
    local cf    = part.CFrame
    local vertices = {}

    -- get all 8 vertices
    vertices[1]  = cf * CFrame.new(size.x*0.5, size.y*0.5, size.z*0.5)
    vertices[2]  = cf * CFrame.new(size.x*-0.5, size.y*0.5, size.z*0.5)
    vertices[3]  = cf * CFrame.new(size.x*0.5, size.y*-0.5, size.z*0.5)
    vertices[4]  = cf * CFrame.new(size.x*0.5, size.y*0.5, size.z*-0.5)
    vertices[5]  = cf * CFrame.new(size.x*-0.5, size.y*-0.5, size.z*0.5)
    vertices[6]  = cf * CFrame.new(size.x*0.5, size.y*-0.5, size.z*-0.5)
    vertices[7]  = cf * CFrame.new(size.x*-0.5, size.y*0.5, size.z*-0.5)
    vertices[8]  = cf * CFrame.new(size.x*-0.5, size.y*-0.5, size.z*-0.5)
    return vertices -- return them
end

local function PointInsidePart(Part, Point) -- Credit for this goes to anaminus
    --- Return's whether a point is inside of a part.
    -- @param Part The part to check. May also be a table with a .Size and .CFrame value in it.

    local PartSize = Part.Size/2
    local RelativePosition = Part.CFrame:pointToObjectSpace(Point)
    --print(RelativePosition, PartSize)
    if not (RelativePosition.X >= -PartSize.X and RelativePosition.X <= PartSize.X) then
        return false
    elseif not (RelativePosition.Y >= -PartSize.Y and RelativePosition.Y <= PartSize.Y) then
        return false
    elseif not (RelativePosition.Z >= -PartSize.Z and RelativePosition.Z <= PartSize.Z) then
        return false
    end

    return true 
end


function doPartsCollide(part0, part1)
    local vertices = getVertices(part0) -- get all the vertices (corners) of our part
    for _, vertex in pairs(vertices) do -- iterate
        if PointInsidePart(part1, vertex.p) then -- if point is inside
            return true -- part collided
        end
    end
    return false -- didn't collide
end

We get all the vertices by using the size vector and the position. It's in cframes to account for rotation.

Anaminus' code is very elegant and compares the size of the part and position(of the point in object space) to determine whether or not the point is outside of the part. Returning true if it isn't.

The we get an array of the first part's vertices and iterate through it. We check if this vertex is inside the other part and return true if it is.

I haven't slept in a while, so sorry if there are any mistakes.

Ad

Answer this question