• Advertisement
Sign in to follow this  

Convert X,Y mouse pos to row, column

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Simply divide the mouse coordinates by the tile size:

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

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by cvg_james
Nested 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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement