FP Camera rotation problem

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

Previous topic - Next topic


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:

public Matrix4f getView() {
		if (!viewChanged) return viewMatrix; 
		if (position == null) position = Vector3f.createEmpty();
		if (rotation == null) rotation = Vector3f.createEmpty();
		viewMatrix = Matrix4f.createIdentity();
		Vector4f forward = Vector4f.multiplicate(viewMatrix, new Vector4f(0, 0, -1, 0));
		Vector4f up = Vector4f.multiplicate(viewMatrix, new Vector4f(0, 1, 0, 0));
		Vector4f right = Vector3f.cross(forward.toVector3f(), up.toVector3f()).toVector4f();
		viewMatrix = new Matrix4f(new float[]{
				right.x,										up.x,										-forward.x,										0,
				right.y,										up.y,										-forward.y,										0,
				right.z,										up.z,										-forward.z,										0,
				-Vector4f.dot(right, position.toVector4f()),	-Vector4f.dot(up, position.toVector4f()),	-Vector4f.dot(forward, position.toVector4f()),	1	
		return viewMatrix;

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

Thanks for your attention.

Best regards.


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.

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.


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:

#version 430

layout(location = 0)in vec4 vertex;
layout(location = 1)in vec4 fragmentColor;

uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;

out vec4 fragColor;

void main() {
	fragColor = fragmentColor;
	gl_Position = projectionMatrix * viewMatrix * modelMatrix * vertex;

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.

Thanks again for your help.