I recently asked for help regarding the rotation of a 3D camera in OpenGL. This answer and the following comments helped me significantly, but there is another serious problem: when moving the camera, the movement is often, but not always, in the opposite direction. For example, when the camera’s orientation matrix is identical, the camera moves perfectly. However, if it rotates in any direction, its movement along an axis perpendicular to the axis of rotation will have the opposite sign of the intended movement.
With that said, I think I have an idea why this inconsistent behavior happens:
As we all know, OpenGL uses the right coordinate system:
If I understand this diagram correctly, when the camera is oriented toward identity, the z axis should point INTO to the camera, and the z-values should decrease with distance from the camera (obviously, confirmed here ). (coordinates measured in world space).
However, in my program, the Z axis indicates AWAY from the camera and the z values increase when you move away from the camera. Here is an example:
The camera moved forward along which the negative z axis should be, but appears to be the positive z axis.
If I correctly interpret this behavior as abnormal, it will explain all my problems with signs of the movement of my camera, since the movement that currently looks “correct” would actually be erroneous, and I have consistent signs that I may just invert to result in proper movement.
So the question is:
- Is my Z axis turning in the opposite direction, or should it be that way?
If it's the other way around, why? Judging by several discussions on the topic ( 1 , 2 , 3 ), the error will probably be when I determine my perspective for truncation, so I will put it here:
public static final int P_ZNEAR = 1, P_ZFAR = 500; public static void perspective() { int i = GL11.glGetInteger(GL11.GL_MATRIX_MODE); GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); double ymax, xmax; ymax = P_ZNEAR * Math.tan(FOV / 2); xmax = ymax * ASPECT_RATIO; GL11.glFrustum(xmax, -xmax, -ymax, ymax, P_ZNEAR, P_ZFAR); GL11.glMatrixMode(i); }
source share