I want to have a building system for building walls and floors and things, but I don’t know how to round to numbers like 4 to make things snap to a grid. How do I do this specific thing?
Maybe this can be of use. It’s a quick forking of mine. This is only reliable for a grid system that has the same grid size. (Like Minecraft or Roblox Islands)
function PositionToGrid(initialValue,step)
local roundedValue = math.floor(initialValue/step)*step
return roundedValue
end
function BlockPosition(BlockName, BlockCFrame, BlockSize, MouseHitPosition)
return PositionToGrid(MouseHitPosition.X+(BlockSize.Y/2), BlockSize.Y), PositionToGrid(MouseHitPosition.Y+(BlockSize.Y/2), BlockSize.Y), PositionToGrid(MouseHitPosition.Z+(BlockSize.Y/2), BlockSize.Y) – The reason for that we use BlockSize.Y all the way through, is because we have a Hitbox/BorderBlock that fills out the whole area of the model/block. (And it will be same length on all axes)
end
DummyBlock:PivotTo(CFrame.new(BlockPosition(BlockName, BlockCFrame, BlockSize, MouseHitPosition))) --The reason for all the Block values is seperated, is due to my serverside validation check aswell.
Why not use math.round(initialValue)
instead of math.floor(initialValue/step)*step
Hi! That single line of code is forked.
When I take a closer look at it, it might have something to do with, that the *step doesn’t get rounded, but the rest does.
Yes, but if you imagine you are using a 10x10 grid.
math.floor(1/10) or (9/10) will equal 0 either way.
math.round(1/10) is 0, but math.round(9/10) is 1.
I’m not sure it goes for grids with decimals.
I hate to revive this thread, but I agree with @Scottifly here.
“Flooring” always pushes numbers down, like how the flooring of a house will always be on the bottom.
“Ceiling” always pushes numbers up, like how the ceiling of a house is always on the top.
Rounding can put numbers to the top or bottom, whichever is closest.
Flooring or ceiling grids will never feel (especially for a placement system) as nice as rounding them.
Correct, math.round is better for grid placement. At least when someone stumbles upon this thread again, they will know to go with math.round over math.floor / math.ceiling