Page 1 of 1

Rotation Matrix seems to squash quadrilateral

Posted: Sat Aug 29, 2020 2:28 am
by JohnpaulTH
Hello fellow coders.
I have been rotating shapes fine using `love.graphics.rotate()`
I have been working on a program to explore efficient and humane ways to develop areas of land. I know that this is not as exciting as the video games you guys whip up. In the process, I came up with an algorithm to select a line. it works fine until I rotate the shape using the rotate function. So I decided to rotate the actual points of the shape.
So I used a rotation matrix. The first rotation matrix I used is

Code: Select all

cos(θ)  -sin(θ)
sin(θ)  cos(θ)
However, I switched it to

Code: Select all

cos(θ)  sin(θ)
-sin(θ) cos(θ)
to accommodate the different coordinate system. Both matrixes exhibit the same squishing behavior.
I have attached a simple .love file with my simple matrix. I am using love 0.8.0 (Rubber Piggy) as that is the latest package from the official OpenBSD repositories. I am aware that a lot has changed since then, but the method is so basic, I expect the same results on your machine.

Thanks in advance for showing me what I am overlooking.
matrix.love
(458 Bytes) Downloaded 192 times

Re: Rotation Matrix seems to squash quadrilateral

Posted: Sat Aug 29, 2020 9:42 am
by ReFreezed
The error is in the loop in apply_matrix. The second line is using the result of the first line when you actually want the previous value.

Code: Select all

function apply_matrix(polygon, matrix)
	for i, pt in ipairs(polygon) do
		local a = pt[1] -- Store and use the previous values.
		local b = pt[2]
		pt[1] = a*matrix[1][1] + b*matrix[1][2]
		pt[2] = a*matrix[2][1] + b*matrix[2][2]
	end
end

[SOLVED] Rotation Matrix seems to squash quadrilateral

Posted: Sat Aug 29, 2020 4:17 pm
by JohnpaulTH
Thanks! that did the trick. I was thinking in the wrong programming language. I am still getting used to tables in Lua.
Your solution worked. Thanks!