I have three vectors representing the forward direction (d
), upward direction (up
), and right direction (right
) in an OpenGL camera system. I need to update these vectors when the camera is rotated, so they continue to face the correct directions. My coordinate system follows the convention of +x
to the right, +y
upward, and +z
out of the screen.
Here's my current approach:
// Create the three vectors (Vector4f used for OpenGL compatibility)
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 the vectors
d.normalise(); up.normalise(); right.normalise();
Problem
However, when the camera is pointing in certain directions, the resulting vectors do not match the expected values. Here are a few examples:
When the camera points in the direction of the x-axis:
Camera Position: (-63, 15, 100)
Camera Rotation: (0, 90, 15)
Current Vectors: d(-1, 0, 0); up(0, 1, 0); right(0, 0, -1)
Desired Vectors: d(1, 0, 0); up(0, 1, 0); right(0, 0, 1)
When the camera points in the direction of the z-axis:
Camera Position: (-15, -15, 51)
Camera Rotation: (0, 0, 15)
Current Vectors: d(0, 0, 1); up(0, 1, 0); right(-1, 0, 0)
Desired Vectors: d(0, 0, 1); up(0, 1, 0); right(-1, 0, 0)
When the camera looks upward along the y-axis:
Camera Position: (-11, -48, 104)
Camera Rotation: (0, 90, -90)
Current Vectors: d(-1, 0, 0); up(0, 0, -1); right(0, -1, 0)
Desired Vectors: d(0, 1, 0); up(-1, 0, 0); right(0, 0, 1)
None of the calculated vectors match the desired vectors, and I'm unsure what went wrong during the rotation. If you require additional information, please let me know, and I will update the post accordingly.
EDIT: I have made some progress by modifying the source code and examples. The resulting vectors are now closer to the desired values, but they still fall short. It seems like there might be a small mistake or oversight that is causing the discrepancy. Additionally, I am curious if there are any specific conditions or limitations for the rotation angles, such as a restricted range (e.g., -180° to +180°), that I should be aware of? (for example only -180° to +180° or something like that?)
Here is a visual representation of my transformation matrix: