1) If the identity quaternion is ( (0,0,0) 1 ) what does this mean? if it counts for a rotation around an axis wouldnt it be something like ( (0,1,0) 0) which to me means 0 rotation along the y axis?
2) How to construct the orient quaternion. my code uses matrices and the Look at function build the quaternion like so.
void _3DObject::SetLookTarget(glVector3& look, glVector3& up, float time) { look.Normalize(); glVector3 right = look.Cross(up); glMatrix4x4 mat = glMatrix4x4( //right.x, up.x, look.x, 0, //right.y, up.y, look.y, 0, //right.z, up.z, look.z, 0, right.x, right.y, right.z, 0, up.x, up.y, up.z, 0, look.x, look.y, look.z, 0, 0, 0, 0, 1); this->anim.orient = mat.GetQuat();}Get Quat -------- //Calculate the trace of the matrix T from the equation: float t = 1 + this->m[0] + this->m[5] + this->m[10]; // 2 2 2 //T = 4 - 4x - 4y - 4z // 2 2 2 // = 4( 1 -x - y - z ) // = 1 + mat[0] + mat[5] + mat[10] //If the trace of the matrix is greater than zero, then //perform an "instant" calculation. //Important note wrt. rouning errors: //Test if ( T > 0.00000001 ) to avoid large distortions! // S = sqrt(T) * 2; // X = ( mat[9] - mat[6] ) / S; // Y = ( mat[2] - mat[8] ) / S; // Z = ( mat[4] - mat[1] ) / S; // W = 0.25 * S; if (t > 0.00000001) { float s = sqrt(t) * 2; return glQuaternion( (this->m[9] - this->m[6]) / s, (this->m[2] - this->m[8]) / s, (this->m[4] - this->m[1]) / s, 0.25 * s); } //If the trace of the matrix is equal to zero then identify //which major diagonal element has the greatest value. //Depending on this, calculate the following: if ( (this->m[0] > this->m[5]) && (this->m[0] > this->m[10]) ) { // Column 0: float s = sqrt( 1.0 + this->m[0] - this->m[5] - this->m[10] ) * 2; return glQuaternion( 0.25 * s, (this->m[4] + this->m[1] ) / s, (this->m[2] + this->m[8] ) / s, (this->m[9] - this->m[6] ) / s); } else if ( this->m[5] > this->m[10] ) { // Column 1: float s = sqrt( 1.0 + this->m[5] - this->m[0] - this->m[10] ) * 2; return glQuaternion( (this->m[4] + this->m[1] ) / s, 0.25 * s, (this->m[9] + this->m[6] ) / s, (this->m[2] - this->m[8] ) / s); } else { // Column 2: float s = sqrt( 1.0 + this->m[10] - this->m[0] - this->m[5] ) * 2; return glQuaternion( (this->m[2] + this->m[8] ) / s, (this->m[9] + this->m[6] ) / s, 0.25 * s, (this->m[4] - this->m[1] ) / s); } //The quaternion is then defined as: // Q = | X Y Z W |
One of the above functions was grabbed from a previous gamedev post but the equation is not well explained.