this is an efficient function made to take two AABBs , and early out collision them if possible, but keep in mind you should do a pre-check like magnitude before using this function, as it is a precise collision, so it will use up a bit of resources, this is designed to work on X and Z axes, but it can be modified to work with X and Y if you’d like, or… Y and Z, but idk if anyone really wants to do that… also, making it OBBOBB would be simple, for someone minorly developed in math
I based it off of: Hyperplane separation theorem, but it is not the exact algorithm, I did a few minor optimizations for AABB/OBBS that will need to be removed should you use polygons.
function AABBAABBCollision(AABB1Points,AABB2Points)
local Axes = {Vector3.new(1,0,0),Vector3.new(0,0,1)}
for i=1,#Axes do
local MinDot,MaxDot = nil,nil
for k=1,#AABB1Points do
local Dot = AABB1Points[k]:Dot(Axes[i])
if MinDot == nil or Dot < MinDot then
MinDot = Dot
end
if MaxDot == nil or Dot > MaxDot then
MaxDot = Dot
end
end
local PassedAxis = false
local MinDot2,MaxDot2 = nil,nil
for k=1,#AABB2Points do
local Dot = AABB2Points[k]:Dot(Axes[i])
if Dot >= MinDot and Dot <= MaxDot then
PassedAxis = true
break
end
if MinDot2 == nil or Dot < MinDot2 then
MinDot2 = Dot
end
if MaxDot2 == nil or Dot > MaxDot2 then
MaxDot2 = Dot
end
end
if not PassedAxis then
if (MinDot < MinDot2 or MinDot > MaxDot2) and (MaxDot < MinDot2 or MaxDot > MaxDot2) then
return false
end
end
end
return true
end