Jump to content

View more

Image of the Day

WIP title screen for #DeathOfAPartisan #screenshotsaturday #gamedev https://t.co/qJNhfZCvd4
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

Quaternion x,y,z rotation

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 benp444   Members   


Posted 05 September 2012 - 05:11 AM

Firstly sorry for yet another newby post regarding quaternions but I have scoured the forums for hours without any luck. Secondly I have posted this in the Direct X section as I am hoping to get answers in the form of D3DXQuaternion... method calls rather than a load of maths which may only hinder my progression.

Anyhow my problem is fairly straightforward. I have a simple model viewer which shows my model in the middle of the screen. When I press the keys X,Y or Z the model should rotate in MODEL coordinates. I am trying to use Quaternions to do this.

The results are that I can only get a single axis to rotate in model space, the other two will subsequently rotate in world space. My code is:



  D3DXVECTOR3 axisZ(0,0,1);
  D3DXVECTOR3 axisY(0,1,0);
  D3DXVECTOR3 axisX(1,0,0);

  //I am not sure if this is neccessary.

  //The first quat will rotate correctly in model space, the latter 2 will rotate in world space.

  D3DXMATRIX rotation;

Note that ga is an instance of GameActor and ga->getRotation() returns a D3DXVECTOR3 containing x,y and z rotations in radians.

I would be grateful if anyone could give me a high level approach to solve my problem?


#2 derKai   Members   


Posted 05 September 2012 - 05:32 AM

i think there should be method to create a quaternion from yaw pitch roll.. or at least from a matrix that can be created from yaw pitch roll.. . i think this is what you want ;)

#3 benp444   Members   


Posted 05 September 2012 - 05:49 AM

Thanks, but I already tried that:


The results are similar in that one axis rotates in model coords and the rest in world coords. Note in this case it is Z that always rotates in model space and X and Y that rotate in world space.

Edited by benp444, 05 September 2012 - 06:05 AM.

#4 derKai   Members   


Posted 05 September 2012 - 06:33 AM

This is because the rotations are executed one after each other..
if you apply a rotation, lets say 45° around the Y axis, your local (object space) x and z axis are rotated 45°, too.
if you then apply another rotation, lets say around the X axis, you rotate around a "neutral" axis, not the 45° rotated one.
for the next transformation this is same as well..

what rotation are you expecting?

maybe your give vector is not meant to use this way.
did you wrote the GameActor class or is it part of a library?

#5 benp444   Members   


Posted 05 September 2012 - 06:59 AM

In answer to 'what I am expecting':

I have a model of an aircraft. I am looking down on it sitting on the runway. the z axis points out of the screen, the y axis points up and the x axis off to the right. The aircraft is pointing up at the top of the screen.

If I press the Y key I expect it to rotate along the planes fuselage (roll), if I press the X key I expect pitch and Z yaw.

I get all these actions if I only press a single key - this is because model space and world space are initially aligned.

The GameActor class is mine and is very transient at the moment. I can/will add any members and methods which enable me to provide motion. Note that the GameActor code only provides accessors so I seen no point in posting it. The meat of what I am trying to do is posted in my code above.

I sort of understand where you are coming from, and I have read elsewhere that you cannot simply convert Euler angles to quats like this. OK so if I can't rotate around X, then Y, then Z what should my approach be? +1 for that.

Do I need to do something with my axis vectors (axisX/Y/Z in the code above)? Is it wrong to specify them the way I have?

Should I store a single quat as a member of my GameActor class and apply rotations on this - I have read this somewhere, but I cannot see how a single quat would work.

Edited by benp444, 05 September 2012 - 07:00 AM.

#6 derKai   Members   


Posted 05 September 2012 - 08:00 AM

okay..i think you've got a math problem ;)
you need to know that matrix operations (and quaternion rotations) are not commutative!
the order in which you rotate your model matters! rotX * rotY != rotY * rotX
this means it's not enough to store the 3 angles (not even 3 quaternions for each axis, what is practically the same as a single quaternion with all 3 rotations, too)
your problem is, that the order you need the rotations multiplied together depends on you mental intention how it should behave..

what you can do is keep track of your axis:
save x y z as vector (initialized at startup)
if you want to rotate around one axis, say Y, create an quaternion "form axis rotation" and pass the current saved y axis and the desired angle
use this quaternion to transform all three axis
create your rotation with the current axis as basis.. this can be done e.g. by via Matrix.LookAt (eye = zero, lookat = forwardAxis, up = upAxis)
maybe you need to transpose this afterwards.. (try it)

Edited by derKai, 05 September 2012 - 08:01 AM.

#7 Burnt_Fyr   Members   


Posted 05 September 2012 - 08:32 AM

Rather than building your quat from scratch each time, you need to update your models orientation from it's previous orientation. I'm not great with quats but here is what i do with matrices, the ideas should perkolate through regardless.

Edit: didn't see answer previously, do as derkai says :)

Edited by Burnt_Fyr, 05 September 2012 - 08:35 AM.

#8 benp444   Members   


Posted 06 September 2012 - 07:06 AM

Thanks for you help. It has taken me a day of playing with a toy aeroplane to work it out, but I have got it working and the resulting code is posted below. The major problem was that I needed to maintain state of my complete model transform and the 3 axis. My game actor now has a stateful tranformation matrix (m_transform) and 3 stateful axis (m_XAxis, m_YAxis and m_ZAxis).

[source lang="cpp"]void GameActor::VUpdate(float elapsedTime){ D3DXQUATERNION qX; D3DXQUATERNION qY; D3DXQUATERNION qZ; D3DXMATRIX matRotX; D3DXMATRIX matRotY; D3DXMATRIX matRotZ; D3DXMATRIX rotation; //calculate incremental change for 3 axis D3DXQuaternionRotationAxis(&qX,&m_Xaxis,m_rotation.x); D3DXQuaternionRotationAxis(&qY,&m_Yaxis,m_rotation.y); D3DXQuaternionRotationAxis(&qZ,&m_Zaxis,m_rotation.z); //get matix rotations for each axis. D3DXMatrixRotationQuaternion(&matRotX,&qX); D3DXMatrixRotationQuaternion(&matRotY,&qY); D3DXMatrixRotationQuaternion(&matRotZ,&qZ); //calculate final rotation. This will still have slight errors as the axis were //not recalculated between calls to D3DXMatrixRotationQuaternion above, but //the are insignificant. rotation=matRotX*matRotY*matRotZ; //modify the stateful transform m_transform=m_transform*rotation; //will add scale and move later. These will both need to be stateless. //modify the stateful axis D3DXVECTOR4 vec4X; D3DXVec3Transform(&vec4X, &m_Xaxis, &rotation); m_Xaxis=D3DXVECTOR3(vec4X.x,vec4X.y,vec4X.z); //realign Y axis D3DXVECTOR4 vec4Y; D3DXVec3Transform(&vec4Y, &m_Yaxis, &rotation); m_Yaxis=D3DXVECTOR3(vec4Y.x,vec4Y.y,vec4Y.z); //realign Z axis D3DXVECTOR4 vec4Z; D3DXVec3Transform(&vec4Z, &m_Zaxis, &rotation); m_Zaxis=D3DXVECTOR3(vec4Z.x,vec4Z.y,vec4Z.z);}[/source]

I still may still be able to make this faster and with less lines of code. Perhaps use QuaternionRotationYawPitchRoll. If anyone can see how to do this in a better way then please say.


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.