• Advertisement
Sign in to follow this  

GLM Quaternion Camera

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


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

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?

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?

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.

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!

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!

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.

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.

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]

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.

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.

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).

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! :) )

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement