# Invert One Axis in Rotation Matrix

## Recommended Posts

Hey. I've searched around and asked some friends, but so far nobody knows the answer... I'm using a 3D gyroscope as input to game, and I want to simply rotate the scene identical to how a user rotates the gyroscope. This seemed easy, especially because one of the gyroscope's outputs is a rotation matrix (seemingly for use with OpenGL, which I'm using). However the issue has always been that the coordinate systems don't line up. I've messed with it enough and finally gotten it down to two correct axes--y and z. (As in, you tilt up, the object tilts up, you roll clockwise, the object rolls clockwise, et cetera.) The final frontier is the x axis, which is inverted. So you pan up, the object pans down. How can I invert/multiply by -1 that axis in the rotation matrix? OpenGL is column-major and the device is seemingly row-major, although I don't think it actually says, so I've already fixed that problem. I'm still not that good with rotation matrices, so getting two axes working was honestly just swapping values until I got it to work. But is there an easy way to fix that one axis? Or perhaps change the scene instead so it looks to be working? I've rotated the camera in certain ways so a different axis is messed up, but I've never had all three correct. Thanks.

##### Share on other sites
So, you want to multiply a row of this matrix by -1? Can you explain, please, why you can't just multiply the values in the row by -1? It seems like maybe there's something I'm not getting about this problem... it seems like a very simple case of "just put a minus sign in"...

##### Share on other sites
Well, I though it was that simple, but it didn't end up seeming so.

According to this website:
http://pages.cs.wisc.edu/~psilord/docs/local_axis.html

A rotation around the X is represented by five different values in a transformation matrix. These five values aren't unique to the X rotation, it seems, so I can't modify them without impacting the other axes.

It's highly possible that I'm not quite understanding this right though.

EDIT: And just to clarify, I have one matrix that has all three rotations inside it.

##### Share on other sites
What Aken is trying to say may not make sense to you if you don't understand one common way to interpret the values in your matrix. In this example, I'm going to be row-major, you may have to transpose for your application.

You can consider the first three rows of your matrix to be the relative X, Y, and Z axes of an object transformed by your matrix. The fourth row is the position of the center of that object. (In all cases, the fourth column is probably [0,0,0,1]).

In other words, to reverse your object's X-axis, all you would need to do is multiply the first row of your matrix by -1. Or if you wanted to move your object 1 unit along its own Z-axis (one definition of "move forward"), you could just increment the fourth row of your matrix by the values in the third row of your matrix. Or if you wanted to rotate the object around its own Y-axis (one definition of "Turn left or right"), you would just rotate around the axis defined by the point [fourth row] and the direction [second row].

Does that help?

##### Share on other sites
Oh, wow, OK. I didn't know you could think about it (and use it) like that.

Thanks a lot. I haven't tried it yet, but I'll try that out tomorrow morning and see what happens.

##### Share on other sites
Quote:
 In this example, I'm going to be row-major, you may have to transpose for your application.You can consider the first three rows of your matrix to be the relative X, Y, and Z axes of an object transformed by your matrix. The fourth row is the position of the center of that object. (In all cases, the fourth column is probably [0,0,0,1]).
Just to try and prevent any confusion on the OP's part, I'll go ahead and point out that this isn't actually what the term 'row major' means. 'Row major' refers to how the matrix is stored in memory; whether the basis vectors of the transform are stored in the rows or columns of the matrix is a separate issue.

##### Share on other sites
Interesting. I'm using OpenGL, so just to see if I have this correct...

According to:
http://www.gamedev.net/community/forums/topic.asp?topic_id=533653&whichpage=1#3449786

He says that "Base vectors in OpenGL are column", and so he stores the rows of the transformation matrix in column-major format. So does that mean that a "column base vector" represented in a column-major memory layout would just be:

X: m[0], m[1], m[2]
Y: m[3], m[4], m[5]
Z: m[6], m[7], m[8]

?

Or is it backwards? (m[0], m[3], m[6], ...)

Thanks.

##### Share on other sites
Quote:
 X: m[0], m[1], m[2]Y: m[3], m[4], m[5]Z: m[6], m[7], m[8]
