Public Group

# Convert X,Y mouse pos to row, column

This topic is 4707 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Each square in my game occupies 32 x 32 pixels. There are 10 rows by 10 columns. For any given mouse click I can record the X and Y pixel coordinates. How do I take this value and convert it into which row and column (0 thru 9) ???? For example, 126,207 is what row and column?

##### Share on other sites
Simply divide the mouse coordinates by the tile size:

(126 / 32) = 3.937 or 3 (if doing division with integers)
(207 / 32) = 6.469 or 6

##### Share on other sites
sorry but "Nested IF statements..." is some very very bad coding style, jdh15 was right, simply dividing it does the job.

##### Share on other sites
Quote:
 Original post by cvg_jamesNested IF statements...*** Source Snippet Removed ***I'm not even sure if that is syntactically correct, but you can give it a shot.

Tis a fairly ugly unintuitive way of doing it (no offence intended cvg_james). I would try jdh15's method but I have a feeling I would have to play around with the rounding because I haven't done that before.

Edit: Beaten like a dirty sav trying to type with marshmellow fingers

##### Share on other sites
the best way of going about is use an integer...

int TileR = Mouse.y / 32;

since integers always round down... if its 1.23 it will round to 1. to shut the compiler up just do

int TileR = (int)(Mouse.y / 32);
int TileC = (int)(Mouse.x /32);

it should tell you the coordinates of the users mouse

[Edited by - willthiswork89 on December 29, 2005 7:45:04 PM]

##### Share on other sites
Thanks...I have this working now.

1. 1
Rutin
41
2. 2
3. 3
4. 4
5. 5

• 10
• 27
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633404
• Total Posts
3011676
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!