Quote:Original post by Renran
Do You have an example of this code?
So, I guess you have the X and Y coordinate of a certain pixel and want to know which tile that is? You know the width of the tiles, and their height. Also do you know the X and Y position of where the tile grid starts.
We take a window of 1000 by 800. Where there is a field of tiles at X = 200 and Y = 100. This field has W = 600 and H = 600 (the field is from point (200, 100) to (800, 700), leaving 200 pixels from field to border left and right, and 100 pixels above and beneath).
We take a pixel, where by mouse click or something else, at point (534, 333). Since this point is of the whole screen and starts at (0, 0) we need to substract 200 from X and 100 from Y to make the pixel counting start at (200, 100), the point where we draw tiles.
If the point has values larger then or equal to 0 and smaller then 600, we go further. We divide the X and Y values by the tile width and tile height respectively. Since we need a round number, we need to 'floor' that number. That will give us the Xth and Yth number of a tile.
Assume tiles are 48 * 48.
For the X value we do this: floor((534 - 200) / 48) = the 6th tile horizontally (counting left to right)
For Y: floor((333 - 100) / 48) = the 4th tile vertically (counting up to down)
Code I use:
int x = 534;int y = 222;if (x > 199 && x < 800 && y > 99 && y < 700){ int tile_x = (int) floor((x - 200) / 48); int tile_y = (int) floor((y - 100) / 48);}
Hope this is what you are looking for.