Help with part breaking and some vector math

Im trying to make a part breaking algorithm for my game. This essentially divides the part into smaller parts as seen here:

Here is the code:

-- THRESHOLD SIZE: 4,4,4 MINIMUM

local blockSize = script.Parent.Size
script.Parent.ProximityPrompt.Triggered:Connect(function()
	local brickSize = Vector3.new(4, 4, 4)
	
	local brickCountX = math.floor(blockSize.X / brickSize.X)
	local brickCountOffstX = (blockSize.X % brickSize.X) / brickCountX
	
	local brickCountY = math.floor(blockSize.Y / brickSize.Y)
	local brickCountOffstY = (blockSize.Y % brickSize.Y) / brickCountY
	
	local brickCountZ = math.floor(blockSize.Z / brickSize.Z)
	local brickCountOffstZ = (blockSize.Z % brickSize.Z) /brickCountZ
	
	local offsetSize = Vector3.new(brickCountOffstX, brickCountOffstY, brickCountOffstZ)
	
	local startingPos = script.Parent.Position + Vector3.new((blockSize.X / 2), (blockSize.Y / 2), (blockSize.Z / 2)) 
	local offset = startingPos
	
	for z = 0, brickCountZ do
		offset =  Vector3.new(offset.X, offset.Y, offset.Z + (brickSize.Z * z))
		for y = 0, brickCountY do
			offset =  Vector3.new(offset.X, offset.Y - brickSize.Y, offset.Z)
			for x = 0, brickCountX do
				local brick = Instance.new("Part")
				brick.Size = brickSize + offsetSize
				brick.Orientation = script.Parent.Orientation
				offset = Vector3.new(offset.X - brickSize.X, offset.Y, offset.Z)
				brick.Position = offset
				brick.Parent = game.Workspace
			end
			offset = Vector3.new(startingPos.X, offset.Y, offset.Z)
		end
		offset = Vector3.new(offset.X, startingPos.Y, offset.Z)
	end
	
	print(brickCountX)
	print(brickCountY)
	print(brickCountZ)
	script.Parent:Destroy()

end)

Although it’s pretty simple, and has some precision problems, it works well. However, the cube has a 0,0,0 rotation. The local axis for the part is lined up with the worlds axis, so this code works fine.

However, rotating this part shows us this issue:

image

Because (as seen in the code), the values the script with are the parts world-position and its size, both of which are unaffected by rotation.

Even the top-left corner of the cube (startingPos in the script) does not match. I want to know if there is a simple way to properly make this script work with the rotation of the part without messing with a bunch of rotations and dot product garbage. Pls help

1 Like

Figured it out using my big fat brain. It came to me in a dream…

I can just add all the fragments to a model and then rotate the model:

-- THRESHOLD SIZE: 2,2,2 MINIMUM

local blockSize = script.Parent.Size
script.Parent.ProximityPrompt.Triggered:Connect(function()
	local brickSize = Vector3.new(2, 2, 2)
	
	local brickCountX = math.floor(blockSize.X / brickSize.X)
	local brickCountOffstX = (blockSize.X % brickSize.X) / brickCountX
	
	local brickCountY = math.floor(blockSize.Y / brickSize.Y)
	local brickCountOffstY = (blockSize.Y % brickSize.Y) / brickCountY
	
	local brickCountZ = math.floor(blockSize.Z / brickSize.Z)
	local brickCountOffstZ = (blockSize.Z % brickSize.Z) /brickCountZ
	
	local offsetSize = Vector3.new(brickCountOffstX, brickCountOffstY, brickCountOffstZ)
	
	local startingPos = script.Parent.Position + Vector3.new((blockSize.X / 2), (blockSize.Y / 2), (blockSize.Z / 2)) 
	local offset = startingPos
	
	local theModel = Instance.new("Model")
	theModel.Parent = game.Workspace.Map.Destructable
	
	for z = 0, brickCountZ -1 do
		offset =  Vector3.new(offset.X, offset.Y, offset.Z + (brickSize.Z * z))
		for y = 0, brickCountY -1 do
			offset =  Vector3.new(offset.X, offset.Y - brickSize.Y, offset.Z)
			for x = 0, brickCountX -1 do
				local brick = Instance.new("Part")
				brick.Size = brickSize + offsetSize
				offset = Vector3.new(offset.X - brickSize.X, offset.Y, offset.Z)
				brick.Position = offset
				brick.Parent = theModel
				brick.CollisionGroup = "Debris"
			end
			offset = Vector3.new(startingPos.X, offset.Y, offset.Z)
		end
		offset = Vector3.new(offset.X, startingPos.Y, offset.Z)
	end
	
	local rot = script.Parent.Rotation
	theModel:PivotTo(theModel:GetPivot() * CFrame.Angles(math.rad(rot.X), math.rad(rot.Y), math.rad(rot.Z)))

	script.Parent:Destroy()

end)

I just need to make it a little more robust since this code hasn’t been tested for bugs

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.