# Six degrees of freedom help (Quaternions)

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

## Recommended Posts

Ok, like the topic says. I am looking for space type of movement for a mesh (6dof). I know quaternions is how to do this from all the research I've done. And speaking of all the research I've done I haven't been able to figure it out by literally 30hours of google. Which makes me feel absolutely dumb.
I haven't been able to find a good tutorial or anything really with actual code.
So I figured I'd post here and see what happens.
My attempts have failed but I will post what I got to see how close/far off I am
directx9 c++

[source]
D3DXMatrixRotationYawPitchRoll(&pitch, two.getAX(), two.getAY(), two.getAZ()); // Rotate based off ships rotation
D3DXVECTOR3 axis = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
D3DXVec3TransformCoord(&axis, &axis, &pitch);

yawChange = two.getAX()- oldYaw; // two.getAX() is the yaw angle of the ship.
pitchChange = two.getAY() - oldPitch;
rollChange = two.getAZ() - oldRoll;

oldYaw = two.getAX();
oldPitch = two.getAY();
oldRoll = two.getAZ();

D3DXQUATERNION rot;
D3DXQUATERNION rotChange = D3DXQUATERNION(yawChange, pitchChange, rollChange, 0.0f);
//D3DXQuaternionRotationAxis(&newRotation, &axis, two.getAX());
//D3DXQuaternionMultiply(&currentRot, &currentRot, &rotChange);
rot = rotChange * currentRot;
//D3DXQuaternionNormalize(&currentRot , &currentRot);
D3DXMATRIX rots;
D3DXMatrixRotationQuaternion(&rots, &rot);
//currentRot = newRotation;
[/source]

Right now I have 2 attempts kind of thrown together to make one giant mess of ugly. I know all the float values(yawChange oldYaw) just have to be the most absolutely wrong way to do it.
As of right now. either attempt just makes my ship not rotate at all. and when I uncomment the normalize my ship just automagically disappears. Which I can't figure out why.

"1. get your ship a nice little variable of quaternion type and name it something ugly like currentRotation.
2. get 3 variables like yawChange, pitchChange, rollChange. each frame get user input there
3. build quaternion rotationChange from these.
4. set currentRotation = rotationChange * currentRotation
5. crate matrix from currentRotation.
6. normalize currentRotation from time to time. "

alot of places seem to say something similar to this. But it makes no sense to me since you aren't rotating anything. Atleast it seems that way to me.

Any links or insight would be much appreciated (and the tutorial at code guru just makes no sense at all p.s)

You guys/gals are wonderful on this forum btw. Just thought I'd point that out. I have already gotten alot of help on here from some good people.

##### Share on other sites
Ok I seem to have made some progress here. Now I am just confused on one thing
my code

[source]
D3DXMATRIX angles;
D3DXMatrixRotationYawPitchRoll(&angles, 0.0f, 0.0f, 0.0f); // Rotate based off ships rotation

D3DXVECTOR3 yaw = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
D3DXVECTOR3 pitch = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
D3DXVECTOR3 roll = D3DXVECTOR3(0.0f, 0.0f, 1.0f);

D3DXVec3TransformCoord(&yaw, &yaw, &angles);
D3DXVec3TransformCoord(&pitch, &pitch, &angles);
D3DXVec3TransformCoord(&roll, &roll, &angles);

D3DXQUATERNION y;
D3DXQUATERNION p;
D3DXQUATERNION r;

D3DXQuaternionRotationAxis(&y, &yaw, yawChange);
D3DXQuaternionRotationAxis(&p, &pitch, pitchChange);
D3DXQuaternionRotationAxis(&r, &roll, rollChange);
currentRot = y*r*p;

D3DXQuaternionNormalize(&currentRot , &currentRot);
D3DXMATRIX rots;
D3DXMatrixRotationQuaternion(&rots, &currentRot);

g_pd3dDevice->SetTransform(D3DTS_WORLD, &(angles*rots*matTranslate));
[/source]

My problem is in the yawpitchroll matrix i'm not sure what the hell do put there. I know that will be the angle it all rotates around but how do I get that angle while also changing the angle of my ship? It seems to be when I do both at the same time they negate eachother and still rotate off of a 0.0f 0.0f 0.0f axis. Well lemm rephrase that. The yaw works but roll and pitch don't.
And yes I do know right now yawpitchroll is set to 0.0f 0.0f 0.0f right now. Because I'm not sure how to find the axis while changing axis if that makes any sense

##### Share on other sites
so you trying to use a quaternion for rotation, right ?
if you get that right can you explain it for other people( including myself ) who read / stumble appon this thread looking for help

##### Share on other sites
Hidden
Yeah I am =) and of course if once I figure it out I'll post back the solution =)

Yeah I am =) and of course if once I figure it out I'll post back the solution =)
I hoping also someone might give me some insight =)

##### Share on other sites
Solution! I figured it out about 2 weeks but I am just now posting the solution. It was actually really stupidly easy.

the getAX() getAY() and getAZ() are just my ships rotation angles. When I press left on the keyboard my X angle goes down. AX = angle X
and the variable turnSpeed which you see in the QuaternionRotationAxis can be anycnumber really. I could put 0.01 in there and it would still rotate. It is just a variable I have in my game which determines how fast I wanna rotate

[source]

yawChange = getAX()-oldYaw;
pitchChange = getAY()-oldPitch;
rollChange = getAZ()-oldRoll;

D3DXQUATERNION y;
D3DXQuaternionRotationAxis(&y, &D3DXVECTOR3(pitchChange, yawChange , rollChange), turnSpeed);
currentRot = y*currentRot;
D3DXQuaternionNormalize(&currentRot, &currentRot);
oldYaw = getAX();
oldPitch = getAY();
oldRoll = getAZ();
[/source]

I hope this is clear for anyone who is trying to figure this out as well and if not just Message me!

##### Share on other sites

Solution! I figured it out about 2 weeks but I am just now posting the solution. It was actually really stupidly easy.

the getAX() getAY() and getAZ() are just my ships rotation angles. When I press left on the keyboard my X angle goes down. AX = angle X
and the variable turnSpeed which you see in the QuaternionRotationAxis can be anycnumber really. I could put 0.01 in there and it would still rotate. It is just a variable I have in my game which determines how fast I wanna rotate

[source]

yawChange = getAX()-oldYaw;
pitchChange = getAY()-oldPitch;
rollChange = getAZ()-oldRoll;

D3DXQUATERNION y;
D3DXQuaternionRotationAxis(&y, &D3DXVECTOR3(pitchChange, yawChange , rollChange), turnSpeed);
currentRot = y*currentRot;
D3DXQuaternionNormalize(&currentRot, &currentRot);
oldYaw = getAX();
oldPitch = getAY();
oldRoll = getAZ();
[/source]

I hope this is clear for anyone who is trying to figure this out as well and if not just Message me!

that looks pretty self explanatory

now all i have to do is funush up this 2D stuff ive got and then i can test it out.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 23
• 37
• 16
• 75