Page 1 of 1

Rotating rectangles

Posted: Wed Mar 30, 2016 4:01 pm
by Vimm
I need the rectangles that spawn in my game to rotate as they move, so I made an angle variable and put

Code: Select all

love.graphics.rotate(angle)
in my draw function just above the rectangle spawning for loop, then i put

Code: Select all

angle = angle + dt * math.pi/2
angle = angle % (2*math.pi)
in my update function, like the love wiki page on graphics.rotate says to do, and I got a weird result, the rectangles are now spinning as if they're orbiting something of screen, and really fast. Which isn't what I wanted at all.

How do I make it so the rectangles rotate on a center axis, and how do I make them rotate a bit slower.

I've included the .love, even though its just a main.lua right now

Re: Rotating rectangles

Posted: Wed Mar 30, 2016 4:53 pm
by s-ol
You need to learn how transforms work. rotate rotates around the point (0, 0), if you want to rotate a rectangle art (x,y) around its center you need to do:

Code: Select all

love.graphics.translate(x, y) -- move relative (0,0) to (x,y)
love.graphics.rotate(angle) -- rotate coordinate system around relative (0,0) (absolute (x,y))
love.graphics.rectangle("fill", -w/2, -h/2, w, h) -- draw rectangle centered around relative (0,0)
Remember that order matters und you need to use push and pop for multiple rectangles.

Re: Rotating rectangles

Posted: Wed Mar 30, 2016 5:55 pm
by Vimm
s-ol wrote:You need to learn how transforms work. rotate rotates around the point (0, 0), if you want to rotate a rectangle art (x,y) around its center you need to do:

Code: Select all

love.graphics.translate(x, y) -- move relative (0,0) to (x,y)
love.graphics.rotate(angle) -- rotate coordinate system around relative (0,0) (absolute (x,y))
love.graphics.rectangle("fill", -w/2, -h/2, w, h) -- draw rectangle centered around relative (0,0)
Remember that order matters und you need to use push and pop for multiple rectangles.
Oh ok, I read on some forums not to use translate for some reason.

So I did that and it's closer to what I need, but still not quite right, I assume it's because im not "pushing and popping" (lol), since I don't know what that means xD

(edited love file included)

Re: Rotating rectangles

Posted: Wed Mar 30, 2016 6:04 pm
by Sulunia
Push means the following rotating, transforming and drawing functions will happen on a separate layer until you pop it.
So if you push, translate and rotate for a given rectangle, draw it and then pop, anything you draw after the pop wont rotate.

Did that make any sense? Perhaps someone can explain this a bit better

Re: Rotating rectangles

Posted: Wed Mar 30, 2016 6:22 pm
by davisdude
If you want to rotate the rectangle about its center do something like this:

Code: Select all

function rotatedRectangle( mode, x, y, w, h, rx, ry, segments, r, ox, oy )
	-- Check to see if you want the rectangle to be rounded or not:
	if not oy and rx then r, ox, oy = rx, ry, segments end
	-- Set defaults for rotation, offset x and y
	r = r or 0
	ox = ox or w / 2
	oy = oy or h / 2
	-- You don't need to indent these; I do for clarity
	love.graphics.push()
		love.graphics.translate( x + ox, y + oy )
		love.graphics.push()
			love.graphics.rotate( -r )
			love.graphics.rectangle( mode, -ox, -oy, w, h, rx, ry, segments )
		love.graphics.pop()
	love.graphics.pop()
end

-- later
rotatedRectangle( 'line', 100, 100, 50, 50, math.pi / 4 )
This could also be achieved using canvases.

Re: Rotating rectangles

Posted: Wed Mar 30, 2016 6:49 pm
by s-ol
@OP: I can't think of a reason not to use transform. Learn how to use the matrix transforms, it's important graphics stuff.
davisdude wrote: This could also be achieved using canvases.
You will have to apply the exact sake transformations and waste precious vram on a single-color rectangle, I dont see the point.

Re: Rotating rectangles

Posted: Wed Mar 30, 2016 7:06 pm
by davisdude
I worded that poorly. I meant to say that you can use this same principle to rotate an entire image with a canvas.

Re: Rotating rectangles

Posted: Wed Mar 30, 2016 9:55 pm
by Ref
Hay, davidude!
Think you got carried away with the pushing and popping.
Only need one push and one pop.
Best

Re: Rotating rectangles

Posted: Mon Apr 10, 2023 6:21 pm
by darkfrei
As easy as possible (rotated about the middle):

Code: Select all

function drawRotatedRectangle(mode, x, y, w, h, angle)
	love.graphics.push()
		love.graphics.translate(x, y)
		love.graphics.rotate(angle)
		love.graphics.rectangle(mode, -w/2, -h/2, w, h)
	love.graphics.pop()
end