Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a problem with making map editor in XNA. I'm making a 2D tile based map editor.

The problem is, I need every tile I put in map editor to be X: 0, or X: 32, or X:64 and so on, not 1.10, 2.99, 5.62, etc. You see, it has to be exactly i*32 tiles, where i is whatever number, not decimal. Thank you if you help.

share|improve this question

migrated from Jul 10 '11 at 22:58

This question came from our site for professional and enthusiast programmers.

That's called an integer... Can you please better describe your problem? – Ricket Jul 11 '11 at 1:27
up vote 6 down vote accepted

The easiest way to "snap" coordinates to a grid is this:

    int objX = (int) Math.Floor(mouseX/tileWidth)*tileWidth;
    int objY = (int) Math.Floor(mouseY/tileHeight)*tileHeight;
share|improve this answer
If the tiles are powers of two, e.g. 32, then you can simply AND off the lower bits, objX = mouseX & (32-1); – PatrickB Jul 12 '11 at 0:44
@PatrickB Nice tip, I prefer objX = mouseX & 0x1f – bobobobo Jul 14 '11 at 1:52

Great OpenSource XNA map editor.

You can fully customize the ground grid and enable grid snapping to make maps. Also since it's opensource you can edit it to suit your exact needs.

I've used it myself.

share|improve this answer
he said making one, not finding one. – Randomman159 Jul 11 '11 at 10:03
@Randomman159 If he can't snap coordinates to a modulo of 32 he should probably find one. – Jonathan Connell Jul 11 '11 at 10:16
@3nixious: Because an individual has an issue with something doesn't mean he can't be really good at other things. Everyone thinks differently. – Bryan Harrington Jul 11 '11 at 18:48

I like the solution here, but I'm currently using

                            // for example 100 - (100 % 32 (which is 4)) = 94 
Vector2 placementPosition = mousePosition 
               - new Vector2(mousePosition.X % 32, mousePosition.Y % 32);

which works perfectly, too

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.