• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
benp444

Quaternion x,y,z rotation

7 posts in this topic

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:

[CODE]
D3DXQUATERNION qZ;
D3DXQUATERNION qY;
D3DXQUATERNION qX;
D3DXQUATERNION qOrient;
D3DXQUATERNION qTotal;

D3DXQuaternionIdentity(&qZ);
D3DXQuaternionIdentity(&qY);
D3DXQuaternionIdentity(&qX);
D3DXQuaternionIdentity(&qOrient);


D3DXVECTOR3 axisZ(0,0,1);
D3DXVECTOR3 axisY(0,1,0);
D3DXVECTOR3 axisX(1,0,0);
D3DXQuaternionRotationAxis(&qZ,&axisZ,ga->getRotation().z);
D3DXQuaternionRotationAxis(&qY,&axisY,ga->getRotation().y);
D3DXQuaternionRotationAxis(&qX,&axisX,ga->getRotation().x);

//I am not sure if this is neccessary.
D3DXQuaternionNormalize(&qZ,&qZ);
D3DXQuaternionNormalize(&qY,&qY);
D3DXQuaternionNormalize(&qX,&qX);

//The first quat will rotate correctly in model space, the latter 2 will rotate in world space.
qTotal=qY*qX*qZ;

D3DXMATRIX rotation;
D3DXMatrixRotationQuaternion(&rotation,&qTotal);
world=scale*rotation*move;
[/CODE]

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?

Ben.
0

Share this post


Link to post
Share on other sites
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 ;)
0

Share this post


Link to post
Share on other sites
Thanks, but I already tried that:

[CODE]
D3DXQuaternionRotationYawPitchRoll(&qOrient,ga->getRotation().y,ga->getRotation().x,ga->getRotation().z);
D3DXMatrixRotationQuaternion(&rotation,&qOrient);
world=scale*rotation*move;
[/CODE]

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
0

Share this post


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

Share this post


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

Share this post


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

Ben.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0