Skip to main content
Answer was wrong, Math.floor didn't round to nearest multiple, but the nearest smaller multiple.
Source Link

You can do something like this

int gridCubeWidth = 16, gridCubeHeight = 16;

cube.Position.X = Math.Floorround(cube.Position.X / gridCubeWidth) * gridCubeWidth;
cube.Position.Y = Math.Floorround(cube.Position.Y / gridCubeHeight) * gridCubeHeight;

This basically rounds the X and Y positions to the nearest multiple of the cube dimensions. Then scales it by the cube dimensions to get the uniform position.

You can do something like this

int gridCubeWidth = 16, gridCubeHeight = 16;

cube.Position.X = Math.Floor(cube.Position.X / gridCubeWidth) * gridCubeWidth;
cube.Position.Y = Math.Floor(cube.Position.Y / gridCubeHeight) * gridCubeHeight;

This basically rounds the X and Y positions to the nearest multiple of the cube dimensions. Then scales it by the cube dimensions to get the uniform position.

You can do something like this

int gridCubeWidth = 16, gridCubeHeight = 16;

cube.Position.X = Math.round(cube.Position.X / gridCubeWidth) * gridCubeWidth;
cube.Position.Y = Math.round(cube.Position.Y / gridCubeHeight) * gridCubeHeight;

This basically rounds the X and Y positions to the nearest multiple of the cube dimensions. Then scales it by the cube dimensions to get the uniform position.

Source Link

You can do something like this

int gridCubeWidth = 16, gridCubeHeight = 16;

cube.Position.X = Math.Floor(cube.Position.X / gridCubeWidth) * gridCubeWidth;
cube.Position.Y = Math.Floor(cube.Position.Y / gridCubeHeight) * gridCubeHeight;

This basically rounds the X and Y positions to the nearest multiple of the cube dimensions. Then scales it by the cube dimensions to get the uniform position.