• Create Account

### #ActualServant of the Lord

Posted 09 July 2012 - 01:00 PM

If for you, everything starts at one, then just subtract 1 before mutliplying.

If x and y start at 1 instead of zero, then you use:
int index = ((TileY-1) * TilesetWidth) + (TileX - 1);

The x and y we are wanting is, "Assuming you start at the first cell, how many steps do you need to move to get to where you want?".

If you want to think of the first cell as cell 1 instead of cell 0, you can think of it like this:
"I'm at cell 1, and I want to be at cell 4, so the difference between cell 1 and 4 is (4 - 1) which is 3, so I need to move 3 steps to get to cell 4"

The % operator is modulus. Think of it like this:
int TileY = (index / TilesetWidth); //Divide index by the width of the tileset. The result is Y, but save the remainder of the division!
int TileX = remainder;

If it helps you, you could mentally calculate it like this:
int TileY = (index / TilesetWidth);
int TileX = (index - TileY); //Whatever is left from the index, after TileY was calculated.

In the examples in the earlier posts, if the index is 11, we divide by the TilesetWidth (which is 5), so Y = (11 / 5) which is 2. But it's not an even division! There is still a remainder of 1, and that remainder goes to X.

### #3Servant of the Lord

Posted 09 July 2012 - 01:00 PM

If for you, everything starts at one, then just subtract 1 before mutliplying.

If x and y start at 1 instead of zero, then you use:
int index = ((TileY-1) * TilesetWidth) + (TileX - 1);

The x and y we are wanting is, "Assuming you start at the first cell, how many steps do you want to move to get to where you want?".

If you want to think of the first cell as cell 1 instead of cell 0, you can think of it like this:
"I'm at cell 1, and I want to be at cell 4, so the difference between cell 1 and 4 is (4 - 1) which is 3, so I need to move 3 steps to get to cell 4"

The % operator is modulus. Think of it like this:
int TileY = (index / TilesetWidth); //Divide index by the width of the tileset. The result is Y, but save the remainder of the division!
int TileX = remainder;

If it helps you, you could mentally calculate it like this:
int TileY = (index / TilesetWidth);
int TileX = (index - TileY); //Whatever is left from the index, after TileY was calculated.

In the examples in the earlier posts, if the index is 11, we divide by the TilesetWidth (which is 5), so Y = (11 / 5) which is 2. But it's not an even division! There is still a remainder of 1, and that remainder goes to X.

### #2Servant of the Lord

Posted 09 July 2012 - 01:00 PM

If for you, everything starts at one, then just subtract 1 before mutliplying.

If x and y start at 1 instead of zero, then you use:
int index = ((TileY-1) * TilesetWidth) + (TileX - 1);

The x and y we are wanting is, "Assuming you start at the first cell, how many steps do you want to move to get to where you want?".

If you want to think of the first cell as cell 1 instead of cell 0, you can think of it like this:
"I'm at cell 1, and I want to be at cell 4, so the difference between cell 1 and 4 is (4 - 1) which is 3, so I need to move 3 steps to get to cell 4"

The % operator is modulus. Think of it like this:
int TileY = (index / TilesetWidth); //Divide index by the width of the tileset. The result is Y, but save the remainder of the division!
int TileX = remainder;

If it helps you, you could mentally calculate it like this:
int TileY = (index / TilesetWidth);
int TileX = (index - TileY); //Whatever is left from the index, after TileY was calculated.

In the examples in the earlier posts, if the index is 11, we divide by the TilesetWidth (which is 5), so Y = (11 / 5) which is 2. But it's not an even division! There is still a remainder of 1, and that remainder goes to X.

### #1Servant of the Lord

Posted 09 July 2012 - 12:59 PM

If for you, everything starts at one, then just subtract 1 before mutliplying.

If x and y start at 1 instead of zero, then you use:
int index = ((TileY-1) * TilesetWidth) + (TileX - 1);

The x and y we are wanting is, "Assuming you start at the first cell, how many steps do you want to move to get to where you want?".

If you want to think of the first cell as cell 1 instead of cell 0, you can think of it like this:
"I'm at cell 1, and I want to be at cell 4, so the difference between cell 1 and 4 is (4 - 1) which is 3, so I need to move 3 steps to get to cell 4"

The % operator is modulus. Think of it like this:
int TileY = (index / TilesetWidth); //Divide index by the width of the tileset. The result is Y, but save the remainder of the division!
int TileX = remainder;

If it helps you, you could mentally calculate it like this:
int TileY = (index / TilesetWidth);
int TileX = (index - TileY); //Whatever is left from the index, after TileY was calculated.

In the examples in the earlier posts, if the index is 11, we divide by the TilesetWidth (which is 5), so Y = (11 / 5) which is 2. But it's not an even division! There is still a remainder of 1, and that remainder goes to X.

PARTNERS