Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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 stackoverflow.com Jul 10 '11 at 22:58

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

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

3 Answers 3

up vote 5 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
3  
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
add comment

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.

share|improve this answer
    
he said making one, not finding one. –  Randomman159 Jul 11 '11 at 10:03
5  
@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
1  
@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
add comment

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
add comment

Your Answer

 
discard

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.