Quaternion camera with directx [SOLVED]

Started by
11 comments, last by AmidaBucu 13 years, 7 months ago
Hi! I want to write quaternion based camera class.
But I was wrong somewhere, but I don't know where.
Here is the wrong code:
void _camera::rotateX(float angle){	thirdA.w = angle;		D3DXQuaternionNormalize(&thirdA, &thirdA);	D3DXQUATERNION tmp;	D3DXQUATERNION tmp2;	D3DXQuaternionInverse(&tmp, &thirdA);	up.w = 0;	D3DXQuaternionMultiply(&tmp2, &thirdA, &up);	D3DXQuaternionMultiply(&up, &tmp2, &tmp);		look.w = 0;	D3DXQuaternionMultiply(&tmp2, &thirdA, &look);	D3DXQuaternionMultiply(&look, &tmp2, &tmp);}


I have 3 quaternions I use to represent the directions of side-to-side and forward movement, or the axis of the rotation. There are look, up and thirdA.

But the code doesn't want to run as way as I want :S
Where was I wrong?

[Edited by - AmidaBucu on August 29, 2010 3:54:33 PM]
Advertisement
you can't just assign the angle to the w component of a quaternion. You need to understand how quaternion works first, by looking at how one is constructed mathematically and what it means. With that said, the easiest way out is to use the D3DX utility functions to create the quaternion for you based on an axis and an angle or from Euler angle.
Okay, now I created the quaternion with a DX function.
Now it's better, but only by a bit :S
Something is still wrong there.

void _camera::rotateX(float angle){	D3DXVECTOR3 tmpv(thirdA.x,thirdA.y,thirdA.z);	D3DXQuaternionRotationAxis(&thirdA, &tmpv, angle);			D3DXQuaternionNormalize(&thirdA, &thirdA);	D3DXQUATERNION tmp;	D3DXQUATERNION tmp2;	D3DXQuaternionInverse(&tmp, &thirdA);	up.w = 0;	D3DXQuaternionMultiply(&tmp2, &thirdA, &up);	D3DXQuaternionMultiply(&up, &tmp2, &tmp);		look.w = 0;	D3DXQuaternionMultiply(&tmp2, &thirdA, &look);	D3DXQuaternionMultiply(&look, &tmp2, &tmp);}
Quote:Original post by AmidaBucu
Hi! I want to write quaternion based camera class.
Don't.

I'll offer my usual advice here. Unless you know *why* you're writing a quaternion-based camera class and can articulate clearly what advantages using quaternions will offer you, then forget about quaternions and just use matrices.

So with that in mind, what do you hope to gain by using quaternions here?
I want to implement camera class only once, so I use quaternions.
In that way I can avoid gimbal lock too.
And I want to rotate around three axis easily.

[It was in the school, but it was not teached very well :(,
so I had to follow that wikipedia says]

I use that from wikipedia, but I am still wrong womewhere.
"http://upload.wikimedia.org/math/1/b/a/1ba16966385e8a79ad10a2934baa013f.png"


So Where was I wrong?

[Edited by - AmidaBucu on August 27, 2010 5:03:58 PM]
You only need 1 quat for that, not 3.

Velocity calculation
D3DXVec3TransformNormal(&vT,&vT,∨);

'vT' is the velocity, 'or' is your orientation matrix

To keep it smooth, you will need to multiply it by the elapsed frame count

pos=vT*ElpFrame;

To do the quat calculations, you will need to do this

blk.x=blk.y=blk.z=0.0f; blk is D3DXVECTOR3


D3DXMatrixAffineTransformation(
∨,
DBWndV.X[DBGM.WndNum].AspRatio,
&blk,
&qR,
&blk//&ori.Pos
);
ori.QRot=qR;
D3DXMatrixTranslation(
&ps,
ori.Pos.x,
ori.Pos.y,
ori.Pos.z
);
D3DXMatrixMultiply(&ori.Orientation,∨,&ps);

qR is a D3DXQUATERNION
ori.Orientation is the final matrix

******************************************************************************************
Youtube Channel

I wanted to 3 quaternions to represent direction of movements and rotation axis.
[So when i move the camera sideto side or forward/backward, then I add a quaternion (as a vector) to the position]
Thanks for the help anyway, but I would like to know where is my code wrong.
Thanks ahead again! :D
Anyway, I will try your code.

[Edited by - AmidaBucu on August 27, 2010 6:55:30 PM]
Quote:I want to implement camera class only once, so I use quaternions.
There's no direct correlation between using quaternions and implementing a camera 'only once'.
Quote:In that way I can avoid gimbal lock too.
Wrong; there is absolutely no correlation between using quaternions and avoiding gimbal lock.
Quote:And I want to rotate around three axis easily.
Whether you can 'rotate around three axes easily' has absolutely nothing to do with whether you use quaternions or not.
Quote:I wanted to 3 quaternions to represent direction of movements and rotation axis.
You don't (typically) use quaternions to represent linear motion, and you don't need three of them.

Don't worry, you're not the only one to have been confused by quaternions. But, if I may be direct, all of the statements you've made regarding quaternions are either confused or outright wrong, so I'll repeat my earlier advice: forget about quaternions and just use matrices for now. Trying to use quaternions without understanding them or what advantages they can offer isn't going to help you at all (IMO).
Send me a pm and I will provide you a download location for the camera code. It does exactly what you are asking for and it uses a single quat and velocity vectors. I have found another using Google by searching for 'camera.cpp', there is a pd source code file called by that name. It will provide you with a basic quat camera class you can drop into your code.

Good luck! Quats were hard to figure out but they are actually easier to use than Euler in my opinion.

******************************************************************************************
Youtube Channel

If there are advantages of using quaternions, then could You give me a link for an explanation abut quaternions? (the math, how is it works, etc.)

This topic is closed to new replies.

Advertisement