I am trying to rotate my camera using a Quaternion
in libGDX. I have a Quaternion
created and being manipulated but I have no idea how to apply it to the camera, everything I've tried hasn't moved the camera at all.
Here is how I set up the rotation Quaternion
:
public void rotateX(float amount) {
tempQuat.set(tempVector.set(1.0f, 0.0f, 0.0f), amount * MathHelper.PIOVER180);
rotation = rotation.mul(tempQuat);
}
public void rotateY(float amount) {
tempQuat.set(tempVector.set(0.0f, 1.0f, 0.0f), amount * MathHelper.PIOVER180);
rotation = tempQuat.mul(rotation);
}
Here is how I am trying to update the camera (Same update method as the original libGDX version but I added the part about the rotation matrix to the top):
public void update(boolean updateFrustum) {
float[] matrix = new float[16];
rotation.toMatrix(matrix);
Matrix4 m = new Matrix4();
m.set(matrix);
camera.view.mul(m);
//camera.direction.mul(m).nor();
//camera.up.mul(m).nor();
float aspect = camera.viewportWidth / camera.viewportHeight;
camera.projection.setToProjection(Math.abs(camera.near), Math.abs(camera.far), camera.fieldOfView, aspect);
camera.view.setToLookAt(camera.position, tempVector.set(camera.position).add(camera.direction), camera.up);
camera.combined.set(camera.projection);
Matrix4.mul(camera.combined.val, camera.view.val);
if (updateFrustum) {
camera.invProjectionView.set(camera.combined);
Matrix4.inv(camera.invProjectionView.val);
camera.frustum.update(camera.invProjectionView);
}
}
I must comment 2 things about your code:
Finally, there are many ways you can rotate your camera, so I can´t really solve your problem, but if you want to see something rotate just call camera.view.rotate(rotation) after the camera.view.setToLookat. May be you should rotate something else (e.g. the direction vector, the up vector, etc), but you can get started with this:
//protected float[] matrix = new float[16];//unused!
public void update(boolean updateFrustum) {
float aspect = camera.viewportWidth / camera.viewportHeight;
camera.projection.setToProjection(Math.abs(camera.near), Math.abs(camera.far), camera.fieldOfView, aspect);
camera.view.setToLookAt(camera.position, tempVector.set(camera.position).add(camera.direction), camera.up);
camera.view.rotate(q); // THIS IS THE ONLY REAL CHANGE TO YOUR CODE!
camera.combined.set(camera.projection);
Matrix4.mul(camera.combined.val, camera.view.val);
if (updateFrustum) {
camera.invProjectionView.set(camera.combined);
Matrix4.inv(camera.invProjectionView.val);
camera.frustum.update(camera.invProjectionView);
}
}
Happy coding!