Page 1 of 1
How to get the x coordinate and y coordinate of tilesheet
Posted: Tue Feb 08, 2011 8:57 pm
by jjackdev
I currently have a problem finding the x and y coordinates of a tilesheet with a Tile Id or index. What I need is a way to get the x index and y index of a tile on a tilesheet from an index.
Re: How to get the x coordinate and y coordinate of tilesheet
Posted: Tue Feb 08, 2011 9:05 pm
by pubby8
jjackdev wrote:I currently have a problem finding the x and y coordinates of a tilesheet with a Tile Id or index. What I need is a way to get the x index and y index of a tile on a tilesheet from an index.
Given index
x coordinate (in terms of tiles) will be index modulo width
y coordinate (in terms of tiles) will be index divided by height floored
Code: Select all
x = index % width;
y = index / height;
Re: How to get the x coordinate and y coordinate of tilesheet
Posted: Tue Feb 08, 2011 9:47 pm
by xiphirx
I believe you're doing this in a loop?
if so... (assumes tiles are 32x32
Code: Select all
unsigned short j = 0; //I'm unsigned short crazy lately, ignore it if you want d:
unsigned short i = 0;
for (unsigned short i = 0; i < tilesheet.w/32; i++)
{
if (i * 32 >= tilesheet.w && j*32 < tilesheet.h)
{
j++;
i=0;
}
x = i*32;
y = j*32
}
I don't remember if you can touch the counting variable in a for loop, might not be able to...
Re: How to get the x coordinate and y coordinate of tilesheet
Posted: Tue Feb 08, 2011 10:19 pm
by pubby8
If you are doing it that way, it is more compact to put into 2 loops:
Code: Select all
for(unsigned int y = 0; y < height/32; ++y)
for(unsigned int x = 0; x < width/32; ++x) {
}
Re: How to get the x coordinate and y coordinate of tilesheet
Posted: Tue Feb 08, 2011 10:24 pm
by xiphirx
Actually
Code: Select all
unsigned short j = 0; //I'm unsigned short crazy lately, ignore it if you want d:
for (unsigned short i = 0; i < (tilesheet.w/32)*(tilesheet.h*32); i++)
{
if (i * 32 >= tilesheet.w)
{
j++;
i=0;
}
x = i*32;
y = j*32
}
Re: How to get the x coordinate and y coordinate of tilesheet
Posted: Tue Feb 08, 2011 10:31 pm
by JesseGuarascia
pubby8 wrote:If you are doing it that way, it is more compact to put into 2 loops:
Code: Select all
for(unsigned int x = 0; x < width / 32; x++)
for(unsigned int y = 0; y < height / 32; y++) {
}
}
That's pretty much the best way to do it. Just be sure that when you're clipping your image, using a tileID for accessing specific tiles, to use a single dimensional array. No point of indexing a two dimensional one.
Re: How to get the x coordinate and y coordinate of tilesheet
Posted: Tue Feb 08, 2011 10:31 pm
by pubby8
xiphirx wrote:Actually
Code: Select all
unsigned short j = 0; //I'm unsigned short crazy lately, ignore it if you want d:
for (unsigned short i = 0; i < (tilesheet.w/32)*(tilesheet.h*32); i++)
{
if (i * 32 >= tilesheet.w)
{
j++;
i=0;
}
x = i*32;
y = j*32
}
This will run forever.
Re: How to get the x coordinate and y coordinate of tilesheet
Posted: Tue Feb 08, 2011 10:47 pm
by xiphirx
Fixed
Code: Select all
unsigned short j = 0; //I'm unsigned short crazy lately, ignore it if you want d:
for (unsigned short i = 0; i < (tilesheet.w/32)*(tilesheet.h*32); i++)
{
if (i * 32 >= tilesheet.w)
{
j++;
}
x = i*32;
y = j*32
}
Re: How to get the x coordinate and y coordinate of tilesheet
Posted: Tue Feb 08, 2011 10:52 pm
by JesseGuarascia
xiphirx wrote:Fixed
Code: Select all
unsigned short j = 0; //I'm unsigned short crazy lately, ignore it if you want d:
for (unsigned short i = 0; i < (tilesheet.w/32)*(tilesheet.h*32); i++)
{
if (i * 32 >= tilesheet.w)
{
j++;
}
x = i*32;
y = j*32
}
Still no, lol. x = i*32, will make the clips go to 100 * 32 px on the x axis, on a tilesheet of 10 x 10 of tiles. :P Just use two for loops; that's the best way...
Re: How to get the x coordinate and y coordinate of tilesheet
Posted: Wed Feb 09, 2011 4:15 am
by N64vSNES
pubby8 wrote:jjackdev wrote:I currently have a problem finding the x and y coordinates of a tilesheet with a Tile Id or index. What I need is a way to get the x index and y index of a tile on a tilesheet from an index.
Given index
x coordinate (in terms of tiles) will be index modulo width
y coordinate (in terms of tiles) will be index divided by height floored
Code: Select all
x = index % width;
y = index / height;
This is how I'd normally handle it.
Re: How to get the x coordinate and y coordinate of tilesheet
Posted: Wed Feb 09, 2011 2:10 pm
by jjackdev
I tried the modulo and division to find the x and y and it worked but not the way I wanted it to. It gave me the wrong tile. I then tried creating a 2d array full of indexes and that gave me the results like the modulo and division way. I imagine that maybe my texture coordinate generation for openGL might not be working. Here is the generation code.
Code: Select all
TextureCoordinates CTilesheet::getTilesheetCoordinates(int desiredTileIndexX, int desiredTileIndexY)
{
GLfloat textureCoordinateS1;
GLfloat textureCoordinateT1;
GLfloat textureCoordinateS2;
GLfloat textureCoordinateT2;
textureCoordinateS1 = ((desiredTileIndexX) * (tileSize)) / textureWidth;
textureCoordinateT1 = ((desiredTileIndexY) * (tileSize)) / textureHeight;
textureCoordinateS2 = ((desiredTileIndexX + 1) * (tileSize)) / textureWidth;
textureCoordinateT2 = ((desiredTileIndexY + 1) * (tileSize)) / textureHeight;
TextureCoordinates textureCoordinates;
textureCoordinates.s1 = textureCoordinateS1;
textureCoordinates.t1 = textureCoordinateT1;
textureCoordinates.s2 = textureCoordinateS2;
textureCoordinates.t2 = textureCoordinateT2;
return textureCoordinates;
}
And the code that is given the index of the tile is here.
Code: Select all
TextureCoordinates CTilesheet::getTilesheetCoordinates(int desiredTile)
{
int amountOfTilesPerRow = ((int)textureWidth/(int)tileSize);
int amountOfRows = (textureHeight/tileSize);
return getTilesheetCoordinates(desiredTile%amountOfRows, desiredTile/amountOfTilesPerRow);
}
Could someone give insight if this texture coordinate generation is incorrect.
Re: How to get the x coordinate and y coordinate of tilesheet
Posted: Wed Feb 09, 2011 4:40 pm
by pubby8
1. I assume your tiles are set up like:
[0][1][2][3]
[4][5][6][7]
etc where number is index.
2. Make sure you are doing floating point division.
textureCoordinateS1 = ((float)(desiredTileIndexX) * (tileSize)) / (float)textureWidth;
3. Make sure you got your openGL positions correct. I believe (0,0) is bottom-right corner, while you may be assuming it is upper-left. Flipping the "y" values over 0.5 should fix this.
Re: How to get the x coordinate and y coordinate of tilesheet
Posted: Wed Feb 09, 2011 4:43 pm
by jjackdev
Thank you for the help but it was Tiled which was outputing wierd GIDS which were one more than what they should be and for the first row they were fine.