Opengl 2D problem

Started by
12 comments, last by Renran 16 years ago
Quote:Original post by Renran
And Decrius, my code is 3D and I read colours in 1 frame (backbuffer)to find the
position of an object = no math involved + always correct


Might be offtopic, but with math it is always correct too (if it draws tiles, even with offset etc) and is much quicker/efficient and requires less code.

[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Advertisement
Do You have an example of this code?
Or a site where I can read about it?

Thanks and Greetings,

RenRan
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.

[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Thank You Decrius, I'm gonna try it out.

Greetings,

RenRan

This topic is closed to new replies.

Advertisement