So I've defined a Projectionmatrix, Viewmatrix and a Modelmatrix using OpenGL (LWJGL).

But when I translate my Viewmatrix to +X it moves my object to the right (hence my camera is moving to the left), and when I move it to -X the object moves to the left (camera moving to the right). Same goes for Y movement, Z works as it should though.

My matrices are as follows:


perspectiveMatrix = new Matrix4f();

float aspectRatio = WIDTH / HEIGHT;
float zRange = zNear - zFar;

perspectiveMatrix.m00 = (float) (1.0f / (aspectRatio * Math.tan(degreesToRadians(fieldOfView / 2.0f))));
perspectiveMatrix.m11 =  (float) (1.0f / (Math.tan(degreesToRadians(fieldOfView / 2.0f))));
perspectiveMatrix.m22 = (-zNear - zFar) / zRange;
perspectiveMatrix.m23 = 1;
perspectiveMatrix.m32 = (2 * zFar * zNear) / zRange;


Matrices.viewMatrix = new Matrix4f();

    //Initialize all vectors
    this.xAxis = new Vector3f();
    this.yAxis = new Vector3f();
    this.zAxis = new Vector3f();
    this.newPos = new Vector3f();
    this.currPos = new Vector3f();

     * position.xyz = 0, 2, 7
     * target.xyz = 0, 0, -1
     * up.xyz = 0, 1, 0
    //Set all values to the Camera-class values

    Vector3f.cross(target, up, xAxis);
    Vector3f.cross(xAxis, target, yAxis); //Recalulate yAxis to make it valid coordinate system.

     * This defines the View Matrix. This is calculated and explained here:
     * http://ogldev.atspace.co.uk/www/tutorial13/tutorial13.html
     * m(Column)(Row)
    Matrices.viewMatrix.m00 = xAxis.x; Matrices.viewMatrix.m10 = xAxis.y; Matrices.viewMatrix.m20 = xAxis.z;
    Matrices.viewMatrix.m01 = yAxis.x; Matrices.viewMatrix.m11 = yAxis.y; Matrices.viewMatrix.m21 = yAxis.z;
    Matrices.viewMatrix.m02 = zAxis.x; Matrices.viewMatrix.m12 = zAxis.y; Matrices.viewMatrix.m22 = zAxis.z;
    Matrices.viewMatrix.m33 = 1;




What you are describing is exactly how the view matrix is supposed to work. The view matrix is the inverse of the camera's world matrix, so you would expect that translating the view matrix is the same as performing the opposite translation on the camera's world matrix.

The reason the z-axis has the opposite effect is that camera's face down the negative z-axis by default, so that if your object and the camera are at the origin and you translate the camera by +50, the camera is now "in-front" of and looking at the object, since it is looking down the negative z-axis. To achieve this same effect by manipulating the view matrix, you would perform the inverse operation so you would translate it by -50 to bring the camera out in front of the object.

  • \$\begingroup\$ Thank you, that is very helpful! Well, guess I don't have to worry about that anymore! \$\endgroup\$ – Simon Petersson Apr 17 '13 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.