Followers 0

Tile sheet spriting math ?

6 posts in this topic

ok i got a tilesheet of 1600x1600 in size and each tile is 32px x 32px

i want to assign the tile like 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,etc

but im confused on the math on how to convert the tile number to the sprite_x_start and sprite_y_start

    void draw_tile(int tile_num, int x, int y, SDL_Surface *output_surface) {

draw_sprite(sprite_sheet, output_surface, draw_x_spot, draw_y_spot ,sprite_x_start, sprite_y_start ,32,32);

}
Edited by MrPhoenix
0

Share on other sites

I recommend starting tile_num at 0, not 1!

Would be similar to this

//50 x 50 tiles
sprite_x_start = 32 * (tile_num % 50);
sprite_y_start = 32 * (tile_num / 50);

assuming sprite_y_start is an integer.

2

Share on other sites

Aercom i have to have the first tile set at 1 since that the way the map file is generated by tiled

0

Share on other sites

Subtract one from the values you read from the map and then work with zero-based indexing. The math turns out so much cleaner with zero-based indexing.

1

Share on other sites

Just add subtract one before calculating the index then.

I illustrated the math here.

0

Share on other sites

struct Rect

{

int x;

int y;

};

All you need to do is divide the tile image width and height by whatever the tile width and tile height you want. Then from that point you can just create an nxm Rect then assign the x and y value for each one of the tile. Then when you reference an tile from Tiles you can just subtract 1 from it to reference the proper rect index and that should contains your x and y value of where in the image that tile begins at. You do not need the width and height since you already know it is 32x32

Edited by BornToCode
0

Create an account

Register a new account