LWJGL 3
Hey again,
So I’ve written this Camera class which handles the viewport transformation but there always seems to be something wrong with the transformation. If I negate the z component of the translation, I can get the camera to move in the right direction… sort of. Basically the more you move around the scene, the more the movement functions fall apart, and the camera begins to move in the wrong direction.
package com.ibm.bitone.renderer;
import org.joml.Matrix4f;
import org.joml.Vector3f;
public class Camera
{
private final float PI = 3.1415926535897932384626433832795f;
private Vector3f position;
private Vector3f rotation;
private Vector3f forward;
private Vector3f right;
private Vector3f up;
private Matrix4f viewMatrix;
//private double movementSpeed = 1.0;
public void init()
{
viewMatrix = new Matrix4f();
//viewMatrix.lookAt(0.0f, 3.0f, -5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, viewMatrix);
rotation = new Vector3f();
position = new Vector3f();
forward = new Vector3f();
right = new Vector3f();
up = new Vector3f();
//position.set(0.0f, 3.0f, -5.0f);
}
public void updateView(float rotX, float rotY, float rotZ)
{
rotation.add(new Vector3f(rotY, rotX, rotZ));
if(rotation.x >= (PI/2))
rotation.x = (PI/2);
else if(rotation.x <= -(PI/2))
rotation.x = -(PI/2);
if(rotation.y >= 2 * PI || rotation.y <= -(2 * PI))
rotation.y = 0;
viewMatrix.identity();
viewMatrix.rotateXYZ(rotation.x, rotation.y, rotation.z);
viewMatrix.translate(position.x, position.y, position.z);
forward.set(viewMatrix.m20, viewMatrix.m21, viewMatrix.m22);
right.set(viewMatrix.m00, viewMatrix.m01, viewMatrix.m02);
up.set(viewMatrix.m10, viewMatrix.m11, viewMatrix.m12);
}
public void moveForward()
{
position.add(forward);
updateView(0, 0, 0);
}
public void moveBackward()
{
Vector3f temp = new Vector3f();
forward.negate(temp);
position.add(temp);
updateView(0, 0, 0);
}
public void moveRight()
{
position.add(right);
updateView(0, 0, 0);
}
public void moveLeft()
{
Vector3f temp = new Vector3f();
right.negate(temp);
position.add(temp);
updateView(0, 0, 0);
}
public Matrix4f getViewMatrix()
{
return new Matrix4f(viewMatrix);
}
}
(Also, I’m aware that I’m not multiplying movement by delta time, I want to get the camera moving in the right direction first )