Creating a rotation matrix

I’m trying to make a rotation matrix for my camera view at the moment and I’m not entirely sure how to do it. I’ve created a camera class with 4 vectors (right, up, forward and origin) and a moveForward method (in java’s OpenGL binding LWJGL) which moves the character forward with a simple origin + forward vector addition, but now I’m on a rotation around the Y axis to simulate turning left/right. I’m not really sure I’d create the rotation matrix and what makes it up.

If anyone could give me a brief walking through or any examples to illustrate your point I would really be grateful. I’ve been struggling to work this out for a few days now.

The reason I’m trying to create these matrices is because I want the scene to rotate around the camera’s position and not the origin. So what I’m doing now is trying to create the matrix for translation and rotation and saving the results to the vectors explained above, then I have an applyCameraTransform method that puts the vectors into a FloatBuffer (as I’m working with java) and then multiplies it by the modelview matrix.

Anyway, any help would be much appreciated.


Check out and pay attention to how the upper 3x3 part of the resulting matrix is computed. You’ll see, that you only need a direction and a linearly independent additional vector.

I want the scene to rotate around the camera’s position and not the origin.

Well, if you’re computing the view matrix like above then this is redundant. Since the camera is assumed to be situated at the origin.

In my previous scene the camera was at the origin, but when they move forward the scene is translated backwards and the middle of the scene remains to be the origin.

Just so you can see what I’ve tried already, I’ve created the rotation matrix and saved it to the right, up and forward vectors. From here I multiply it with the current modelview matrix.

	private void rotationMatrix(float delta,float x,float y,float z)
		float s = (float) Math.sin(delta);
		float c = (float) Math.cos(delta);
		float mag = (float) Math.sqrt(x * x + y * y + z * z);
		if(mag == 0.0f)
		float xx,yy,zz,xy,xz,yz,sz,sy,sx,one_c;
		xx = x * x;
		yy = y * y;
		zz = z * z;
		xy = x * y;
		xz = x * z;
		yz = y * z;
		sz = s * z;
		sy = s * y;
		sx = s * x;
		one_c = 1.0f - c;
		vXAxis.setX(one_c * xx + c);
		vXAxis.setY(one_c * xy + sz);
		vXAxis.setZ(one_c * xz - sy);
		vUp.setX(one_c * xy - sz);
		vUp.setY(one_c * yy + c);
		vUp.setZ(one_c * yz + sx);
		vForward.setX(one_c * xy + sy);
		vForward.setY(one_c * yz - sx);
		vForward.setZ(one_c * zz + c);

Here’s where it’s called from:

	public void rotateY(float delta)

And here’s where I call this one from:


Sorry to pester, I know how annoying that can be, but I’ve sort of run out of options. At the moment I have a method in my Camera class that rotates around the Y axis:

	public void rotateY(float delta)
		delta = (float) Math.toRadians(delta);
		m.m00 = (float) Math.cos(delta);
		m.m02 = -(float) Math.sin(delta);
		m.m20 = (float) Math.sin(delta);
		m.m22 = (float) Math.cos(delta);

When this is called it just seems to reverse my moveForward method. I’m just trying to create a rotation matrix of an angle (delta) and load it into the current transformation matrix. Does anyone see where I’ve made a mistake?

Is it your intention to simulate a free camera you can rotate about y and x?

Yeah, I’m trying to make a first person camera. At the moment I’m just trying to rotate about the Y axis, though.

Then please check my first answer again and read the gluLookAt reference carefully. The function does exactly what you need.

Yeah, I was trying to get that working the other day. I know the first 3 parameters can be the player’s coordinates and the last 3 I can keep as 0 1 0 for now, but how would I calculate where the character is facing?

You got a point the camera is exactly focusing, the center. For instance, if you camera is situated at (10, 10, 10) and you want to look at the origin, the second pack of parameters is (0, 0, 0). It should be obvious, that the eye and the center need to be different to yield meaningful results.

The calculations are simple: If you know which point to look at, then the direction is center - eye. If you know the direction, then the center is some point along the line eye + direction. The direction must not be parallel to the up vector since their cross product would be 0.