Page 1 of 1

Rotating a polygon

Posted: Tue Oct 16, 2012 12:06 pm
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?

Re: Rotating a polygon

Posted: Tue Oct 16, 2012 12:38 pm
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.

Re: Rotating a polygon

Posted: Tue Oct 16, 2012 1:35 pm
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.

Re: Rotating a polygon

Posted: Tue Oct 16, 2012 1:57 pm
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.

Re: Rotating a polygon

Posted: Tue Oct 16, 2012 2:35 pm
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?

Re: Rotating a polygon

Posted: Tue Oct 16, 2012 3:10 pm
by vrld
Counter question: Does it matter?
Because it sure is faster to write...