World and Object Rotations (Quaternions Are Killing Me)

Member
Posts: 33
Joined: 2002.04
Post: #1
I had been using quaternions in the following manner with success:

Code:
...
GLfloat Matrix[16];

//create temp quaternions from euler angle deltas, these are reset every frame
qX.CreateQuaternion(rotX, 1, 0, 0);
qY.CreateQuaternion(rotY, 0, 1, 0);
qZ.CreateQuaternion(rotZ, 0, 0, 1);

baseQuaternion *= qX;
baseQuaternion *= qY;
baseQuaternion *= qZ;

//persistent quaternion
baseQuaternion.CreateRotationMatrix(Matrix);
glMultMatrixf(Matrix);

...


What I want to do now is a Y rotation in world coordinates instead of object coordinates.

My thought process was that by taking the current matrix and multiplying it by the world up vector I would get the correct axis in object coords. Like so...

Code:
Vector mv(0,1,0);

//multiply world up vector by current rotation matrix
mv.x = Matrix[0] * mv.x + Matrix[1] * mv.y + Matrix[2] * mv.z + Matrix[3];
mv.y = Matrix[4] * mv.x + Matrix[5] * mv.y + Matrix[6] * mv.z + Matrix[7];
mv.z = Matrix[8] * mv.x + Matrix[9] * mv.y + Matrix[10] * mv.z + Matrix[11];
            
mv.normalize();

//use vector as axis of rotation
qWorldY.CreateQuaternion(5,mv.x,mv.y,mv.z);

baseQuaternion *= qWorldY;

This looks like it's working for a moment, but then the object starts to roll around funky. Am I going about this the right way? Feel free to point out anything wrong with my examples.

N.
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2006.10
Post: #2
I don't know if this is your problem or not, but one thing to watch out for is that OpenGL does not index its matrix entries the way that you (or at least I) think it would. So if the Matrix[] that you're using to multiply the vector with is the same one that you passed to glMultMatrixf earlier, then you are actually passing in the transpose of your rotation matrix.
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2006.10
Post: #3
However, a more serious problem is that your matrix multiplication code is broken. You reassign mv.x and mv.y before they are used in later lines of code.
Quote this message in a reply
Member
Posts: 33
Joined: 2002.04
Post: #4
Wow. Thanks for the reality check. That was all it was.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Quaternions in 2D Frank C. 7 10,679 Jun 28, 2012 04:21 PM
Last Post: Charybdis
  Implementing a formula with quaternions akb825 12 6,354 Oct 16, 2006 09:36 AM
Last Post: akb825
  Kinematics with Quaternions akb825 3 4,666 Sep 19, 2006 04:34 PM
Last Post: akb825
  Axis-Angle Rotations DoG 2 2,600 Apr 21, 2006 05:16 PM
Last Post: DoG
  glMultMatrixf and rotations jSTIN 7 6,672 Nov 25, 2003 04:56 AM
Last Post: DoG