Jump to content
  • Advertisement
Sign in to follow this  
flashinpan

Convert X,Y mouse pos to row, column

This topic is 4529 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
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
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!