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

GLM Quaternion Camera

13 posts in this topic

I'm trying to switch over to glm as I quite like the glsl syntax. It is also more complete than my own math library and has quaternions, not to mention a few other nifty features. So I decided that for a space sim gimbal locks were definitely a bad thing and set off to rewrite my camera with quaternions via glm... They are actually quite pleasant to use and can't believe I didn't use them before, that is until I came across relative camera movement. Basically, if I don't rotate the camera, everything is fine, but as soon as I do, forward is sometimes backwards or strafe_left, and basically every direction is messed up. From what I understood, if you take the oritentation quaternion multiplied by the movement vector and then by the inverse of the quaternion it should rotate the vector. I've tried that, and I've also tried this little snippet below which is supposed to do the same thing I think. NOTE: Camera rotation by the mouse works great!

quite simply I'm just orienting my relative movement vector with the quaternion and adding that to my camera position:
[code]

void Camera::Move(glm::vec3 movement){
position += glm::gtx::quaternion::rotate(orientation, movement);
}[/code]

I can post the whole class if required, but I think this should be enough.
0

Share this post


Link to post
Share on other sites
People really need to stop making other people believe that Quaternions are the answer to Gimbal lock. They are completely interchangeable with rotation matrices and irrational fear of gimbal lock is NOT one of the good reasons to use them. And especially if someone DOES have issues with gimbal lock, just replacing matrices with quaternions and still basing everything on Euler angles won't help a thing.
0

Share this post


Link to post
Share on other sites
[quote name='Trienco' timestamp='1314125099' post='4852887']
People really need to stop making other people believe that Quaternions are the answer to Gimbal lock. They are completely interchangeable with rotation matrices and irrational fear of gimbal lock is NOT one of the good reasons to use them. And especially if someone DOES have issues with gimbal lock, just replacing matrices with quaternions and still basing everything on Euler angles won't help a thing.
[/quote]

LOL. Euler angles come with the problem of gimbal lock. Proper use of quaternions fixes that problem, and presents lots of other advantages as well. What's your deal, are you one of Euler's ancestors?
0

Share this post


Link to post
Share on other sites
Well, you'll be happy to know I replace my euler angles with a quaternion. I'm pretty sure I'm using it in such a way to avoid said paitfalls. Thanks though for the heads up! Anyone have any ideas?
0

Share this post


Link to post
Share on other sites
Sounds like your movement vector isn't being tranformed into the camera's space properly - although transforming it by the camera's quaternion *should* work...

Another way to do it, assuming that you derive a matrix from the camera's position/orientation at some point (for rendering, etc), would be to pull the camera's world x/y/z out of that. so to move the camera forward, you grab the z vector fron the camera's matrix, scale it by speed*time and add it to the position.
1

Share this post


Link to post
Share on other sites
That did the trick, now I just have to read and figure out how to restrict certain rotation directions so that I don't start "rolling" when simply turning. :)

Thanks a lot lads and possibly lasses!
0

Share this post


Link to post
Share on other sites
The easiest way I've found to constrain an orientation, and you are going to hate me, is to convert the quaternion back into euler angles, constrain those, and then turn it back into a quaternion again (directly - not via matrices).
I'd love to hear if anyone knows how to piecewise do that to a quaternion directly!
0

Share this post


Link to post
Share on other sites
Well, if I'm contraining for a specific type of camera... ie ground based fps camera, then I shouldn't encounter a gimbal lock anyway. So that should be fine. Makes sense, I admit, I had thought of this, but I was trying to avoid it.
0

Share this post


Link to post
Share on other sites
[quote name='PaloDeQueso' timestamp='1314190294' post='4853183']
That did the trick, now I just have to read and figure out how to restrict certain rotation directions so that I don't start "rolling" when simply turning. :)

Thanks a lot lads and possibly lasses!
[/quote]

Well you can think in terms of Euler angles, i.e. rotation around the x, y or z axes (of the camera). So to apply pitch (rotation around the x axis), you simply grab the camera's x axis from the matrix and construct a quaternion using that; I'm not familiar with glm but I assume there's a quaternion(axis, angle) function.
0

Share this post


Link to post
Share on other sites
When I did that it restricted rotation completely...

Here's what I've done so far:

Method 1 (first mentioned)
This still rotates stuff but it still rolls as well; which makes sense because I only fill angles x and y components from the mouse for rotation.
[code]
void Camera::Rotate(glm::vec3 angles){
glm::vec3 euler_angles = glm::gtx::quaternion::eulerAngles(orientation); // orientation is my classes rotation quat
euler_angles.x += angles.x;
euler_angles.y += angles.y;
orientation = glm::quat(euler_angles);
glm::gtc::quaternion::normalize(quaternion);
}
[/code]

