Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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:

Projectionmatrix

perspectiveMatrix = new Matrix4f();
perspectiveMatrix.setIdentity();

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

//COLUMN / ROW
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;

Viewmatrix

Matrices.viewMatrix = new Matrix4f();
    Matrix4f.setIdentity(Matrices.viewMatrix);

    //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
    position.negate(this.currPos);
    target.normalise(this.zAxis);
    up.normalise(this.yAxis);

    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;

    Matrices.viewMatrix.translate(this.currPos);

Modelmatrix

Identity
share|improve this question

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
Thank you, that is very helpful! Well, guess I don't have to worry about that anymore! –  Simon Petersson Apr 17 '13 at 18:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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