dougieddug
In a nutshell:
1. Determine the size of your grid tiles such as 16x16
2. Take the x coordinate of your sprite such as 77.
3. Divide that coordinate by the size of your grid tile so 77/16 = 4.8125
4. Discard the .8125 by using the floor(77/16) = 4
5. Multiply your new value by the size of your grid tile so 4 x 16 = 64.
What this does it removes the fractional component of each coordinate. So any x coordinate from 0 to 15 will snap to 0. From 16 to 31 will snap to 16. From 32 to 47 will snap to 32. From 48 to 64 will snap to 48.