• 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
GarrickW

How to Use a Quaternion as a Camera Orientation

4 posts in this topic

So a while ago I was trying to figure out how to rotate objects using quaternions, and I eventually succeeded. Now, I'm trying to adapt the same system for use in my camera. I am coding in C++, using OpenGL and SFML. Here is how I rotate and translates objects at the moment:

[CODE]
//Create change quaternions for each axis
Quaternion xOffset = Quaternion();
xOffset.FromAxisAngle(Rotation.x * m_TurnSpeed, 1.0, 0.0, 0.0);
Quaternion yOffset = Quaternion();
yOffset.FromAxisAngle(Rotation.y * m_TurnSpeed, 0.0, 1.0, 0.0);
Quaternion zOffset = Quaternion();
zOffset.FromAxisAngle(Rotation.z * m_TurnSpeed, 0.0, 0.0, 1.0);

//Multiply the change quats by the current quat
m_Orientation = yOffset * zOffset * xOffset * m_Orientation;

//Get matrix
m_Orientation.Normalize();
m_Orientation.RotationMatrix(m_RotationMatrix);

Translation = m_Orientation.MultVect(Translation);

m_Position.x += (Translation.x * m_MoveSpeed);
m_Position.y += (Translation.y * m_MoveSpeed);
m_Position.z += (Translation.z * m_MoveSpeed);
[/CODE]

I apply m_RotationMatrix using glMultMatrix(), and m_Position using glTranslatef().

This works fine for objects rotating and translating about, but when I apply this to my camera, obviously things don't work. Now, I say obviously, because as far as I know, if I want to change the direction I'm "looking at" in OpenGL, I need to apply the [i]reverse [/i]of the changes I'd apply to an object. However, I'm not quite sure how to do this, and all my attempts have failed.

Leaving everything exactly the same, I find rotations don't add up and translations are backwards or inverted. Inverting the input doesn't help.

I believe that using

[CODE]
m_Orientation = m_Orientation * yOffset * zOffset * xOffset;
[/CODE]

Instead of the previous form is appropriate for the camera, and this is enough to fix all the rotation issues, but I simply can't manage to get translations to work after that. I can get close by adding the X and Y changes to the position normally and then subtracting the Z change from the position, but this still leads to wonky behavior when moving and rotating around the Z axis at the same time, and the object ends off spiraling away from the point it is supposed to be pointing towards.

So, any tips on how to adapt the above behavior for proper use as a camera?

Also, when applying a camera to OpenGL, to I need to use glPushMatrix() and glPopMatrix()? I ask because that helped some tests run better than others, but it doesn't seem to make sense to do so, since those are basically for pushing a new [i]local [/i]space for transformations onto the stack (I'm horribly mangling my terminology, I'm sure). Edited by Saldan
0

Share this post


Link to post
Share on other sites
"Doing the reverse" means in this case m_TurnSpeed becomes "-m_TurnSpeed"
And, when translating, m_MoveSpeed is also negated "- m_MoveSpeed"

But don't change the order of the quaternion multiplcations, it's not the same. The idea behind the "camera matrix" (actually called view matrix) is that, if you can't the camera, move the whole world. This means if the camera is moved 2 units to the left, it's the same as if [i]everything else[/i] moves 2 units to the right. Same with rotations, etc. You get the idea.

If you still don't get it working after negating those variables, the way you're extracting the translation part and then apply movement doesn't look quite right. Can't really tell without debugging the numbers.
If I'm not mistaken, that code should make the camera spin around the origin when trying to rotate, rather than rotating around it's center. But in the models you don't see it glitch like that because you normalize the matrix, and use glTranslate to let OpenGL perform the translation for you, so it miraculous works.

But in the camera, you don't have that, so if I'm not mistaken, you are unable to move it, right?
0

Share this post


Link to post
Share on other sites
Thanks for the reply!

[quote name='Matias Goldberg' timestamp='1337580247' post='4941832']
"Doing the reverse" means in this case m_TurnSpeed becomes "-m_TurnSpeed"
And, when translating, m_MoveSpeed is also negated "- m_MoveSpeed"

But don't change the order of the quaternion multiplcations, it's not the same. The idea behind the "camera matrix" (actually called view matrix) is that, if you can't the camera, move the whole world. This means if the camera is moved 2 units to the left, it's the same as if [i]everything else[/i] moves 2 units to the right. Same with rotations, etc. You get the idea.
[/quote]

See, I thought this was the case, but when I try that out (inverting m_TurnSpeed and m_MoveSpeed, leaving the order of the quaternion multiplication alone), what happens is the rotations are in the wrong direction, and they don't add up. So for example, if I turn to the right around the Y axis (by pressing the key meant to turn to the left), then try to rotate around the Z axis, I end up rotating around the global Z axis, not the camera's Z axis, i.e. I end up rotating around the camera's X axis (if it's a 90° turn). When I leave the values alone and reverse the multiplication order, rotation *appears* to work as expected (i.e. rotation around the Y axis, then around the Z axis, leads to a rotation around the new, post-Y-rotation Z axis and not the old one). It might not be, of course; if that's the case, then there's a deeper problem.

