PDA

View Full Version : Snapping to centered grid killing me.



grandpaw broon
January 15th, 2009, 09:12 PM
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.

scottc
January 15th, 2009, 09:20 PM
just add
+ gridRes/2

sounds like it should fix it.

grandpaw broon
January 16th, 2009, 05:37 AM
Thanks Scot.

It kind of works a bit better but its not 100 percent solved.

If i move my mouse to the left of the center of the screen then it seems i need to lose it.

So say this is my grid

-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5

If i point the mouse cursor at 2 then the highlight will snap to 2

however

if i point the mouse cursor to -3 then the highlight will snap to -2.... this makes it non-consistant.

Any other ideas?

Cheers.