1
\$\begingroup\$

I have a quaternion class implementation, and when I apply this class to my objects all gone right (rotations), but I have a lot of troubles with the camera. I've been searching a lot trying to solved my problem but nothing works.

When I do a yaw and then a pitch rotation (or vice versa) a roll rotation is introduced. If I do the Y rotation around local axis the undesired roll disappears, but looking up and down will not work properly after a roll rotation (so I need to apply the Y rotation around the global axis).

Like I said, applying the quaternion to the object works fine, so maybe the problem is when passing the rotation data to opengl. I do this like that:

 mainCamera.getAnglesAxis(angle, vAngle);
 glRotatef(-angle, vAngle.getX(), vAngle.getY(), vAngle.getZ());
 glTranslatef(-mainCamera.getX(), -mainCamera.getY(), -mainCamera.getZ());

 glTranslatef(obj.getX(),obj.getY(),obj.getZ());
 cube.getAnglesAxis(angle, vAngle);
 glRotatef(angle, vAngle.getX(), vAngle.getY(), vAngle.getZ());

 drawCube();

I'm doing it right?

\$\endgroup\$
6
  • \$\begingroup\$ Both a quaternion solution and a two basis vector solution will work. There's a difference in storage (4 values vs 6) and you need to keep the two vectors orthogonal in addition to the unit length constraint, but none of these considerations are big obstacles. Since you haven't described what kind of problem with your camera you're trying to solve, we don't have a lot to go on here. Try expanding your question to describe the symptoms you're experiencing. Switching your rotation representation might or might not be helpful for solving it, depending on what the issue is. \$\endgroup\$
    – DMGregory
    Jun 7, 2016 at 11:55
  • \$\begingroup\$ @DMGregory My problem it's that I obtain a slightly rotation about another axis (roll) when I rotate my camera. I tried a lot of things, but it doesn't work or solved this problem but add other (losing degree of freedom for example). It's strange because I can rotate the object without problem with quaternions. I don't know if quaternions and camera it's not a good combination or I'm doing something wrong. \$\endgroup\$
    – STK Oscar
    Jun 7, 2016 at 12:30
  • \$\begingroup\$ Quaternions just represent your data. If the sequence of rotations you're performing introduces roll, then changing to a different representation will just represent that roll differently. What you need to do is examine the rotations you're asking your camera to perform. Roll can creep in from combinations of yaw and pitch if you're rotating around the camera's local axes each time. Check that link - is that the problem you're trying to solve? \$\endgroup\$
    – DMGregory
    Jun 7, 2016 at 13:23
  • \$\begingroup\$ @DMGregory yeah, this solve my problem, but I want my camera to rotate in the local axis. With this, the roll rotation doesn't appear but if I introduce a 90 roll rotation and then try to go up, the camera go right and left (local axis, I know). I'm trying to avoid that. Thank you for your time, seriously, I appreciate it. \$\endgroup\$
    – STK Oscar
    Jun 7, 2016 at 13:33
  • \$\begingroup\$ I'd recommend editing this question to describe your camera problem, rather than the red herring about quaternions versus vectors. Then I or another user can suggest answers that let you keep using local axes. As a general rule when asking for help, watch out for the XY problem where you ask about an attempted solution instead of the real root problem/symptoms you're trying to solve. \$\endgroup\$
    – DMGregory
    Jun 7, 2016 at 13:39

1 Answer 1

5
\$\begingroup\$

As I explained above, this is not an issue with quaternions. Quaternions just represent your rotation.

The problem is that you have mutually conflicting goals for this rotation:

  1. The camera can look in any direction and always turn left/right/up/down about its own local axes
  2. The camera never rolls relative to the world up direction

To see why these are contradictory, imagine the following:

  • Start with your camera facing north
  • Look 90 degrees up (its local up vector/the top of its head is now facing south)
  • Look slightly to the right by rotating the camera around its local up vector (as point 1 says we're allowed to do)
  • We're now looking slightly east, with our local up vector still pointing straight south. In other words, our viewpoint has a roll of 90 degrees relative to the world up direction! (Violating point 2)

Animated gif demonstrating the issue

Games usually solve this in two ways:

  • For first-person games, camera rotation is often clamped to you can't look a full 90 degrees up or down. Yaw rotation follows the world up axis. (Sacrifice point 1) See this answer for more details

  • For free-flying games, the camera lets you look any way you want, and by doing certain moves (like the one above) you can develop some roll in your viewpoint. (Sacrifice point 2) Often these games have an auto-righting feature, where they twist the camera to reduce the roll angle over subsequent frames until you're looking straight upright again.

A neat trick I saw recently in Star Wars Battlefront's fighter battles is that they apply this roll correction only when the player is turning the camera. If you let go of the controls you'll keep whatever roll you have, avoiding the disorienting lurch when the camera twists on its own. For most maneuvers the movement on screen from the player's camera manipulation does a good job of masking the roll correction, so it's hardly noticeable.

So, these are your options. This isn't an artifact of your rotation method or quaternion math, this is just the way rotations in 3D space work, and the best you can do is correct for unwanted roll (gradually, so as not to snap the viewpoint distractingly) or restrict the rotation freedom to avoid it.

\$\endgroup\$
1
  • \$\begingroup\$ First of all, thank you very much for making the effort in explain me my problem. I've wasted my time with this and I was desperate, now I think that a can afford it. I don't know how to compensate this "rotation" right now, but I can try to solved it once I know where my problem is and even enjoy my "program". That you again. \$\endgroup\$
    – STK Oscar
    Jun 7, 2016 at 17:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .