## Vector Rotation

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
IMP1
Prole
Posts: 39
Joined: Mon Oct 03, 2011 8:46 pm

### Vector Rotation

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.

Attachments
vectors.love

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

### Re: Vector Rotation

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

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

### Re: Vector Rotation

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



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

### Re: Vector Rotation

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

### Who is online

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