Public Group

Quaternion camera with directx [SOLVED]

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

Recommended Posts

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]

Share on other sites
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.

Share on other sites
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);}

Share on other sites
Quote:
 Original post by AmidaBucuHi! 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?

Share on other sites
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 :(,

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]

Share on other sites
You only need 1 quat for that, not 3.

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

'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(
&or,
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,&or,&ps);

qR is a D3DXQUATERNION
ori.Orientation is the final matrix

Share on other sites
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.
Anyway, I will try your code.

[Edited by - AmidaBucu on August 27, 2010 6:55:30 PM]

Share on other sites
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).

Share on other sites
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.

Share on other sites
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.)

1. 1
2. 2
3. 3
Rutin
14
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• Forum Statistics

• Total Topics
633674
• Total Posts
3013277
×