How to get the x coordinate and y coordinate of tilesheet
Moderator: Coders of Rage
How to get the x coordinate and y coordinate of tilesheet
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
Given indexjjackdev 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.
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;
Last edited by pubby8 on Tue Feb 08, 2011 10:24 pm, edited 1 time in total.
- xiphirx
- Chaos Rift Junior
- Posts: 324
- Joined: Mon Mar 22, 2010 3:15 pm
- Current Project: ******** (Unkown for the time being)
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Contact:
Re: How to get the x coordinate and y coordinate of tilesheet
I believe you're doing this in a loop?
if so... (assumes tiles are 32x32
I don't remember if you can touch the counting variable in a for loop, might not be able to...
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
}
StarCraft II Zerg Strategy, open to all levels of players!
Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of
Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of
Re: How to get the x coordinate and y coordinate of tilesheet
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) {
}
- xiphirx
- Chaos Rift Junior
- Posts: 324
- Joined: Mon Mar 22, 2010 3:15 pm
- Current Project: ******** (Unkown for the time being)
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Contact:
Re: How to get the x coordinate and y coordinate of tilesheet
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
}
StarCraft II Zerg Strategy, open to all levels of players!
Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of
Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of
-
- Chaos Rift Cool Newbie
- Posts: 70
- Joined: Mon Dec 13, 2010 10:55 pm
Re: How to get the x coordinate and y coordinate of tilesheet
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.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++) { } }
Last edited by JesseGuarascia on Tue Feb 08, 2011 10:32 pm, edited 1 time in total.
-- Jesse Guarascia
I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
Re: How to get the x coordinate and y coordinate of tilesheet
This will run forever.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 }
- xiphirx
- Chaos Rift Junior
- Posts: 324
- Joined: Mon Mar 22, 2010 3:15 pm
- Current Project: ******** (Unkown for the time being)
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Contact:
Re: How to get the x coordinate and y coordinate of tilesheet
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
}
StarCraft II Zerg Strategy, open to all levels of players!
Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of
Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of
-
- Chaos Rift Cool Newbie
- Posts: 70
- Joined: Mon Dec 13, 2010 10:55 pm
Re: How to get the x coordinate and y coordinate of tilesheet
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...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 }
-- Jesse Guarascia
I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
Re: How to get the x coordinate and y coordinate of tilesheet
This is how I'd normally handle it.pubby8 wrote:Given indexjjackdev 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.
x coordinate (in terms of tiles) will be index modulo width
y coordinate (in terms of tiles) will be index divided by height flooredCode: Select all
x = index % width; y = index / height;
Re: How to get the x coordinate and y coordinate of tilesheet
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.
And the code that is given the index of the tile is here.
Could someone give insight if this texture coordinate generation is incorrect.
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;
}
Code: Select all
TextureCoordinates CTilesheet::getTilesheetCoordinates(int desiredTile)
{
int amountOfTilesPerRow = ((int)textureWidth/(int)tileSize);
int amountOfRows = (textureHeight/tileSize);
return getTilesheetCoordinates(desiredTile%amountOfRows, desiredTile/amountOfTilesPerRow);
}
Re: How to get the x coordinate and y coordinate of tilesheet
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.
[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
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.