Rotating rectangles

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
User avatar
Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

Rotating rectangles

Post 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
Attachments
rotate.love
(550 Bytes) Downloaded 205 times
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Rotating rectangles

Post 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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

Re: Rotating rectangles

Post 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)
Attachments
rotate.love
(564 Bytes) Downloaded 233 times
User avatar
Sulunia
Party member
Posts: 203
Joined: Tue Mar 22, 2016 1:10 pm
Location: SRS, Brazil

Re: Rotating rectangles

Post 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
Don't check my github! It contains thousands of lines of spaghetti code in many different languages cool software! :neko:
https://github.com/Sulunia
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Rotating rectangles

Post 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.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Rotating rectangles

Post 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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Rotating rectangles

Post 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.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Rotating rectangles

Post by Ref »

Hay, davidude!
Think you got carried away with the pushing and popping.
Only need one push and one pop.
Best
User avatar
darkfrei
Party member
Posts: 1196
Joined: Sat Feb 08, 2020 11:09 pm

Re: Rotating rectangles

Post 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
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests