SOLVED LWJGL Quaternion camera

Hello JGO,

I’m trying to implement quaternions for rotations, but I got stuck. When I try to rotate, the camera goes in random directions. I’m not very experienced with quaternions, so perhaps I’m making huge mistakes in it.


	/**
	 * Add rotation (rx, ry, rz) to the camera
	 */
	public void addRotation(float rx, float ry, float rz) {
		Quaternion.mul(new Quaternion(1f, 0f, 0f, (float) (ry * PI_OVER_180)), rotation, rotation);
		rotation.normalise();
		Quaternion.mul(new Quaternion(0f, 1f, 0f, (float) (rx * PI_OVER_180)), rotation, rotation);
		rotation.normalise();
		Quaternion.mul(new Quaternion(0f, 0f, 1f, (float) (rz * PI_OVER_180)), rotation, rotation);
		rotation.normalise();
	}

Does anyone have experience with quaternions or knows some helpful resources, because I can’t find the answer?

I’m still searching for the solution, but am really stuck with my project. So I’ll try to explain the problem a bit more.

In my update method, I call the function addRotation(rx, ry, rz) to rotate the camera with mouse movement:


	public void addRotation(float rx, float ry, float rz) {
		// Rotate Y axis
		Quaternion.mul(new Quaternion(1f, 0f, 0f, ry * FastMath.PI_OVER_180), rotation, rotation);
		rotation.normalise();
		// Rotate X axis around current rotation
		Quaternion.mul(rotation, new Quaternion(0f, 1f, 0f, rx * FastMath.PI_OVER_180), rotation);
		rotation.normalise();
		Quaternion.mul(new Quaternion(0f, 0f, 1f, rz * FastMath.PI_OVER_180), rotation, rotation);
		rotation.normalise();
	}

The behaviour I want to achieve is like most FPS camera’s have.

And before rendering, I apply the camera:


	/**
	 * Apply the camera's transformations.
	 */
	public void apply() {
		// Make the view matrix an identity.
		view.setIdentity();

		// Rotate the view
		Matrix4f rotMatrix = MatrixUtil.toMatrix4f(rotation);
		Matrix4f.mul(rotMatrix, view, view);
		
		// Move the camera
		Matrix4f.translate(position.negate(null), view, view);
	}

This is how I convert a Quaternion to a Matrix4f:


	public static Matrix4f toMatrix4f(Quaternion q) {
		Matrix4f matrix = new Matrix4f();
		matrix.m00 = 1.0f - 2.0f * (q.getY() * q.getY() + q.getZ() * q.getZ());
		matrix.m01 = 2.0f * (q.getX() * q.getY() + q.getZ() * q.getW());
		matrix.m02 = 2.0f * (q.getX() * q.getZ() - q.getY() * q.getW());
		matrix.m03 = 0.0f;

		// Second row
		matrix.m10 = 2.0f * (q.getX() * q.getY() - q.getZ() * q.getW());
		matrix.m11 = 1.0f - 2.0f * (q.getX() * q.getX() + q.getZ() * q.getZ());
		matrix.m12 = 2.0f * (q.getZ() * q.getY() + q.getX() * q.getW());
		matrix.m13 = 0.0f;

		// Third row
		matrix.m20 = 2.0f * (q.getX() * q.getZ() + q.getY() * q.getW());
		matrix.m21 = 2.0f * (q.getY() * q.getZ() - q.getX() * q.getW());
		matrix.m22 = 1.0f - 2.0f * (q.getX() * q.getX() + q.getY() * q.getY());
		matrix.m23 = 0.0f;

		// Fourth row
		matrix.m30 = 0;
		matrix.m31 = 0;
		matrix.m32 = 0;
		matrix.m33 = 1.0f;

		return matrix;
	}

Currently when I move my mouse, It looks like every frame the Y (up) axis is inverted. And I have no clue why this happens and how to fix it.

Take a look at this topic if you haven’t already. Perhaps it can provide some insight into your problem.

Sorry I can’t be of more help, I barely understand Quaternions as it is.

I’ve searched a lot, but didn’t find that one. Thank you very much!