How do I prevent parts from intersecting while maintaining support for rotating parts

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Prevent parts from intersecting after they’ve been rotated.

  2. What is the issue? The if statements based off of faces go wack because when you rotate the thing that faces are all off.

  3. What solutions have you tried so far? I tried removing the if statements and just using the mouse position but that failed.

local Buttons = game.Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("Frame"):WaitForChild("ScrollingFrame")
local RunService = game:GetService("RunService")
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local Models = game:GetService("ReplicatedStorage").Parts
local connection
local Rem = game:GetService("ReplicatedStorage").placeBlock
local UpdatePos
local selected = 1
local UIS = game:GetService("UserInputService")
local CAS = game:GetService("ContextActionService")
local SaveRot
local BlockName

local Blocks = {
	Models.Plank, 
	Models.Marble,
	Models.Rod
}

local CloneForYou = Blocks[selected]:Clone()

local ButtonTbl = {
	Buttons.Planks,
	Buttons.Marble,
	Buttons.Rod
}

for i,v in pairs(ButtonTbl) do
	print(v.Name)
	v.Activated:Connect(function()
		BlockName = i
		selected = i
		CloneForYou:Destroy()
		CloneForYou = Blocks[selected]:Clone()
	end)
end

Mouse.Button1Down:Connect(function()
	if script.Parent.Parent.Name == "Backpack" then return end
	SaveRot = CloneForYou[Blocks[selected].Name].Orientation
	Rem:FireServer(UpdatePos, CloneForYou[Blocks[selected].Name].Orientation, selected)
	CloneForYou:Destroy()
	CloneForYou = Blocks[selected]:Clone()
	CloneForYou[Blocks[selected].Name].Orientation = SaveRot
end)

local function rot(action, InputState, obj)
	if script.Parent.Parent ~= game.Players.LocalPlayer.Character or InputState ~= Enum.UserInputState.Begin then return end
	if action == "R" then
		CloneForYou[Blocks[selected].Name].Orientation += Vector3.new(0, 90, 0)
	elseif action == "T" then
		CloneForYou[Blocks[selected].Name].Orientation += Vector3.new(0, 0, 90)
	end
end

script.Parent.Equipped:Connect(function()
	local function updatePos()
		Mouse.TargetFilter = CloneForYou[Blocks[selected].Name]
		CloneForYou.Parent = workspace
		
		local X = Mouse.Hit.p.X
		local Y = 1
		local Z = Mouse.Hit.p.Z
		local TargetSurface = Mouse.TargetSurface.Name
		local MousePos = Mouse.Hit.p

		if Mouse.Target then
			if TargetSurface == "Top" then
				Y = MousePos.Y + 0.5
			elseif TargetSurface == "Bottom" then
				Y = MousePos.Y - 0.5
			elseif TargetSurface == "Left" then
				X = MousePos.X - 1
				Y = Mouse.Hit.p.Y
			elseif TargetSurface == "Right" then
				X = MousePos.X + 0.5
				Y = Mouse.Hit.p.Y
			elseif TargetSurface == "Front" then
				Z = MousePos.Z - 1
				Y = Mouse.Hit.p.Y
			elseif TargetSurface == "Back" then
				Z = MousePos.Z + 0.5
				Y = Mouse.Hit.p.Y
			end 
		end
				
		UpdatePos = Vector3.new((X + 0.5) - (X + 0.5) % 1, (Y + 0.5) - (Y + 0.5) % 1, (Z + 0.5) - (Z + 0.5) % 1)
		if ButtonTbl[selected].Name == "Rod" then
			UpdatePos += Vector3.new(0,1,0)
			local Z = CloneForYou.Rod.Orientation.Z
			if Z == 90 or Z == 270 or Z == -90 or Z == -270 then
				UpdatePos -= Vector3.new(0, 1.5, 0)
			end
		end
		CloneForYou[Blocks[selected].Name].Position = UpdatePos
		
		if script.Parent.Parent.Parent.Parent.Name == "Players" then
			CloneForYou:Destroy()
			CloneForYou = Blocks[selected]:Clone()
			connection:Disconnect()
		end
	end
	connection = RunService.Heartbeat:Connect(updatePos)
end)

CAS:BindAction("R", rot, false, Enum.KeyCode.R)
CAS:BindAction("T", rot, false, Enum.KeyCode.T)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

What you’re trying to do is essentially collision detection between two cuboids. You can look up algorithms to do that, or just use Rotated Region 3 module that’s freely available.

1 Like