Jump to content
  • Advertisement
Sign in to follow this  
AmidaBucu

Quaternion camera with directx [SOLVED]

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

If you intended to correct an error in the post then please contact us.

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 this post


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

Share this post


Link to post
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 this post


Link to post
Share on other sites
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?

Share this post


Link to post
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 :(,
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]

Share this post


Link to post
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 this post


Link to post
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.
Thanks ahead again! :D
Anyway, I will try your code.

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!