•      Sign In
• Create Account

# GLM Quaternion Camera

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.

13 replies to this topic

### #1PaloDeQueso  Members   -  Reputation: 283

Like
0Likes
Like

Posted 23 August 2011 - 10:08 AM

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:

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

I can post the whole class if required, but I think this should be enough.
Douglas Eugene Reisinger II
Projects/Profile Site

Sponsor:

### #2Trienco  Crossbones+   -  Reputation: 2171

Like
0Likes
Like

Posted 23 August 2011 - 12:44 PM

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.
f@dzhttp://festini.device-zero.de

### #3A Brain in a Vat  Members   -  Reputation: 313

Like
0Likes
Like

Posted 23 August 2011 - 12:55 PM

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.

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?

### #4PaloDeQueso  Members   -  Reputation: 283

Like
0Likes
Like

Posted 23 August 2011 - 02:31 PM

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?
Douglas Eugene Reisinger II
Projects/Profile Site

### #5johnchapman  Members   -  Reputation: 540

Like
1Likes
Like

Posted 23 August 2011 - 03:23 PM

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.

### #6PaloDeQueso  Members   -  Reputation: 283

Like
0Likes
Like

Posted 24 August 2011 - 06:51 AM

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!
Douglas Eugene Reisinger II
Projects/Profile Site

### #7 __Homer__   Members   -  Reputation: 58

Like
0Likes
Like

Posted 24 August 2011 - 07:30 AM

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!
In C++, friends have access to your privates.
In ObjAsm, your members are exposed!

### #8PaloDeQueso  Members   -  Reputation: 283

Like
0Likes
Like

Posted 24 August 2011 - 07:35 AM

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.
Douglas Eugene Reisinger II
Projects/Profile Site

### #9johnchapman  Members   -  Reputation: 540

Like
0Likes
Like

Posted 24 August 2011 - 08:31 AM

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!

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.

### #10PaloDeQueso  Members   -  Reputation: 283

Like
0Likes
Like

Posted 24 August 2011 - 12:02 PM

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


Method 2 using the axes
This results in no rotation at all.
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);
}


Douglas Eugene Reisinger II
Projects/Profile Site

### #11Trienco  Crossbones+   -  Reputation: 2171

Like
0Likes
Like

Posted 24 August 2011 - 01:18 PM

LOL. Euler angles come with the problem of gimbal lock. Proper use of quaternions fixes that problem

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.
f@dzhttp://festini.device-zero.de

### #12johnchapman  Members   -  Reputation: 540

Like
0Likes
Like

Posted 24 August 2011 - 01:24 PM

In Method 2 you're not using the 'angles' parameter. Try this:

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);
}

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.

### #13Trienco  Crossbones+   -  Reputation: 2171

Like
0Likes
Like

Posted 24 August 2011 - 02:16 PM

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.

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

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

[ 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) ]

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

orientation = rotationX * rotationY * orientation

[ 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).
f@dzhttp://festini.device-zero.de

### #14PaloDeQueso  Members   -  Reputation: 283

Like
0Likes
Like

Posted 25 August 2011 - 06:42 AM

for the non-fps cameras I stuck with what I had which was simply:

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


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! )
Douglas Eugene Reisinger II
Projects/Profile Site

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.

PARTNERS