I'm getting the bottom surface of a part incorrectly, need some help. (Math question)

I am not the best at math.
I am currently writing a simple function to split a part evenly based on its size.

Original brick:
image
(Size: 10, 1, 10)

What I want:
image

My code:

local function cut(part)
	local sX, sY, sZ = part.Size.X, part.Size.Y, part.Size.Z
	local bottom = part.CFrame * CFrame.new(0, -sY / 2, 0)

	for i = 1, sX / 2, 1 do -- part size is always going to be divisible by 2
		local clone = part:Clone()
		clone.Size = Vector3.new(2, sY, sZ)
		clone.CFrame = bottom + Vector3.new(0, i * 2, 0)
		clone.Parent = game.Workspace.Terrain
	end
	
	part:Destroy()
end

My result:

It seems to split fine, but it offsets the position weirdly. I think I got the bottom surface of the part incorrectly.

If anyone could help me with that (should be a simple fix really) I would appreciate it. I’ve done my fair share of research on DevForums and Google too.

2 Likes

Took me a little while to figure out what was going on because I think you have your part rotated in a weird direction. You should make the parts orientation (0, 0, 0) instead of like (0, 180, -90) (at least that’s what I think its oriented at).

What was tripping you up was probably that the part size and part position vector values were switched. On the part size, the X vector was the height instead of the Y. So you were multiplying the wrong thing. I wrote some code below fixing the issue using your current part orientation, but for future reference try and make everything oriented normally so that weird bugs like this don’t occur. :slightly_smiling_face:

local function cut(part)
	local sX, sY, sZ = part.Size.X, part.Size.Y, part.Size.Z
	local bottom = part.CFrame * CFrame.new(sX/2 + 1, 0, 0)
	for i = 1, sX / 2 do -- part size is always going to be divisible by 2
		local clone = part:Clone()
		clone.Size = Vector3.new(2, sY, sZ)
		clone.CFrame = bottom + Vector3.new(0, i*2, 0)
		clone.Parent = game.Workspace.Terrain
	end

	part:Destroy()
end