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)
local PartSize = Part.Size/2
local RelativePosition = Part.CFrame:pointToObjectSpace(Point)
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 = {}
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
end
local function PointInsidePart(Part, Point)
local PartSize = Part.Size/2
local RelativePosition = Part.CFrame:pointToObjectSpace(Point)
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)
for _, vertex in pairs(vertices) do
if PointInsidePart(part1, vertex.p) then
return true
end
end
return false
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.
2 comments
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.
have you thought about using a touch event?