How to make manual Cube-Cube collision detection?

I have encountered some occasional issues with the Touched Event so I have been trying to make my own detection of collisions. The following code works, but it doesn’t work with rotated parts, I was thinking I could use the CFrame’s rotation matrix to achieve my goal but I’m not sure how to do that.

function intersect(a, b)
  return (a.CFrame.p.X-a.Size.X/2 <= b.CFrame.p.X+b.Size.X/2) and (a.CFrame.p.X+a.Size.X/2 >= b.CFrame.p.X-b.Size.X/2) and
         (a.CFrame.p.Y-a.Size.Y/2 <= b.CFrame.p.Y+b.Size.Y/2) and (a.CFrame.p.Y+a.Size.Y/2 >= b.CFrame.p.Y-b.Size.Y/2) and
         (a.CFrame.p.Z-a.Size.Z/2 <= b.CFrame.p.Z+b.Size.Y/2) and (a.CFrame.p.Z+a.Size.Z/2 >= b.CFrame.p.Z-b.Size.Z/2)
end


game:GetService("RunService").Heartbeat:Connect(function()
	print(intersect(workspace.A, workspace.B))
end)

Your code doesn’t work in all cases, for example:

Ego has a great article on this with all the math involved:
http://wiki.roblox.com/index.php?title=User:EgoMoose/Articles/Rotated_region3

And he also provide a couple of modules to mess with:


1 Like

I think the first thing to do is understand why Touched events isn’t working properly for you. What are you trying to achieve?

Check out the file with my configurations. Just figured out it only works with cubes as mentioned in the title. I was aware of EgoMoose’s Rotated Region3 modules but I’m trying to find an alternative without using Region3, but I will check it out.

Collision problem.rbxl (13.2 KB)

I have made a weapon by welding a part to the RightHand of the character and yielded the Touched event to it. (Everything on the server) However, occasionally but somewhat regularly, the event stops triggering or does not trigger at all even though it is hitting the dummy and its hitbox which I made to try resolve this issue.

I think I have solved my issue by changing the CollisionFidelity to Box

Requested to be locked by topic creator, as issue was resolved.