[quote name='Matias Goldberg' timestamp='1337580247' post='4941832']
If you still don't get it working after negating those variables, the way you're extracting the translation part and then apply movement doesn't look quite right. Can't really tell without debugging the numbers.
If I'm not mistaken, that code should make the camera spin around the origin when trying to rotate, rather than rotating around it's center. But in the models you don't see it glitch like that because you normalize the matrix, and use glTranslate to let OpenGL perform the translation for you, so it miraculous works.
[/quote]

Hm, that could be a problem. Here is what MultVect() looks like:

[CODE]
sf::Vector3<float> Quaternion::MultVect(sf::Vector3<float> Vector)
{
//From http://www.idevgames.com/articles/quaternions
Quaternion VectorQuat = Quaternion();
VectorQuat.x = Vector.x;
VectorQuat.y = Vector.y;
VectorQuat.z = Vector.z;
VectorQuat.w = 0.0;
Quaternion Inverse = (*this);
Inverse.Invert();
Quaternion Result = Inverse * VectorQuat * (*this);
sf::Vector3<float> ResultVector;
ResultVector.x = Result.x;
ResultVector.y = Result.y;
ResultVector.z = Result.z;
return ResultVector;
}
[/CODE]

I do use glTranslafef() when rendering the scene. Here is what that code looks like (it is called before anything else in the scene is rendered):

[code]
//Rotate camera
glMultMatrixf(m_PlayerCamera.GetMatrix());
//Translate camera
sf::Vector3<float> CameraPos = m_PlayerCamera.GetPosition();
glTranslatef(CameraPos.x, CameraPos.y, CameraPos.z);
[/code]

[quote name='Matias Goldberg' timestamp='1337580247' post='4941832']
But in the camera, you don't have that, so if I'm not mistaken, you are unable to move it, right?
[/quote]

It actually moves quite smoothly, just in the wrong direction. If I do the following, I get near-perfect movement so long as I don't try to rotate around the camera's Z axis.

[CODE]
//Create new quaternions for each axis
Quaternion xOffset = Quaternion();
xOffset.FromAxisAngle(Rotation.x * m_TurnSpeed, 1.0, 0.0, 0.0);
Quaternion yOffset = Quaternion();
yOffset.FromAxisAngle(Rotation.y * m_TurnSpeed, 0.0, 1.0, 0.0);
Quaternion zOffset = Quaternion();
zOffset.FromAxisAngle(Rotation.z * m_TurnSpeed, 0.0, 0.0, 1.0);
//Multiply the new quaternions into the orientation
m_Orientation = m_Orientation * yOffset * zOffset * xOffset;
//Get matrix
m_Orientation.Normalize();
m_Orientation.RotationMatrix(m_RotationMatrix);
///Translate camera
Translation = m_Orientation.MultVect(Translation);
m_Position.x += (Translation.x * m_MoveSpeed);
m_Position.y += (Translation.y * m_MoveSpeed);
m_Position.z -= (Translation.z * m_MoveSpeed);
[/CODE]

Now, mathematically, I'm almost certain something isn't right here, since that's not what I'd expect the proper operations to look like. Edited by Saldan
0

Share this post


Link to post
Share on other sites
Good news! I've solved the problem. The issue was that I should have been pushing a matrix created from the [i]inverse [/i]of the Orientation (as well as reversing all the transformation data, i.e. the rotation and translation). What I do now is

[CODE]
//Rotate camera
glMultMatrixf( m_PlayerCamera.GetInvertedMatrix() );

//Translate camera
sf::Vector3<float> CameraPos = m_PlayerCamera.GetPosition();
glTranslatef(CameraPos.x, CameraPos.y, CameraPos.z);
[/CODE]


Where GetInvertedMatrix() looks like this:

[CODE]
GLfloat* Camera::GetInvertedMatrix()
{
//Get a new quaternion, the inverse of the Orientation
Quaternion InvertedOrientation = m_Orientation;
InvertedOrientation.Invert();

//Fill the matrix with the orientation
InvertedOrientation.RotationMatrix(m_RotationMatrix);

//Return that matrix
return m_RotationMatrix;
}
[/CODE]

Once I do this, everything works as intended.

In case anybody in the future is looking for the way to Invert() a quaternion, here's my code:

[CODE]
void Quaternion::Invert()
{
float Length = 1.0 / (x*x + y*y + z*z + w*w);
x *= -Length;
y *= -Length;
z *= -Length;
w *= Length;
}
[/CODE]
1

Share this post


Link to post
Share on other sites
[quote name='Saldan' timestamp='1337608644' post='4941912']
In case anybody in the future is looking for the way to Invert() a quaternion, here's my code:

[CODE]
void Quaternion::Invert()
{
float Length = 1.0 / (x*x + y*y + z*z + w*w);
x *= -Length;
y *= -Length;
z *= -Length;
w *= Length;
}
[/CODE]
[/quote]

Right on. Clever way to minimize the division ops. This is a handy cheatsheet for quaternion ops/functions (the Boost code is good too, or was, last time that I checked):
[url="http://world.std.com/~sweetser/quaternions/intro/tools/tools.html"]http://world.std.com...ools/tools.html[/url] Edited by taby
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