Description
I have three vectors d (forward vector), up and right as you can see here .
p represents the position of the camera, d should always face straigt forward, right should face to the right and up should face up. I the camera gets rotated the three vectors should be updated, that they still face in the right directions. My coordinatesystem is: +x to the right, +y up and +z out of the screen:
Approach:
// create the three vectors
// Vector4f because opengl only allows to transform with Matrix4f
d = new Vector4f(0, 0, -1, 0);
right = new Vector4f(1, 0, 0, 0);
// create a transformation matrix
Matrix4f matrix = new Matrix4f();
matrix.setIdentity();
Matrix4f.rotate((float) Math.toRadians(cam.getPitch()), new Vector3f(1, 0, 0), matrix, matrix);
Matrix4f.rotate((float) Math.toRadians(cam.getYaw()), new Vector3f(0, 1, 0), matrix, matrix);
Matrix4f.rotate((float) Math.toRadians(cam.getRoll()), new Vector3f(0, 0, 1), matrix, matrix);
// Transform the 3 vectors with the matrix
Matrix4f.transform(matrix, d, d);
Matrix4f.transform(matrix, right, right);
up = Vector3f.cross(right.xyz, d.xyz, null);
// normalize them
d.normalise(); up.normalise(); right.normalise();
Problem
When the camera points in the direction of the x-axis the vectors are:
Cam Pos(-63, 15, 100) Cam Rot(0, 90, 15)
d(-1, 0, 0); up(0, 1, 0); right(0, 0, -1)
desired: d(1, 0, 0); up(0, 1, 0); right(0, 0, 1)
direction of z-axis:
Cam Pos(-15, -15, 51) Cam Rot(0, 0, 15)
d(0, 0, 1); up(0, 1, 0); right(-1, 0, 0)
desired: d(0, 0, 1); up(0, 1, 0); right(-1, 0, 0)
direction of y-axis (look in the sky)
Cam Pos(-11, -48, 104) Cam Rot(0, 90, -90)
d(-1, 0, 0); up(0, 0, -1); right(0, -1, 0)
desired: d(0, 1, 0); up(-1, 0, 0); right(0, 0, 1)
As you can see no coordinate is correct and I'm not sure, why not...
Maybe someone can figure out, what went wrong while rotating the vectors.
If you need more information feel free to ask and I'll edit the post
EDIT 1:
I got confused with the coordinatesystem. Now it's right.
EDIT 2: I made some progress and edited the source code and the examples. The result is much better, but still not right. For me it looks like a small mistake, maybe someone will find one. And is there a condition for the rotation? (for example only -180° to +180° or something like that?)
This is how my matrix look like (moved from comment by Spektre):