How to get the x coordinate and y coordinate of tilesheet

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
jjackdev
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Mon Aug 30, 2010 11:55 pm

How to get the x coordinate and y coordinate of tilesheet

Post 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.
pubby8
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 58
Joined: Mon Feb 07, 2011 12:22 am

Re: How to get the x coordinate and y coordinate of tilesheet

Post 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;
Last edited by pubby8 on Tue Feb 08, 2011 10:24 pm, edited 1 time in total.
User avatar
xiphirx
Chaos Rift Junior
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

Post 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...
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 :)
pubby8
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 58
Joined: Mon Feb 07, 2011 12:22 am

Re: How to get the x coordinate and y coordinate of tilesheet

Post 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) {
	
}
User avatar
xiphirx
Chaos Rift Junior
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

Post 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
}
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 :)
JesseGuarascia
Chaos Rift Cool Newbie
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

Post 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.
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)
pubby8
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 58
Joined: Mon Feb 07, 2011 12:22 am

Re: How to get the x coordinate and y coordinate of tilesheet

Post 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.
User avatar
xiphirx
Chaos Rift Junior
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

Post 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
}
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 :)
JesseGuarascia
Chaos Rift Cool Newbie
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

Post 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...
-- Jesse Guarascia

I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: How to get the x coordinate and y coordinate of tilesheet

Post 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.
jjackdev
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Mon Aug 30, 2010 11:55 pm

Re: How to get the x coordinate and y coordinate of tilesheet

Post 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.
pubby8
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 58
Joined: Mon Feb 07, 2011 12:22 am

Re: How to get the x coordinate and y coordinate of tilesheet

Post 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.
jjackdev
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Mon Aug 30, 2010 11:55 pm

Re: How to get the x coordinate and y coordinate of tilesheet

Post 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.
Post Reply