Page 1 of 1

love.graphics.push/pop to rotate around a pivot point

Posted: Thu Jun 09, 2016 5:05 am
by garcia1000
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:

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
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?

Re: love.graphics.push/pop to rotate around a pivot point

Posted: Thu Jun 09, 2016 9:00 am
by garcia1000
Hello. That's stupid code. I'm stupid. Sorry. Here's my better code:

Code: Select all

function Gem:draw(x, y, RGBTable, rotation, displace_x, displace_y)
  ID.draw = ID.draw + 1
  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
    love.graphics.translate(-self.width / 2, -self.height / 2)
    
    love.graphics.draw(self.image, self.quad, displace_x or 0, displace_y or 0)
  love.graphics.pop()
end
Sorry, I'm embarrassed at the first post. What was I doing.

Re: love.graphics.push/pop to rotate around a pivot point

Posted: Thu Jun 09, 2016 3:47 pm
by pgimeno
I think we're now confused about whether you still need help or not, and what with :)

Edit: If the question is whether there is a simpler way to do it, then yes, take a detailed look at the parameters of [wiki]love.graphics.draw[/wiki]. Especially r, ox, and oy.

Re: love.graphics.push/pop to rotate around a pivot point

Posted: Fri Jun 10, 2016 2:20 am
by garcia1000
I solved it myself but I wanted to leave it up in case other people have similar problem :crazy:

I tried using offsets in love.graphics.draw. The problem I found was that offsets not only specify the point for the transformations, but ALSO where to draw the image on the screen.