package com.javamarcher.ecs.component;
import org.joml.Vector3f;
import at.fhooe.mtd.ecs.Component;
/**
* Own transform implementation for the camera position and especially rotation handling.
*/
public class CameraTransform extends Component {
// X Axis rotation
private float pitch = 0f;
// Y Axis rotation
private float yaw = 90f;
public Vector3f position = new Vector3f(0, 0, 0);
public Vector3f forward = new Vector3f(0, 0, 1);
public Vector3f right = new Vector3f(1, 0, 0);
public Vector3f up = new Vector3f(0, 1, 0);
public CameraTransform() {
updateCameraVectors();
}
public void translate(Vector3f translation) {
position.add(translation);
}
public void processMouseMovement(float xOffset, float yOffset) {
yaw += xOffset;
pitch += yOffset;
// Constrain bounds
if(pitch > 89.0f) {
pitch = 89.0f;
}
if(pitch < -89.0f) {
pitch = -89.0f;
}
updateCameraVectors();
}
private void updateCameraVectors() {
final Vector3f front = new Vector3f();
front.x = (float) (Math.cos(Math.toRadians(yaw)) * Math.cos(Math.toRadians(pitch)));
front.y = (float) Math.sin(Math.toRadians(pitch));
front.z = (float) (Math.sin(Math.toRadians(yaw)) * Math.cos(Math.toRadians(pitch)));
forward.set(front.normalize());
// Calculate right and up vector
right.set(new Vector3f(front).cross(Transformation.LOCAL_UP));
up.set(new Vector3f(right).cross(forward).normalize());
}
public float getPitch() {
return pitch;
}
public void setPitch(float pitch) {
this.pitch = pitch;
updateCameraVectors();
}
public float getYaw() {
return yaw;
}
public void setYaw(float yaw) {
this.yaw = yaw;
updateCameraVectors();
}
}