Copy a part directly on top of itself? [Math heavy]

Hello! I’ve been working on recreating the classic roblox copy tool and I wanted it to be able to copy a part and place that copy directly on top of the original part, as close as it can be without intersecting it. I noticed that when turning Collisions on from roblox studio, pressing Ctrl + D while selecting a part reproduces the desired functionality, as shown in the following pictures:

Before copying

After copying (new part in yellow)

I then tried to recreate that with the following script placed inside game.Workspace:

local part = game.Workspace.Part -- The part to be copied
local newPart = part:Clone()
newPart.Parent = game.Workspace
newPart.BrickColor = BrickColor.new("Pastel yellow")
local maxCos = part.Size.Y/math.sqrt(part.Size.X^2+part.Size.Y^2)

while true do
	local a = part.CFrame.UpVector
	local b = Vector3.new(0,1,0)
	local cross = math.abs(a:Dot(b))
	if cross >= maxCos then
		newPart.CFrame = part.CFrame + Vector3.new(0,part.Size.Y/cross,0)
	else
		cross = math.sqrt(1-cross^2)
		newPart.CFrame = part.CFrame + Vector3.new(0,part.Size.X/cross,0)
	end
	wait()
end

The previous script works fine as long as it’s used on a part rotated along the X or Y axis, as show in the following gifs:

Along the X axis
3c107b1226f2fb5e01462bc8e032f109

Along the Y axis
bc7adb9107db0f8e62523153182c17fe

However, undesired results arise when trying to run the script on parts rotated along the Z axis:

Along the Z axis
76c516cb6baee7830d31630b9be95c10

How can I fix the script for it to work on parts rotated in any direction? Any form of documentation about the subject, even if it does not directly answer my question, would be greatly appreciated.

1 Like

it prolly has to do with :Dot() bc it doesnt go negative

1 Like

I don’t have much to add but I have been trying to figure out how to solve this problem. It seems to me like it’s because you only offset based on the size of the Y axis, and when you are rotating by the X axis you should offset based on that axis size. I’m not quite sure how to solve this at the moment without using conditionals and checking the orientation, but wanted to share in case you had better ideas.

I should also mention that there is a roblox built-in dragger class, if you want to use that to get the old feeling of the dragger.

1 Like

I use math.abs to make sure it doesn’t, otherwise the copied part would be located under the original and I want it to always be placed on top of it.

Thanks for the insight on the subject. I haven’t figured out how to work it out either, even when checking for the part’s orientation. I will try using raycasting until I find a solution which does not involve it.