PDA

View Full Version : Matrix Rotation loses 1 axes of rotation



Benzino
02-13-2011, 06:46 PM
Hey all, I'm having an annoying problem and so I decided to register to see if anybody can help me :)

I'm new to openGL, so go easy on me :D

What I am doing is trying to rotate a rigid body, in this case a cube made of quads. I am using a rotation matrix to rotate the body.

Now if I use glMultMatrixd(), the cube rotates perfectly. But, if my understanding is correct, that simply rotates the world and not the cube, and so technically isn't correct. Never the less, I am working on collision detection and response, so I need to be able to access the rotated vertices of the rigid body, which glMultMatrixd() won't let me do.

Instead, I want to rotate each vertex of the cube by the rotation matrix. This works fine unless the cube is rotating about all three axis, in which case it will lose one axis of rotation.

Here is a video where you can see the problem. YouTube - Rotation Problem (http://www.youtube.com/watch?v=uz3uFmrHl6w)

The rotation matrix is the same when using both methods.

Does anybody know what might be causing this?

Here is the code, the matrix is a 3X3 matrix by the way.



for (int i = 0; i < 24; i++)
{
vertex = *vertices[i];

// Rotate the vertex
vertex = orientation * vertex;

// Apply the vertex
glVertex3f( vertex.x(), vertex.y(), vertex.z());

}

// Multiplying a matrix by a vector
inline Vector operator* (const matrixn3&amp; m, const Vector&amp; v)
{
Vector res;

res.x() = m.c(1,1) * v.x() + m.c(1,2) * v.y() + m.c(1,3) * v.z();
res.y() = m.c(2,1) * v.x() + m.c(2,2) * v.y() + m.c(2,3) * v.z();
res.z() = m.c(3,1) * v.x() + m.c(3,2) * v.y() + m.c(3,3) * v.z();

return res;
}

burito
02-15-2011, 05:53 AM
You're not showing the code that matters, how are you building the rotation matrix?

At a guess it looks like you're applying the rotations in a different order. The first video looks like it's rotated 45 degrees around Z and X and then are applying a time based rotation around Y. In the second video I'm seeing a time based rotation around Y and *then* rotation of 45 degrees around Z and X.

Matrix multiplication is not commutative, A * B != B * A

I'm not seeing any loss of rotation axis in that video, although if you are writing your own physics code you will encounter this shortly, it's a phenomenon known as Gimbal Lock (http://en.wikipedia.org/wiki/Gimbal_lock), for which there are all manner of fun solutions. I've read that it's possible to avoid it in matrices with 12 rotations (by rotating your rotations, for each axis, before applying them to your matrix, this hurts a lot more than it sounds). My research tells me the canonical method of avoiding gimbal lock is with quaternion based rotation.