Camera Y axis rotation problem

So I’ve been banging my head against the wall with this one. I’ve been doing theese tutorials : https://lwjglgamedev.gitbooks.io/3d-game-development-with-lwjgl/content/chapter8/chapter8.html
and everything works nicely, except camera rotation around vertical (Y) axis. The scene rotates around world origin, which is not intuitive to the user. The camera in this implementation has a position vector (3f) and rotation vector (also 3f) which than make up a view matrix.
My intention is to make camera rotate around the point it is looking at. After a lot of googling and reading I managed to get current camera position in world coordinates (or at least so I belive, the output coords seem correct) by multiplying camera position by inverted view matrix. I calculated point I’d like to rotate it around. I calculated the point where I’d like the camera to be after being rotated, in world coordinates. However when I multiply the resulting point by view matrix (which I assume I should since I got its position by multiplying by inverted view matrix), I get weird values, and when I position camera using those, the problem is not quite resolved as the camera still moves weridly. So my questions are :

  1. How does one correctly position camera at known world coordinates ?
  2. Is this even the correct approach to what I’m trying to accomplish ?

From your problem description it sounds like you want an arcball camera rotation.
Since you seem to be using JOML for this, use the following code to apply such a view transformation:


// apply an arcball camera rotation to 'mat':
mat.translate(0, 0, -distanceToCenter)
   .rotateX(upDownRotation)
   .rotateY(leftRightRotation)
   .translate(-center.x, -center.y, -center.z);

It first translates every vertex to be relative to the camera’s center, so that we can rotate about that center.
It then performs rotation about the Y and afterwards about the X axis. This will result in the local X axis of the camera to always be coplanar to the world’s XZ-plane.
Finally, the vertex is displaced ‘distanceToCenter’ back along the local Z axis, so that the camera is looking at its center position again from that specified distance away.
Also, see ArcBallCamera and the ArcBallCameraDemo for a working demo using LWJGL3 and OpenGL 1.1.

Right, so I applied your code example, and rotations work brilliantly. But I can’t figure translations in this context though. Also I must have forgotten converting to radians somewhere, since the rotations work nicely when I make no conversion, when I do it just rotates around origin.

EDIT: So I’ve reached kind of a dead end, I don’t quite understand this code and I can’t seem to figure out how to move the camera now :frowning: I’ll try ThinMatrix’s approach to the 3rd person cam.

So, for posteriority, I based my approach on ThinMatrix’s 3rd person camera tutorial, and applied this to get view matrix :

public Matrix4f getViewMatrix(Camera camera) {
		viewMatrix.identity();
		viewMatrix.rotateX((float) Math.toRadians(camera.getPitch()));
		viewMatrix.rotateY((float) Math.toRadians(camera.getYaw()));
		viewMatrix.translate(-camera.getPosition().x, -camera.getPosition().y, -camera.getPosition().z);
		return viewMatrix;
	}

…and it works as I wanted :slight_smile: