Jump to content
  • Advertisement
Sign in to follow this  

Math for determining tiles of a sprite sheet (SFML)

Recommended Posts

Posted (edited)


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 this post

Link to post
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.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!