For both a row-major 3x3 matrix with row vectors or a column-major 3x3 matrix with column vectors, the elements of the basis vectors would map to the elements of a 1-d array just as you've shown above.

For a column-major matrix with row vectors or a row-major matrix with column vectors, it would be the other way around (i.e. m[0], m[3], m[6], etc.).

##### Share on other sites
I've confirmed that it's column-major with column vectors, however for some reason I can't get it to work.

Let's say I want to flip the rotation about the Z axis. So I multiply indices 8, 9, and 10 (4x4 matrix) by -1. However, instead of the rotation being flipped (so moving the gyro to the right makes it go to the right), the geometry is flipped. So now a line drawn from 0 to 10 in the Z now looks like it is going from 0 to -10?

I'll try to keep this in the math/physics category, but perhaps I have a bigger OpenGL problem in addition to the matrix stuff...

Thanks.

##### Share on other sites
[quote name='inanimate' timestamp='1249225304' post='4502385']
I've confirmed that it's column-major with column vectors, however for some reason I can't get it to work.

Let's say I want to flip the rotation about the Z axis. So I multiply indices 8, 9, and 10 (4x4 matrix) by -1. However, instead of the rotation being flipped (so moving the gyro to the right makes it go to the right), the geometry is flipped. So now a line drawn from 0 to 10 in the Z now looks like it is going from 0 to -10?

I'll try to keep this in the math/physics category, but perhaps I have a bigger OpenGL problem in addition to the matrix stuff...

Thanks.
[/quote]
I have the same problem. Is there any way to invert one axis directly from the rotation matrix? I can't find anything that can help me. Any other method to do it?
Thanks!

##### Share on other sites
I'm not sure but I have the impression that another conceptual misunderstanding is underway here. It seems me that space mirroring and rotation inversion are confused.

Multiplying a column / row of the basis matrix means a mirroring in 1 dimension of the space. It is the same as applying a scale matrix where one of the factors is -1.

When looking at a rotation matrix, you'll notice that there are cosine and sine terms inside. E.g.

[code]
[ c s 0 ]
[ -s c 0 ]
[ 0 0 1 ]
[/code]
(where c means cos(angle) and s means sin(angle)) is a simple, z-axis rotation matrix.

Now, inverting the rotation can be achieved by negating the angle. But look at the terms then, considering that
cos(angle) == cos(-angle)
sin(angle) == -sin(-angle)
Hence you cannot yield in an inverse rotation by negating one of the columns / rows!

Is this the problem you've encountered?

BTW: The term "axis" may mean a column / row of the basis (orientation matrix) and hence a dimension in a space, or else a pivot for a rotation. Both may be coincident (i.e. in Euler rotations) but are not the same.

##### Share on other sites
Ok, I guess I can't do directly from the matrix ... I will try to get euler angles and create a new matrix ...

##### Share on other sites
It appears the gyroscope is providing you with a left-hand system and you're using OGL as a right-hand system.

Try doing the rotation in the gyroscope's system, rather than OGL. As mentioned above, you just scale the x-axis vector. However, you have to apply that scaling both [i]before[/i] and [i]after[/i] you do the rotation.

I.e., create a scale matrix for scale factors ( -1, 1, 1 ); scale the x-axis by -1.

Your_matrix = scaleMatrix * gyroMatrix * scaleMatrix

Note: this is for a rotation matrix only, before your multiply by the OGL translation.

Application of the first scaleMatrix moves into gyro space. The gyroMatrix applies the rotation. The second scaleMatrix moves back into OGL space. I believe the second scaling matrix is what you're missing.

EDIT: Good heavens! Just noticed the OP posting date. Guess this is a bit late. Edited by Buckeye

##### Share on other sites
[quote name='Pertur' timestamp='1301480218' post='4792099']
Ok, I guess I can't do directly from the matrix ... I will try to get euler angles and create a new matrix ...
[/quote]

^^ What buckeye said ^^ ..... for that is the correct way

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628333
• Total Posts
2982139

• 9
• 24
• 9
• 9
• 13