Sign in to follow this  

Despondent with quaternions :(

Recommended Posts

Ok, I have been going round in circles trying to gain control of my ingame objects.. I have been using vectors like a camera, to control movement, simple and works! I have moved into quaternions for the direction of the object, but its all up the wrong, and I am feeling lost! Here's a synopsis of my code, firstly to change direction:
void Cobject::pitch( float rads, float timeDelta ){

	rads *= timeDelta;

	//Pitch about right vector.
	D3DXMatrixRotationAxis(&R, &mRight, rads);
	D3DXVec3TransformCoord(&mLook, &mLook, &R);
	D3DXVec3TransformCoord(&mUp, &mUp, &R);

	//Set quaternion for direction change
	D3DXQuaternionRotationAxis( &qDelta, &mRight, rads );
	D3DXQuaternionMultiply( &qRotation, &qDelta, &qRotation );

and here's how I render the object:
void Cobject::render()

	D3DXMATRIX	matObjSpin;
	D3DXMATRIX	matObjTrans;

	D3DXMatrixRotationQuaternion( &matObjSpin, &qRotation );

	D3DXMatrixTranslation( &matObjTrans, mPos.x, mPos.y, mPos.z );
	matObj =  matObjTrans * matObjSpin;

	m_lpDevice->SetTransform( D3DTS_WORLD, &matObj );

The result is that the camera appears in be inside the object ( brief flashes of poly appear across the screen!). If I change D3DXMatrixRotationQuaternion( &matObjSpin, &qRotation ); to D3DXMatrixRotationQuaternion( &matObjSpin, &qDelta ); (where qDelta is the quaternion representing this frames' rotation), I can see the object trying to rotate, but reset each frame. Only when I try to multiply that with the qRotation varial do things go very weird!!!!! Any advice really gratefullt received!!! Thank you Simon

Share this post

Link to post
Share on other sites
I'm assuming the first part is your old matrix math and the second is the new quaternion math.

If so, you're creating the R matrix, so clearly, it's zero-ed out, but is qDelta zero-ed out anywhere? I don't see that in the code.

Alse, why not use the D3DXQuaternionRotationYawPitchRoll instead? Perhaps you should try it just to see if it changes your results.

Finally, I don't really see why you need quaternions at all, I mean, according to what I know, they are mostly useful for cameras since you can store a lookat point to rotate around, but I doubt you'd need that in your object rotation. You can easily use a matrix with the same functionality, unless theres something special you need.

Share this post

Link to post
Share on other sites
hi simon
As sirob mentioned, there's no point in using quaternions for objects, although I strongly beleive that quats shouldn't be used at all since the way they are represented in d3d is too weired up! anyway it's better to use the old and good tested-and-trusted yawpitchroll method, it should work, also try replacing your variables, it's very likely that depending on their declaration place, their inital value gets reset every frame.
have good times coding with D3D!

Share this post

Link to post
Share on other sites
I agree about not using quaternions for objects, but I started down this road after having little success with matrices.

After asking on this site, people suggested trying quats since they don't suffer from gimbal lock and are smoother!

Can anyone point me to some sample code that can rotate and object around its OWN axes? my attempts have only rotated about the XYZ axes, or else something very weird!



Share this post

Link to post
Share on other sites
I'll post a very small portion from my app (this uses matrices, not quats, but should be exactly the same).

bool cWorldPos::Rotate(float rx, float ry, float rz)
D3DXMATRIX matTmp, matTmp2;
D3DXMatrixRotationX(&matTmp, ry); // You can also use the YawPitchRoll function.
D3DXMatrixRotationY(&matTmp2, rx);
D3DXMatrixMultiply(&matTmp, &matTmp2, &matTmp);
D3DXMatrixRotationZ(&matTmp2, rz);
D3DXMatrixMultiply(&matTmp, &matTmp2, &matTmp);
D3DXMatrixMultiply(&m_MatRot, &matTmp, &m_MatRot); // m_MatRot is a member D3DXMATRIX, which
// only stores the rotation of the object.
return true;

Generally, the order of the last multiply affects which space's axis you're rotating around, world or object.

Also, AFAIK, marices do not suffer from gimbal lock. At least mine doesn't.

Hope this helps.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this