Math for determining tiles of a sprite sheet (SFML)

Recommended Posts

Posted (edited)

Hello,

So I'm reading a book called SFML By Example C++ .  I'm having trouble using my own sprite sheet and I have been messing around with their equation for getting the correct tile.

In the Map class, they have an enum called Sheet that has the TileSize = 32, Sheet_Width = 250, Sheet_Height = 250.

They determine the texture rect like this

sf::IntRect tileBoundaries(m_id % (Sheet::Sheet_Width / Sheet::Tile_Size) * Sheet::Tile_Size,
m_id / (Sheet::Sheet_Height / Sheet::Tile_Size) * Sheet::Tile_Size,
Sheet::Tile_Size, Sheet::Tile_Size);

What I noticed is the Y-axis does not determine the correct tile if the sheet isn't the same dimensions. So what I did is divide m_id by 2 and it seems to work for the most part. I also was using 64 for the tile size instead of 32

...., m_id / 2 / (Sheet::Sheet_Height / Sheet::Tile_Size) * Sheet::Tile_Size,  Sheet::Tile_Size, Sheet::Tile_Size);

I decided to just run their formula vs my formula adjustment vs keeping the dimensions the same as the X axis in the console to see what was going on. Their formula was always off, and my adjustment by dividing by 2 worked until a specific point. When I was plugging in 896 and moving up by 64 to test numbers it began to fall apart.

Is there a specific equation I could do with Y where I wouldn't have to worry about it falling apart? I don't have a sprite sheet that big, but 15 sprites down don't seem unrealistic and I'd like to be accurate every time. What confuses me is using the same dimensions for X and Y always worked, but I'm not sure if that could lead to some unwanted problems. I'd like to say it would not and I'd like to confirm that. I just find it pointless to have a Sheet_Height if the equation needs equal dimensions as the Sheet_Width. They specifically mention that the size doesn't' have to be the same. But it might as well be called SheetSize.

Thanks, I hope that makes sense.

Edited by RamblingBaba

Share on other sites

Both x and y should use the same "(Sheet::Sheet_Width / Sheet::Tile_Size)"  as divisor, no Sheet::Sheet_Height in the y coordinate computation.

Create an account

Register a new account

• Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 9
• 56
• 18
• 28