Rotating a shape?

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
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Rotating a shape?

Post by hasen »

I can't seem to find exactly how to rotate a shape in Love2d. At first I wanted to add rotation to the object's movement in bump:

Code: Select all

  local future_l = self.l + self.vx * dt
  local future_t = self.t + self.vy * dt

  local next_l, next_t, cols, len = world:move(self, future_l, future_t, self.filter)
But I don't see that rotation is a part of the movement in bump? The shape is just a rectangle so in it's draw method it's like this but I can't see how I would make it rotate:

Code: Select all

function Object:draw()
  love.graphics.setColor(247,205,55)
  love.graphics.rectangle("fill", self.l, self.t, self.w, self.h)
end
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Rotating a shape?

Post by zorg »

rectangles are axis-aligned, as far as love.graphics.rectangle is concerned; you can only rotate those through the transform function love.graphics.rotate, which takes an angle in radians.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Re: Rotating a shape?

Post by hasen »

zorg wrote: Tue Feb 20, 2018 7:57 am rectangles are axis-aligned, as far as love.graphics.rectangle is concerned; you can only rotate those through the transform function love.graphics.rotate, which takes an angle in radians.
Ok I see. But love.graphics.rotate just takes one argument which is an angle, how would you tell it to rotate any particular rectangle? In the wiki it talks about rotating the screen so I assumed it was not for rotating individual shapes. Love.graphics.draw has an angle argument so rotating images is comparatively simple.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Rotating a shape?

Post by grump »

1. love.graphics.rotate
2. draw rectangle
3. love.graphics.origin to reset the transformation
4. draw other stuff
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Re: Rotating a shape?

Post by hasen »

grump wrote: Tue Feb 20, 2018 8:51 am 1. love.graphics.rotate
2. draw rectangle
3. love.graphics.origin to reset the transformation
4. draw other stuff
It says 'rotating a scene'. I doubt there would be so much code just to simply rotate a rectangle.
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Rotating a shape?

Post by zorg »

hasen wrote: Tue Feb 20, 2018 8:15 am
zorg wrote: Tue Feb 20, 2018 7:57 am rectangles are axis-aligned, as far as love.graphics.rectangle is concerned; you can only rotate those through the transform function love.graphics.rotate, which takes an angle in radians.
Ok I see. But love.graphics.rotate just takes one argument which is an angle, how would you tell it to rotate any particular rectangle? In the wiki it talks about rotating the screen so I assumed it was not for rotating individual shapes. Love.graphics.draw has an angle argument so rotating images is comparatively simple.
love.graphics.rotate rotates the coordinate system; if you translate that system to the centerpoint of any particular rectangle, rotate, then draw said rectangle, it will be drawn in the way you wanted it to be as; that said, after that's done, you need to reset the coordinate system.

In other words, you would either do what has been suggested above, or you could also wrap all that into a love.graphics.push and love.graphics.pop pair, since popping the rotation off the stack will also reset the coordinate system to the state before the first push.
hasen wrote: Tue Feb 20, 2018 8:57 am It says 'rotating a scene'. I doubt there would be so much code just to simply rotate a rectangle.
That said, if you'd use a mesh or just an image, you could use love.graphics.draw, which has its own transformation stack, hence you could just pass the rotation angle (and the center offsets) into that one function call. It's called OpenGL and it's called trade-offs (that the löve team decided when they implemented basic shape drawing as separate, simpler functions)
Last edited by zorg on Tue Feb 20, 2018 9:24 am, edited 1 time in total.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
PGUp
Party member
Posts: 105
Joined: Fri Apr 21, 2017 9:17 am

Re: Rotating a shape?

Post by PGUp »

Code: Select all

function love.draw()
love.graphics.translate(rectangleX, rectangleY)
love.graphics.rotate(math.rad(rectangleAngle))
love.graphics.rectangle("fill", -rectangleWidth()/2, -rectangleHeight()/2, rectangleWidth, rectangleHeight)
end
Add love.graphics.push() before translate and love.graphics.pop() after drawing rectangle if you are drawing multiple rectangles, works for all kinds of shape
Last edited by PGUp on Thu Feb 22, 2018 7:57 am, edited 1 time in total.
-
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Rotating a shape?

Post by grump »

hasen wrote: Tue Feb 20, 2018 8:57 am It says 'rotating a scene'. I doubt there would be so much code just to simply rotate a rectangle.
The transformation stack doesn't care if you rotate a whole scene or just a single rectangle. You set up a transformation for everything that you want transformed in a certain way. That's how these things work, love.graphics.draw just hides it from you. You'd probably be surprised what kind of math and programming is involved in order to "simply rotate a rectangle", if you programmed "bare" instead of using a framework of some sort.
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Re: Rotating a shape?

Post by hasen »

grump wrote: Tue Feb 20, 2018 10:28 am
hasen wrote: Tue Feb 20, 2018 8:57 am It says 'rotating a scene'. I doubt there would be so much code just to simply rotate a rectangle.
The transformation stack doesn't care if you rotate a whole scene or just a single rectangle. You set up a transformation for everything that you want transformed in a certain way. That's how these things work, love.graphics.draw just hides it from you. You'd probably be surprised what kind of math and programming is involved in order to "simply rotate a rectangle", if you programmed "bare" instead of using a framework of some sort.
All that code behind the scenes is built in and fully optimised though. I don't like long code anyway, the shorter I can make it look, the better. To that extent I prefer Jquery to Javascript and I like ternary operators. Glad that Lua can do that at least in some way.
PGUp wrote: Tue Feb 20, 2018 9:17 am

Code: Select all

function love.draw()
love.graphics.translate(rectangleX, rectangleY)
love.graphics.rotate(math.rad(rectangleAngle))
love.graphics.rectangle("fill", -rectangleWidth()/2, -rectangleHeight()/2, rectangleWidth, rectangleHeight)
end
Add love.graphics.push() before translate and love.graphics.pop() after translate if you are drawing multiple rectangles, works for all kinds of shape
Ok I see, thanks.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 52 guests