Page 1 of 1

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

Posted: Wed Aug 14, 2019 12:20 am
by Rickton
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.

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

Posted: Wed Aug 14, 2019 6:55 am
by raidho36
Your problem is that you didn't set sprite center point properly, it's slightly off-center.

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

Posted: Wed Aug 14, 2019 9:43 am
by pgimeno
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)

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

Posted: Wed Aug 14, 2019 10:48 pm
by Rickton
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.