Dividing spritesheets into quads

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Dilir
Prole
Posts: 4
Joined: Mon Jan 27, 2020 10:49 pm

Dividing spritesheets into quads

Post by Dilir »

In the tutorial in the love2d official tutorials page, https://love2d.org/wiki/Tutorial:Animation, there's a part I don't understand. The for loop conditionals that go like this:
for y = 0, image:getHeight() - height, height do

Why are we doing this? I read the explanation on the tutorial page but sadly can't make sense of it. I would highly appreciate if anyone could explain this to me.
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Dividing spritesheets into quads

Post by pgimeno »

The subimages inside the image are each width x height. If the image's width or height is not a multiple of the subimage's width or height, and you go past the image height minus the subimage height, you would be trying to take a quad that would go past the image border.

Let's take a simplified example. Say we have a 10x10 image and the subimages are 4x4. That means that you can take 4 quads: one at (0, 0) which extends to (3, 3) inclusive, one at (4, 0) which extends to (7, 3) inclusive, one at (0, 4) which extends to (3, 7) inclusive, and one at (4, 4) which extends to (7, 7) inclusive.

If you loop, say, y from 0 to image:getHeight()-1, you would be looping from 0 to 9 in steps of 4. That would loop over the numbers 0, 4 and 8. When y is 8, you would be trying to take a 4x4 quad at (8, 0), extending up to (11, 3). But the image is only 10 pixels high.

So, you need to loop up to the height of the image minus the height of the subimage. That guarantees that you will always have enough height for each quad in the last row. Similarly for columns.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Dividing spritesheets into quads

Post by raidho36 »

I'd suggest you use a dedicated software for this instead of managing all of that by hand.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 6 guests