Vector Rotation

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
IMP1
Prole
Posts: 39
Joined: Mon Oct 03, 2011 8:46 pm

Vector Rotation

Post by IMP1 » Sat Oct 12, 2013 5:14 pm

I'm trying to make my own vector module, and for the most part it works (as far as I know), but in trying to add rotation I'm getting unexpected results.
I'm not 100% sure whether the results are right, and my expectations are wrong, or vice versa.

I'm rotating a 3D vector by an angle 'a', and then rotating it by '-a'. But it doesn't end up the same as it started.

The vector rotation code is on lines 166 through to 181.
I know and don't mind that it assumes the vector is 3D.

Can anyone please help?
Attachments
vectors.love
(1.79 KiB) Downloaded 65 times

User avatar
Xgoff
Party member
Posts: 211
Joined: Fri Nov 19, 2010 4:20 am

Re: Vector Rotation

Post by Xgoff » Sat Oct 12, 2013 7:24 pm

precision issues? (seems kind of excessive though...)

User avatar
chezrom
Citizen
Posts: 59
Joined: Tue May 28, 2013 11:03 pm
Location: France

Re: Vector Rotation

Post by chezrom » Sat Oct 12, 2013 7:31 pm

The problem is due to modification of origin vector during computation (origin vector = result vector).

For the rotation around x-axis, the formula must be :
x' = x
y' = cos * y + sin *z
z' = cos * z - sin * y

Or in fact you do y = ...., so you change y value for the z' computation and the the z result is false.

You can rewrite you code as this :

Code: Select all

-- MATRIX ROTATION SHORTCUT
---------------------------
function Vector:rotate(a, b, c)
    local outcome = self:copy()
	local x,y,z

    -- x-axis
	y,z = outcome.y, outcome.z
    outcome.y = y * math.cos(a) + z * math.sin(a)
    outcome.z = z * math.cos(a) - y * math.sin(a)

    -- y-axis
	x,z = outcome.x, outcome.z
    outcome.x = x * math.cos(b) - z * math.sin(b)
    outcome.z = z * math.cos(b) + x * math.sin(b)

    -- z-axis
	x,y = outcome.x,outcome.y
    outcome.x = x * math.cos(c) + y * math.sin(c)
    outcome.y = y * math.cos(c) - x * math.sin(c)
    
    return outcome
end


User avatar
IMP1
Prole
Posts: 39
Joined: Mon Oct 03, 2011 8:46 pm

Re: Vector Rotation

Post by IMP1 » Sun Oct 13, 2013 12:43 pm

Ah, of course, that makes perfect sense.
Thank you very much for pointing out my mistake.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 20 guests