1
\$\begingroup\$

I am writing a OpenGL game engine and also an editor for the engine. In my editor, I can import 3D models from FBX/Collada as a scene graph. Now I want to implement the option for the user to rotate and the camera in the viewport using mouse.

I found many links to rotate the camera by some angle based on the delta x and delta y of the mouse. This is fine. But my problem is selecting the axis for rotation.

For example, if the user moves the mouse in the x axis, I am changing the camera's local rotation angle along y axis (the up axis). But this is not always working. In case if the camera node's parent node is rotated 90 degree in the x axis, when I change the camera's local y axis rotation angle, the scene is rotating in wrong direction. So in this case I have to rotate the camera's z axis angle.

This is my problem. So how can I ensure the camera always finally rotate left and right (whatever the parent nodes' angles are rotated) when the user moves the mouse horizontally?

Also I want to mention that when the user moves mouse up and down, I want the camera to rotate up and down always.

\$\endgroup\$
  • \$\begingroup\$ Multiple camera transformation matrix with parent transformation matrix ... \$\endgroup\$ – A---B Jul 7 '16 at 19:19
  • \$\begingroup\$ @ritwik sinha, yes i am always multiplying the camera's transformation with parents transformation to get the view matrix. But my problem is that parent's transformation is affecting the camera's transformation and so the camera is not rotating in the left and right direction. \$\endgroup\$ – Sankar Jul 8 '16 at 10:38
1
\$\begingroup\$

The problem you are experiencing is called Gimbal Lock.
There are some ways to circumvent this problem. One possibility is to use Quaternions. There are plenty of resources on the web about them so I won't go to deep into the details here.
Quaternions are fast but not very intuitive for a 3D editor. But it is possible to convert Euler angles to Quaternions. There even is a Wikipedia article here. Quaternions also enable you to interpolate between two orientations which is very hard when working with Euler only.

Some resources to get you started:
I also liked this article which contains some nice examples how to work with Quaternions: OpenGL Tutorial 17 : Rotations
This one from gamedev.net has some nice explanations and examples too: Gamedev.net Quaternion Powers

I hope this helps you to get started.

\$\endgroup\$
  • \$\begingroup\$ sorry if i was not clear in my original post but my problem is not Gimbal lock. Even if i use Quaternion, my question is still valid. My question is how to always rotate my camera left to right, even if the camera node's parent nodes' are rotated to any angles. But anyway thanks for the links shared. \$\endgroup\$ – Sankar Jul 8 '16 at 10:36
  • \$\begingroup\$ Sorry if I misunderstood you. The problems you described sounds a lot like gimbal look. When the user looks 90° up and rotates left/right the y and z axes might be switched. Is the camera a object the user can see, or is it the rendering viewport the user uses to view the world? \$\endgroup\$ – Skalli Jul 8 '16 at 11:20
  • \$\begingroup\$ the camera is not a 'renderable' object, it is the rendering viewport that the user sees the world. \$\endgroup\$ – Sankar Jul 8 '16 at 11:28
  • \$\begingroup\$ What is the parent of the camera? Maybe you can show an example of your camera movement code and how your matrices are structured. \$\endgroup\$ – Skalli Jul 8 '16 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.