Quaternion Prob

Started by
8 comments, last by Muhammad Haggag 18 years, 5 months ago
Hi guys, was wondering if anyone could give me a quick explaination of what I'm doing wrong here... I have a object, in this case a space ship drawn on my screen. I have been doing reading and found Quaternions are what I need to make it rotate the way I want. Only problem is it seems to only rotate properly along the Z axis. When I do a roll no matter what position the nose faces it rolls properly... Although the yaw and pitch angle the ship along the screen X and Y and I can't figure out why.

//matWorld predefined else where
//objYr, objXr, objZr are used to keep the previous angles

D3DXQUATERNION quatWorld;
D3DXMATRIX matRot; 

D3DXQuaternionRotationYawPitchRoll(&quatWorld,(objYr+=y),(objXr+=x),(objZr+=z));
D3DXQuaternionNormalize(&quatWorld,&quatWorld);
D3DXMatrixRotationQuaternion(&matRot, &quatWorld);	
D3DXMatrixTranslation(&matWorld,objX,objY,objZ);

matWorld = matRot * matWorld;
[Edited by - Coder on November 5, 2005 10:08:31 PM]
Advertisement
Looks like the usual quaternion-related problem. Using a quaternion as an intermediate between Euler angles and a matrix (as you're doing) accomplishes nothing. You might as well just use the MatrixYawPitchRoll() function directly. I'm not sure what sort of motion you want, but if it's 6dof motion with local-axis rotations, you can do it just as easily with matrices as with quaternions. In either case, you need to maintain your quaternion or matrix from frame to frame, apply incremental rotations each frame about each local axis, and normalize/orthogonalize occasionally to prevent drift. There are a few more details, but that's the basic idea.
so am i closer to the target with something like this?
D3DXMATRIX matRot;D3DXMatrixIdentity(&matRot);	vRight = D3DXVECTOR3 (matRot (0,0), matRot (0,1), matRot (0,2)) ;vUp = D3DXVECTOR3 (matRot (1, 0), matRot (1, 1), matRot (1, 2)) ;vDir = D3DXVECTOR3 (matRot (2, 0), matRot (2, 1), matRot (2, 2)) ;D3DXMatrixRotationAxis(&matRot, &vRight, x);D3DXVec3TransformCoord(&vUp,&vUp, &matRot);D3DXVec3TransformCoord(&vDir,&vDir, &matRot);D3DXMatrixRotationAxis(&matRot, &vUp, y);D3DXVec3TransformCoord(&vRight,&vRight, &matRot);D3DXVec3TransformCoord(&vDir,&vDir, &matRot);	D3DXMatrixRotationAxis(&matRot, &vDir,z);D3DXVec3TransformCoord(&vRight,&vRight, &matRot);D3DXVec3TransformCoord(&vUp,&vUp, &matRot);matWorld *= matRot;


[Edited by - Coder on November 5, 2005 10:46:51 PM]
Try using this function

D3DXQuaternionRotationAxis(&quatRotation, &vRotationAxis, fAmt);

If you want to Yaw, Pitch and Roll the quaternion then you will need to pull that info our of the quaternion like this.
// Matrix to hold rotationD3DXMATRIX matRotation;// Create the rotation based on the quaternionD3DXMatrixRotationQuaternion(&matRotation, &m_quatOrientation); // Create the axis of rotationvRotationAxis.x = matRotation._21 + matRotation._41;vRotationAxis.y = matRotation._22 + matRotation._42;vRotationAxis.z = matRotation._23 + matRotation._43;


This is for Yaw. For Pitch and Roll use _1X and _3X from the matrix.

[Edited by - Coder on November 5, 2005 10:14:51 PM]
Quote:Original post by def
so am i closer to the target with something like this?

*...*;
Yes, I think so. Just remember that you'll need to orthonormalize your up, right, and direction vectors occasionally.
I still seem to be having a problem with this... The ship is just rotating uncontrollably... I think I'll have the same problem if I try modifying my code to use the quaternion instead... So I havent tried to use that sample code yet.

If I comment out the directions on their own each rotation works normal.... But put them together and its uncontrollable. Is it something with my matrix? Or are my vectors getting fried?



[Edited by - def on November 5, 2005 8:11:20 AM]
You might post your code in its current form (use [ source ] tags to preserve formatting). Make sure to include the part where you re-assemble the direction vectors into a matrix for submission to D3D (I assume that's how you're doing it). Also, I assume you're using radians as per DirectX convention...
look up a bit, i re-edited my previous post to show exactly how im' using it... if it's not enough I can post more.... Although now it doesnt rotate at all with that code. But if I comment out 2 of the 3 rotations which ever rotation works on the proper axis... So still back to square one. hmmm
You need to transform your quaternion to a matrix then combine that matrix with the axis that you want to rotate. Then you can rotate your quaternion based on that new vector. It's a very tricky process, here's how I transform my quaternions:

D3DXVECTOR3 pAxis = D3DXVECTOR3(0.0f, 1.0f, 0.0f); //Rotate along the Y axis(Yaw)D3DVECTOR NewAxis;D3DXMATRIX matRotation;D3DXMatrixRotationQuaternion(&matRotation, YOURQUATERNION);NewAxis.x = pAxis->x * matRotation._11 + pAxis->y * matRotation._21 + pAxis->z * matRotation._31 + matRotation._41; NewAxis.y = pAxis->x * matRotation._12 + pAxis->y * matRotation._22 + pAxis->z * matRotation._32 + matRotation._42;NewAxis.z = pAxis->x * matRotation._13 + pAxis->y * matRotation._23 + pAxis->z * matRotation._33 + matRotation._43;memcpy(pAxis, &vNewAxis, sizeof(vNewAxis)); // Copy axis.D3DXQUATERNION Rotation;D3DXQuaternionRotationAxis( &Rotation, &pAxis, DEGREESINRADIANS );YOURQUATERNION *= Rotation;D3DXQuaternionNormalize(&YOURQUATERNION, &YOURQUATERNION);//! Then you Create a Matrix with the conjugate of the quaternion and multiply the new matrix by the view matrix and you're done.


[Edited by - Coder on November 5, 2005 10:27:55 PM]
All source code in this thread was wrapped with source tags according to the rules found here.

This topic is closed to new replies.

Advertisement