World and Object Rotations (Quaternions Are Killing Me)
I had been using quaternions in the following manner with success:
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...
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.
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.
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.
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.
Wow. Thanks for the reality check. That was all it was.
Possibly Related Threads...
Thread:  Author  Replies:  Views:  Last Post  
Quaternions in 2D  Frank C.  7  12,347 
Jun 28, 2012 04:21 PM Last Post: Charybdis 

Implementing a formula with quaternions  akb825  12  7,327 
Oct 16, 2006 09:36 AM Last Post: akb825 

Kinematics with Quaternions  akb825  3  5,136 
Sep 19, 2006 04:34 PM Last Post: akb825 

AxisAngle Rotations  DoG  2  2,865 
Apr 21, 2006 05:16 PM Last Post: DoG 

glMultMatrixf and rotations  jSTIN  7  8,000 
Nov 25, 2003 04:56 AM Last Post: DoG 