# How to make map editor with XNA?

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.

-

## migrated from stackoverflow.comJul 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

## 3 Answers

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;
``````
-
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. http://gleed2d.codeplex.com/

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.

-
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

-