Collision detection using vector3

My placement system only has 90 degree rotations, which allows for regular vector3 collision detection without using spatial query or region3, which is what I’m trying to achieve.

The reason to why I’m avoiding spatial query & region 3 is performance, if thousands of parts are being placed (which yes, is possible in my game) collision performance becomes very very vital.

I already have some collision detection in place, the current problem is when the two parts are cross intersecting, the upper and lower corners are technically not intersecting.

2D Example of the issue:

My current code checks if atleast one of the two corners intersect on X, Y, and Z.
Example: corner 1 can intersect on X and Z, and corner 2 can intersect on Y, which basically allows for collision detection of all corners with only two, the upper most and lower most.

local function CheckCollision(upperCorner, lowerCorner, model)
	local vUpper = Vector3RoundToGrid(model.PrimaryPart.CFrame * (model.PrimaryPart.Size/2), 0.1)
	local vLower = Vector3RoundToGrid(model.PrimaryPart.CFrame * (-model.PrimaryPart.Size/2), 0.1)
	
	return
		(
			( (upperCorner.X >= vLower.X and upperCorner.X <= vUpper.X)
				or (lowerCorner.X >= vLower.X and lowerCorner.X <= vUpper.X) )

			and ( (upperCorner.Z >= vLower.Z and upperCorner.Z <= vUpper.Z)
				or (lowerCorner.Z >= vLower.Z and lowerCorner.Z <= vUpper.Z) )

			and ( (upperCorner.Y >= vLower.Y and upperCorner.Y <= vUpper.Y)
				or (lowerCorner.Y >= vLower.Y and lowerCorner.Y <= vUpper.Y) )
		)
		
		or (
			( (vUpper.X >= lowerCorner.X and vUpper.X <= upperCorner.X)
				or (vLower.X >= lowerCorner.X and vLower.X <= upperCorner.X) )

			and ( (vUpper.Z >= lowerCorner.Z and vUpper.Z <= upperCorner.Z)
				or (vLower.Z >= lowerCorner.Z and vLower.Z <= upperCorner.Z) )

			and ( (vUpper.Y >= lowerCorner.Y and vUpper.Y <= upperCorner.Y)
				or (vLower.Y >= lowerCorner.Y and vLower.Y <= upperCorner.Y) )
		)
end

function module.IsColliding(plot, cf, size, excTbl)
	excTbl = excTbl or {}
	local upperCorner = Vector3RoundToGrid(cf * (size/2), 0.1) - Vector3.new(0.01, 0.01, 0.01)
	local lowerCorner = Vector3RoundToGrid(cf * (-size/2), 0.1) + Vector3.new(0.01, 0.01, 0.01)
	
	for _, v in pairs(plot.Placement:GetChildren()) do
		if (not table.find(excTbl, v)) and CheckCollision(upperCorner, lowerCorner, v) then
			return true
		end
	end
end

Nevermind, apparently with the increasing number of parts, using the vector3 method would check each part individually, whereas spatial query would quite literally just check the space, making it a ton more optimized if there are already alot of parts.

1 Like