Rotating a polygon

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
Jack5500
Party member
Posts: 149
Joined: Wed Dec 07, 2011 8:38 pm
Location: Hamburg, Germany

Rotating a polygon

Post by Jack5500 »

Hey,

I've been trying to rotate a polygon with 3 Points, which is actually a triangle, but I guess it doen't make a difference, since they both don't have a parameter for an angle. So is there a good way, that's easy to use? I've seen one with a canvas, but that seemed rather too complicated for such a simple cause. or would you recommend an image?
User avatar
timmeh42
Citizen
Posts: 90
Joined: Wed Mar 07, 2012 7:32 pm
Location: Cape Town, South Africa

Re: Rotating a polygon

Post by timmeh42 »

As you said, you can draw the polygon to a canvas and draw that canvas rotated by a certain angle. This is a simple way, but probably not too fast.
A better way would be to do some rotation maths on every point - the relevant equations under the heading "Matrix Algebra" should work, but they will rotate about the origin (0;0). To get them to rotate around a different point, you will first have to translate the polygon to more-or-less above the origin before doing the rotation, then translate it back.

On the other hand using an image would be much easier, and allow for textures or coloured polygons to be used, but may suffer a slight loss of quality and speed.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Rotating a polygon

Post by T-Bone »

Is there a good matrix library that can be used with LÖVE? Because that would've been really helpful in this situation.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Rotating a polygon

Post by vrld »

canvas
Wat?

Code: Select all

local t = 0
function love.update(dt)
	t = t + dt
end

function love.draw()
	love.graphics.push()
	love.graphics.translate(150,150)   -- rotation center
	love.graphics.rotate(-t/2)         -- rotate
	love.graphics.translate(-150,-150) -- move back
	love.graphics.polygon('fill', 100, 100, 200, 100, 150, 200)
	love.graphics.pop()

	love.graphics.polygon('fill', 200, 200, 300, 200, 250, 300)
end
T-Bone wrote:Is there a good matrix library that can be used with LÖVE? Because that would've been really helpful in this situation.
hump.vector might be what you're looking for. But if you just want to draw the polygon, you don't need it. At all.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
timmeh42
Citizen
Posts: 90
Joined: Wed Mar 07, 2012 7:32 pm
Location: Cape Town, South Africa

Re: Rotating a polygon

Post by timmeh42 »

Oh dang, I forgot about love's transformation functions. I guess that's the simple way of doing it.
Question though: is it faster to use built-in transformation functions than the pure maths for such things?
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Rotating a polygon

Post by vrld »

Counter question: Does it matter?
Because it sure is faster to write...
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
Post Reply

Who is online

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