From your problem description I take it there are the following invariants:
- the distance between the camera and the player projected on the (left-to-right)-direction (which I will call the x-axis from now on) in your image is always constant, i.e. the camera is always at the same distance "behind" the player
- the camera always should look at the exact same point/spot on the player (e.g. the player's center)
- both the y-position (with y being "up-down") of the camera and the player are solely determined by the height of the terrain at that position
Provided all this, the "pitch" (the angle in radians) is just:
pitch = atan(dY / dX)with:
- dY = y-difference between camera's desired position and player position
- dX = constant x-distance between camera and player
Now you can build a rotation transformation about the camera's "pitch" axis.
But how you go about defining your rotation transformation now depends on your representation of such a transformation.
If you use a matrix it is as simple as building a rotation about the pitch axis, like described here:
WikipediaEDIT:
If you do not want to work with angles but with vectors instead and are using LWJGL 2, you can use the convenient GLU.gluLookAt() method.
Using gluLookAt() you can specify the parameters of the camera, such as its position and the point it should look at, directly via method parameters.
This method applies its transformation to OpenGL's current matrix stack. You know, the glMatrixMode(GL_MODELVIEW).
In case you want to use LWJGL 3, you can use the
JOML library which also features this
lookAt() method to build a matrix in the same way as GLU does.
With LWJGL 3 you can then apply the JOML matrix to OpenGL's matrix stack or upload it to a shader.
Have a look at JOML's documentation on its
GitHub site as well as on its
GitHub Wiki.