Problem with using a negative value for scale in love.graphics.draw() when scaling or rotating is used

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
Rickton
Party member
Posts: 125
Joined: Tue Mar 19, 2013 4:59 pm
Contact:

Problem with using a negative value for scale in love.graphics.draw() when scaling or rotating is used

Post by Rickton » Wed Aug 14, 2019 12:20 am

My game uses 32x32 sprites, drawn with an offset of 16 on both the X and Y axes. This is the code used to draw sprites:

Code: Select all

love.graphics.draw(image,x+16,y+16,entity.angle,(entity.faceLeft and -1 or 1)*scale,scale,16*scale,16*scale)
(assume image, x, y, and scale are passed in from elsewhere)

This works fine either when scale is 1, or scale isn't 1 for things that don't have angle set or have faceLeft set to true.
In those cases, the image is displaced:

Image

Image
The sprites are all being shifted to the right. The bloodstain sprites are randomly rotated, and seem to shift in whatever direction they've been rotated. (I don't have any non-rotated, non-faceleft sprites in this screenshot to compare, but they are being drawn in the correct place)

I'm assuming the answer is that I need to adjust the offset, but I'm not really sure what it would be changed to. Since a 16px offset works on a 32px sprite, it seems like a 16*scale offset should work with a 32*scale sprite.
Possession - Escape from the Nether Regions, my roguelike made in LÖVE for the 2013 7-Day Roguelike Challenge
And its sequel, simply called Possession , which is available on itch.io or Steam, and whose engine I've open-sourced!

User avatar
raidho36
Party member
Posts: 1857
Joined: Mon Jun 17, 2013 12:00 pm

Re: Problem with using a negative value for scale in love.graphics.draw() when scaling or rotating is used

Post by raidho36 » Wed Aug 14, 2019 6:55 am

Your problem is that you didn't set sprite center point properly, it's slightly off-center.

User avatar
pgimeno
Party member
Posts: 1767
Joined: Sun Oct 18, 2015 2:58 pm

Re: Problem with using a negative value for scale in love.graphics.draw() when scaling or rotating is used

Post by pgimeno » Wed Aug 14, 2019 9:43 am

I think the problem is that you're multiplying the offset by scale. Offset is in image coordinates, not in screen coordinates, and is therefore not affected by scale. If your images are 32x32, just use 16,16.

The draw coordinates, on the other hand, should probably be affected by scale.

The draw call would then be:

Code: Select all

love.graphics.draw(image,x+16*scale,y+16*scale,entity.angle,(entity.faceLeft and -1 or 1)*scale,scale,16,16)

Rickton
Party member
Posts: 125
Joined: Tue Mar 19, 2013 4:59 pm
Contact:

Re: Problem with using a negative value for scale in love.graphics.draw() when scaling or rotating is used

Post by Rickton » Wed Aug 14, 2019 10:48 pm

pgimeno wrote:
Wed Aug 14, 2019 9:43 am
I think the problem is that you're multiplying the offset by scale. Offset is in image coordinates, not in screen coordinates, and is therefore not affected by scale. If your images are 32x32, just use 16,16.

The draw coordinates, on the other hand, should probably be affected by scale.

The draw call would then be:

Code: Select all

love.graphics.draw(image,x+16*scale,y+16*scale,entity.angle,(entity.faceLeft and -1 or 1)*scale,scale,16,16)
That seems to be the problem exactly, thanks! It's funny, I tried adding the scaling to the x and y, and tried removing it from the offset, but not both at the same time.
Possession - Escape from the Nether Regions, my roguelike made in LÖVE for the 2013 7-Day Roguelike Challenge
And its sequel, simply called Possession , which is available on itch.io or Steam, and whose engine I've open-sourced!

Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests