Rotation Matrix seems to squash quadrilateral

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
JohnpaulTH
Prole
Posts: 3
Joined: Tue Aug 25, 2020 10:47 pm

Rotation Matrix seems to squash quadrilateral

Post 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 187 times
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Rotation Matrix seems to squash quadrilateral

Post 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
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
JohnpaulTH
Prole
Posts: 3
Joined: Tue Aug 25, 2020 10:47 pm

[SOLVED] Rotation Matrix seems to squash quadrilateral

Post 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!
Post Reply

Who is online

Users browsing this forum: No registered users and 255 guests