Imagine you have a 2d plane in C++, where the X go from 0 to MaxX (there is no negative X) and a Y go from 0 to MaxY (go down, not up like normal irl geometry, and no NegativeY)
so you would have something like this:
For example MaxX = 6, MaxY = 4
So as you can see there is a total of MaxX * MaxY square, which is 24 (0 to 23)
Assuming each square is 32x32
How would u write a function that give the coordinate of each square given their ID (number between 0 - 23)? write it dynamically
I tried but it didnt work this is what i got:
1 2 3 4 5 6 7 8 9 10 11
void Tile::CalXY()
{
int MaxTilePerRow = TILE_WIDTH / 32;
int HowManyRow = TILE_HEIGHT / 32;
int foo = TileID / HowManyRow;
int bar = TileID % HowManyRow;
box.x = bar * 32; // this is the X coordinate
box.y = foo * 32; // this is the Y coordinate. Time 32 because each box is 32 apart
}
int column = id % maxX ;
int row = id / maxX ;
box.x = column * 32 ;
box.y = row * 32 ;
Using TILE_WIDTH and TILE_HEIGHT in place of the magic number 32 in the last two lines would be better but, given your attempt, I'm not certain exactly what they're supposed to represent.