Public Group

# I'm sorry, but right now I hate quats

This topic is 5002 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Don't worry. With your help, I might like them later. I'll try to post some code that sorta explains what I'm trying to do, and then someone can elaborate. Im trying to upgrade my engine's rotations from matrices to quaternions. I'm using it to orient a ship. The code is as follows: // The ship is initialized with the following orientation. It initially // points in the positive z direction (left-handed, away from the camera) D3DXQUATERNION Orientation = {0,0,0,1}; // Construct some constants to make 5 degree quats float cos = cosf(5*D3DX_PI/180), sin = sinf(5*D3DX_PI/180); // Extract the ship's right, up, and forward vectors. I'm using forum // found knowledge that the columns of my quat matrix (a la DirectX) contain // my local x, y, and z axes D3DXVECTOR3 vF, vU, vR; D3DXMATRIX mat; D3DXRotationMatrixQuaternion(&mat, &Orientation); vR = D3DXVECTOR3(mat._11, mat._21, mat._31); vU = D3DXVECTOR3(mat._12, mat._22, mat._32); vF = D3DXVECTOR3(mat._13, mat._23, mat._33); // If pressing up arrow, build a quat to update the pitch and apply it // to our current orientation. Note pseudocode if (pressing up arrow) { D3DXVec3Normalize(&vR, &vR); D3DXQUATERNION qRot = D3DXQUATERNION(vR.x*sin, vR.y*sin, vR.z*sin, cos); Orientation = Rot*Orientation; } // If pressing right arrow, build a quat to update the yaw and apply it // to our current orientation. Note pseudocode if (pressing right arrow) { D3DXVec3Normalize(&vU, &vU); D3DXQUATERNION qRot = D3DXQUATERNION(vU.x*sin, vU.y*sin, vU.z*sin, cos); Orientation = Rot*Orientation; } // If pressing 'Z' key, build a quat to update the roll and apply it // to our current orientation. Note pseudocode if (pressing 'z' key) { D3DXVec3Normalize(&vF, &vF); D3DXQUATERNION qRot = D3DXQUATERNION(vF.x*sin, vF.y*sin, vF.z*sin, cos); Orientation = Rot*Orientation; } // Build a rotation matrix for the hardware D3DXRotationMatrixQuaternion(&mat, &Orientation); // Set as world matrix (pseudocode) D3Ddevice->SetTransform( D3DTS_WORLD, &mat); // Render Render(); I don't know why, but the above code makes my ship rotate fine all around each of the axes. But when I try to combine two or more rotations, the assigned buttons turn the ship in directions other than I intended them to. In other words, if I yaw the ship 45 degrees and then try to pitch it 90 degrees, the pitch button will pitch-roll-yaw. Please let me know if you can spot a flaw in my logic. Thank you. Ricardo

##### Share on other sites
It looks like you have the right idea. Where you wrote 'Rot' I assume you meant 'qRot'? Also, you probably already know that quaternions are formulated using the sin and cos of half the angle. However, this shouldn't be messing things up - it just means you're rotating by 10 degrees instead of 5.

I don't know how D3D matrices are arranged. Are the axes 11, 21, 31 as you've written? Or should they be 11, 12, 13? I don't know - it's just a place where things could potentially go wrong.

Also, I assume that D3D quats pre-multiply, so that qRot * Orientation is correct?

Also, are you setting up a camera matrix, or object matrix at the end? (Maybe that's why your axes are 11, 21, 31.)

I guess my main question is, when D3D creates a matrix from a quaternion, does it put the axes in the columns (11, 21, 31) or the rows (11, 12, 13)? If the answer is rows, that may be part of your problem.

Sorry I can't give you a more definite answer, but I'm just not familiar enough with the DirectX math library.

##### Share on other sites
Hey, thanks for the extra effort helping me.

You were right, I extracted my orient vectors wrong (transposed). Works like a charm now (finally!). And the best thing yet, no singularities, no gimbal lock. Thanks.

I developed an app to help visualize quaternions in my quat quest. If anybody needs it b/c quats are kicking their ass like they were to me, feel free to drop me a line and I'll send it your way.

Ricardo The Quat Master

1. 1
Rutin
47
2. 2
3. 3
4. 4
5. 5

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632996
• Total Posts
3009776
• ### Who's Online (See full list)

There are no registered users currently online

×