# Tile sheet spriting math ?

This topic is 2157 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### 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.

##### 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

##### 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.

##### Share on other sites

Just add subtract one before calculating the index then.

I illustrated the math here.

##### 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

##### Share on other sites

thanks Servant of the Lord i fixed my program it running great using your method :)

1. 1
2. 2
Rutin
20
3. 3
khawk
17
4. 4
A4L
14
5. 5

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633759
• Total Posts
3013720
×