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:
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