# Simple math help

Okay so I’m making a grab tool and a placement tool and I want to align the parts onto 1 stud. Can someone make a function that takes a part and it’s position and then returns the position the part would have to be to align a 1x1 grid (preferably vector3). Next thing is I want to make a rotate and tilt function so the part rotates and tilts, ATM I have rotate but idk how I would do tilt since tilt should rotate based off a certain face.

function snapToGrid(currentPosition)
return Vector3.new(math.floor(currentPosition.X), math.floor(currentPosition.Y), math.floor(currentPosition.Z))
end

[quote] function snapToGrid(currentPosition)
return Vector3.new(math.floor(currentPosition.X), math.floor(currentPosition.Y), math.floor(currentPosition.Z))
end [/quote]

That doesn’t work because when you rotate a part with an uneven size on the Z or X it doesn’t snap properly

EDIT:

Try getting the excess size, so round the size of each direction, then get the remainder and align it to the grid - the excess size?
that might work

```do local f = math.floor function snapToGrid(position, part) local x, y, z = position.X, position.Y, position.Z local sX, sY, sZ = part.Size.X, part.Size.Y, part.Size.Z return Vector3.new(f(x+.5) + (sX%2)/2, f(y+.5) + (sY%2)/2, f(z+.5) + (sZ%2)/2) end end```

[quote] ```do local f = math.floor function snapToGrid(position, part) local x, y, z = position.X, position.Y, position.Z local sX, sY, sZ = part.Size.X, part.Size.Y, part.Size.Z return Vector3.new(f(x+.5) + (sX%2)/2, f(y+.5) + (sY%2)/2, f(z+.5) + (sZ%2)/2) end end``` [/quote]

takes back thumbs up, lol That doesn’t work either it results in the same problem as using just math.floor()

[quote] ```do local f = math.floor function snapToGrid(position, part) local x, y, z = position.X, position.Y, position.Z local sX, sY, sZ = part.Size.X, part.Size.Y, part.Size.Z return Vector3.new(f(x+.5) + (sX%2)/2, f(y+.5) + (sY%2)/2, f(z+.5) + (sZ%2)/2) end end``` [/quote]

takes back thumbs up, lol That doesn’t work either it results in the same problem as using just math.floor()

Are they always offset by 0.5 studs when you use mine, or?

Not always, but once I rotate the part it doesn’t snap to 1 studs. So idk if I should make some sort of offset for rotating or what.

Lol, looking at the number of replies, I thought this was answered already xD

So, that being the case, sorry for the late reply, this is what Control does:

``````function Round(Num,Precision)
if Precision == nil then Precision = 1 end
Num = Num / Precision
if Num - math.floor(Num) >= 0.5 then
return math.ceil(Num)*Precision
else
return math.floor(Num)*Precision
end
end

local GridSize = 1
local TSize = ObjectSize
local Offset = (CF-CF.p)*Vector3.new(TSize.X/2-Round(TSize.X/2,GridSize),0,TSize.Z/2-Round(TSize.Z/2,GridSize))
Hit = Vector3.new(Round(Hit.X,GridSize),Hit.Y,Round(Hit.Z,GridSize)) + Offset``````

It’s a bit snipped up from being in Control’s code, and I’ve not tested it outside of Control, but that basic math is there, while being old and a little inefficient, it should work for you

``````local function snapPartToGrid(part)
if part:IsA("BasePart") then
part.Position = Vector3.new(math.round(part.Position.X), math.round(part.Position.Y), math.round(part.Position.Z)) --math.round is more accurate in this context than math.floor/math.ceil
end
end

local function rotatePart(part, clockwise)
if part:IsA("BasePart") then
if clockwise then --perform clockwise rotation
part.Orientation += Vector3.new(0, 90, 0) --90 degree rotation along the y-axis
elseif not clockwise then --perform anticlockwise rotation
part.Orientation -= Vector3.new(0, 90, 0) --this can also be += with the 90 replaced with -90
end
end
end

local function tiltPart(part, seesaw)
if part:IsA("BasePart") then
if seesaw then --perform fowards tilt
part.Orientation += Vector3.new(90, 0, 0) --90 degree rotation along x-axis
elseif not seesaw then --perform backwards tilt
part.Orientation -= Vector3.new(90, 0, 0) --this can also be += with the 90 replaced with -90
end
end
end
``````

The “part” argument in all 3 functions would be a reference to the BasePart instance, “clockwise” and “seesaw” are parameters which expect Boolean arguments which represent whether to perform anticlockwise/clockwise rotations and backward/forward tilts respectively.