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