🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Math for determining tiles of a sprite sheet (SFML)

Started by
1 comment, last by Alberth 5 years, 3 months ago

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.

Advertisement

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.

This topic is closed to new replies.

Advertisement