## News:

LWJGL 3.3.2 released

## FP Camera rotation problem

Started by afonsolage, July 03, 2013, 22:27:58

#### afonsolage

Hello folks,

I'm developing with LWJGL about 1 year, but now i'm trying to develop using OpenGL 4.3 and I need to write Model View and others matrices by my own and i'm getting stucked on this process. The Model Matrix and Projection Matrix seems to be OK, the problem is with Camera rotation.

Now I have this camera rotation:

And this is not what I want, I need to rotations be made based on world axis, not on local axis like this camera (I think). So, how can I achieve this rotation? I've read dosen of tutorials and articles and all them talk about Quaternions, Euler Angles, Axis Angles and I understand all of them now, but dont know how to use.

This is my current method:

The Matrix4f, Vector3f and Vector4f arent LWJGL classes, I made them by my own because I need to understand all this.

Best regards.

#### quew8

First of all +1 on making your own classes - same here.
Also +1 on the useless but pretty animation - I approve.

Now then:

-A rotation matrix contains the three coordinate axes - that is what it is. It makes no sense to transform the coordinate vectors by this matrix and then use them in creating a new rotation matrix, because you're just duplicating it by a long and silly method.

-I have no idea if your translation stuff is right or not

-Make sure your classes are using the same order as you write the contents (column major / row major).

Here is how I would suggest doing things. Create each component of the camera transform in its own matrix (ie the x-rotation/pitch, y-rot/yaw, z-rot/roll and translation). Then multiply these matrices together in the right order bearing in mind that due to mathematical magic transforms are applied in the opposite order to that which you multiply them together. So for a regular fps camera, you want to translate to the player position, then rotate by yaw (Y-axis), then by pitch (X-axis), then by roll. So take the z-rot, times it by the x-rot, then by the y-rot and finally by the translation matrix.

Edit:
Quaternions are the best way to handle the rotation side of things, but (No offence meant) if you don't understand this stuff (YET) then you don't have a hope in hell of understanding quaternions. I don't even use them because I don't trust them since I don't really understand them.

#### afonsolage

Hello quew8,

Thanks for your answer. This may sounds strange but I think I got what Quaternions are and I have my own class too. But even if I know what they are I dont know how to use them.

Quaternions are a 4d representation of a rotation, where the last item (w or teta) can be used to calculate a interpolation between two angles, so if I have the vector 0,1,0 and I want to rotate it over 45 degress on Z axis (0,0,1) I just create a quaternion from Axis 0,0,1 using teta as 45 and multiplicate it by a quaternion 0,1,0 with teta 0. This will result in a quaternion intersection between 0,1,0 and 0,0,1 with 45 of degress.

If I understood right, I can use Quaternions to handle my axis rotations. But what I didnt understood is how to use them. Should I store 3 quaternions with X, Y and Z rotation and them multiplicate all them together ((Z * Y) * X) and transform the result into a Matrix4f, multiplicate with my Translation matrix and send to my shader?

Note: Dont know if this matters, but my viewMatrix goes directly to shader and its multiplicated there:

Anyway, When I got home I'll try to use a Matrix4f for pitch, one for yaw, one for roll and one for translation like you said. After that I'll post the results here.