love.graphics.push/pop to rotate around a pivot point
Posted: Thu Jun 09, 2016 5:05 am
Hey everyone! I'm not really understanding transformations in love2d, would love some help with this.
Here's my current code, which will rotate an image around a pivot point specified relative to the image center:
Amazingly, the code works, but the problem is that I do not understand what I'm doing here at all, which will be a problem when it comes to the future. And also it looks ugly. I'd like to explain what I'm trying to do:
The first love.graphics.translate sets the x and y coordinates to draw the piece.
The first love.graphics.rotation sets the rotation for the piece.
The second love.graphics.translate... moves the x and y coordinates by the pivot distance (?)
The second rotation resets the rotation (???)
then in the draw function, the x and y coordinates are the pivot_x and pivot_y values because (???????)
I hope it's clear what I want to do, but what is a better way of doing this?
Here's my current code, which will rotate an image around a pivot point specified relative to the image center:
Code: Select all
function Gem:draw(x, y, RGBTable, rotation, pivot_x, pivot_y)
love.graphics.push("all")
if RGBTable then love.graphics.setColor(RGBTable) end
love.graphics.translate(x or self.x, y or self.y)
if rotation then love.graphics.rotate(rotation) end
if pivot_x and pivot_y then love.graphics.translate(pivot_x, pivot_y) end
if rotation then love.graphics.rotate(-rotation) end
love.graphics.draw(
self.image,
self.quad,
pivot_x or 0,
pivot_y or 0,
rotation or 0,
1, -- scale x
1, -- scale y
self.width / 2, -- origin x
self.height / 2, -- origin y
0,
0
)
love.graphics.pop()
end
The first love.graphics.translate sets the x and y coordinates to draw the piece.
The first love.graphics.rotation sets the rotation for the piece.
The second love.graphics.translate... moves the x and y coordinates by the pivot distance (?)
The second rotation resets the rotation (???)
then in the draw function, the x and y coordinates are the pivot_x and pivot_y values because (???????)
I hope it's clear what I want to do, but what is a better way of doing this?