snap to grid math code example
Example: snap to grid math
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;