## News:

LWJGL 3.3.2 released

## Keeping camera pitched at player

Started by Void, June 02, 2015, 14:57:25

#### Void

First I'll explain which objects I have.

There is a camera with position x, y, z, pitch and yaw variables.
There is a player with x, y, z and rotationY variables.
There is also a terrain, the camera the camera cannot get below a certain point (10f above the terrain), it keeps 'floating'.

I can change the pitch manually in-game with this code:

In the image below. The arrow represents the camera pointing direction and the color matches the player. The red one is how it is suppose to be on a flat terrain. As you can see, when I move downwards on the terrain, the camera keeps pointing forward instead of pointing to a certain point on my player, until I come back up to the same height or adjust the pitch manually. How can this be fixed by pitching the camera? (See second image).

How it is right now:

How I want it:

#### Kai

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: Wikipedia

EDIT:

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.