# Snapping to centered grid killing me

**URL:** <https://forum.kirupa.com/t/snapping-to-centered-grid-killing-me/279558>\
**Category:** Uncategorized\
**Created:** [January 16, 2009, 2:12am UTC](https://forum.kirupa.com/t/snapping-to-centered-grid-killing-me/279558 "2009-01-16T02:12:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![grandpaw\_broon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/grandpaw_broon/32/3985_2.png) [@grandpaw\_broon](https://forum.kirupa.com/u/grandpaw_broon)\
**Post date:** [January 16, 2009, 2:12am UTC](https://forum.kirupa.com/t/snapping-to-centered-grid-killing-me/279558/1 "2009-01-16T02:12:38Z")

</div>

Hi all.

I am trying to do something relatively simple. I want the mouse position to “snap” a sprite called highlight to a grid.

In a top left world, this works…

highlight.x = ((this.mouseX) - (this.mouseX%gridRes));  
highlight.y = ((this.mouseY) - (this.mouseY%gridRes));

The problem is, that for multiple reasons, i need to use a centered grid. I have drawn the grid correctly but the snapping is weird now and does not work correctly. It snaps to the center of my grid squares and not the corners. This has something to do with me now having negative values on my grid but no idea how to fix it because i do not really understand the Modulus operator anyway far less negative values.

Can you help?

Cheers.