Method 2 using the axes
This results in no rotation at all.
[code]
void Camera::Rotate(glm::vec3 angles){
glm::vec3 euler_angles = glm::gtx::quaternion::eulerAngles(orientation);
glm::quat quat_x = glm::gtx::quaternion::angleAxis(euler_angles.x * (180.0f / float(PI)), glm::vec3(view_matrix[[0][0], view_matrix[1][0], view_matrix[2][0]));
glm::quat quat_y = glm::gtx::quaternion::angleAxis(euler_angles.y * (180.0f / float(PI)), glm::vec3(view_matrix[[0][1], view_matrix[1][1], view_matrix[2][1]));
orientation = quat_x * quat_y * orientation;
glm::gtc::quaternion::normalize(quaternion);
}
[/code]
0

Share this post


Link to post
Share on other sites
[quote name='A Brain in a Vat' timestamp='1314125750' post='4852892']LOL. Euler angles come with the problem of gimbal lock. Proper use of quaternions fixes that problem[/quote]

So does proper use of matrices. Quaternions don't fix that because they are quaternions, they fix it, because they aren't Euler angles. The subset of quaternions used represent one rotation around one axis. Hooray, so does a rotation matrix. They are completely interchangeable. Show me any implementation using quaternions to "fix gimbal lock" and I can show you the very same implementation using matrices that does exactly the same (without having to constantly convert stuff back and forth between 3(!) representations of the same thing).

Yes, quaternions are neat if you want to save memory or have to concatenate a lot of rotations (for example skeletal animation). They are not "the only hope against gimbal lock", yet unfortunately way too many people hear "gimbal lock" and have the knee jerk reaction of "i must use quaternions, quaternions are fairy dust". My personal reaction is more like "must store orientation in useful way (rotation matrix), Euler angles suck" (except maybe for fps-style cameras, which I'd consider the limit of their usefulness).

Use them where it makes sense and because you understand WHY you are using them, not because people on the net say it's a magical silver bullet against gimbal lock (which I often take as a sign of "copy/pasted it from tutorial without understanding and now tells everybody to do the same"). I seriously fail to see how they are worth the conversions and overhead for something like a camera class.
0

Share this post


Link to post
Share on other sites
In Method 2 you're not using the 'angles' parameter. Try this:

[code]void Camera::Rotate(glm::vec3 angles){
glm::quat quat_x = glm::gtx::quaternion::angleAxis(angles.x * (180.0f / float(PI)), glm::vec3(view_matrix[[0][0], view_matrix[1][0], view_matrix[2][0]));
glm::quat quat_y = glm::gtx::quaternion::angleAxis(angles.y * (180.0f / float(PI)), glm::vec3(view_matrix[[0][1], view_matrix[1][1], view_matrix[2][1]));
orientation = quat_x * quat_y * orientation;
glm::gtc::quaternion::normalize(quaternion);
}[/code]

Note that you don't need to get the Euler angles from the original orientation quaternion - you're just building 2 new quaternions and applying them to the original rotation.
0

Share this post


Link to post
Share on other sites
[quote name='PaloDeQueso' timestamp='1314208941' post='4853311']
This still rotates stuff but it still rolls as well; which makes sense because I only fill angles x and y components from the mouse for rotation.
[/quote]

The most likely reason it rolls is that for building a quat from Eulers glm is probably applying rotations in the order x,y,z. What you want for a fps style camera is y.x.z (and that's what you do in your second method). The reason I point out that Eulers suck is that they do NOT represent one unique orientation, but _six_ possible orientations depending on the completely arbitrary choice of the order in which you apply them.

Even using quats/matrices you have that problem every frame when you turn mouse movement into rotations (which to apply first?). You can use the mouse coordinates to create a single rotation axis instead (normalized_delta_x * axis_up + normalized_delta_y * axis_right), but that would be counter productive for your needs.

edit: this needs some corrections and explanations...

Also at this point you could just store the cameras transformation matrix and replace your entire Rotate-function with

[s]orientation = glm::gtx::euler_angles:_yawPitchRoll( angles.y, angles.x, 0 ) * orientation;[/s]

[ Actually you can't, because you'd run into the same issues. If you look up 45° in the previous frame and then turn 180° right like this, you end up looking backwards and 45° down (if the function does it the way I expect it to) ]

[s]or[/s] just manually apply the two rotation matrices (angles.y around (0,1,0) and angles.x around (1,0,0) ) as

[s]orientation = rotationX * rotationY * orientation[/s]

[ again fell for it myself... for fps style camera, the whole point is to always rotate around "global" up first (and before your existing transformations) and around "local" right second (and after all previous transformations).. and maybe local "forward" if you want some leaning effect, so the correct order for this special case is:

orientation = rotationX * orientation * rotationY

One thing to always have in mind: matrix multiplication is applying transformations right to left and each one obviously changes your local coordinate system (ie. matrix). By doing rotationY first, you apply it while "local up" is still "world up" (ie. identity matrix).

This should kind of make it obvious that the order of rotations matters and why it simply won't work with just storing/summing up three single angles except for the trivial special case of typical fps style cameras, as long as you apply your angles in the order y,x,z. ]

Bottom line: there is no good reason to deal with either Euler Angles or quaternions for this kind thing (unless fps style is all you want to support).
0

Share this post


Link to post
Share on other sites
for the non-fps cameras I stuck with what I had which was simply:

[code]
orientation = glm::quat(angles) * orientation;
// then normalize
[/code]

that continues to work and thanks toTrienco's wonderful explanation, I stopped using my view matrix vectors as axes for rotation and also read up on the order of operations and how it affects quaternions and voila. I now have a non-fps and a fps camera.

Thank you to everyone so much for so much help! Not only did I get a much improved camera class, but now I have a much better understanding of the usefulness of quaternions as well. (and when they're not useful! :) )
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