我在LWJGL3游戏中创建了第三人称相机。问题是,当我使用鼠标旋转时,该实体不会在游戏中的其他实体之后被挡住。即:我希望相机仍在观察时围绕实体旋转。 代码:
public class Camera {
private float distanceFromPlayer = 35;
private float angleAroundPlayer = 0;
private Vector3f position = new Vector3f(0, 0, 0);
private float pitch = 20;
private float yaw = 0;
private float roll;
public Camera(Vector3f position, float pitch, float yaw, float roll) {
super();
this.position = position;
this.pitch = pitch;
this.yaw = yaw;
this.roll = roll;
}
public void move() {
calculateZoom();
calculatePitch();
calculateAngleAroundPlayer();
float horizontalDistance = calculateHorizontalDistance();
float verticalDistance = calculateVerticalDistance();
calculateCameraPosition(horizontalDistance, verticalDistance);
this.yaw = 180 - (Main.selectedEntity.getRotY() + angleAroundPlayer);
}
public void invertPitch() {
this.pitch = -pitch;
}
public Vector3f getPosition() {
return position;
}
public float getPitch() {
return pitch;
}
public float getYaw() {
return yaw;
}
public float getRoll() {
return roll;
}
private void calculateCameraPosition(float horizDistance, float verticDistance) {
float theta = angleAroundPlayer;
float offsetX = (float) (horizDistance * Math.sin(Math.toRadians(theta)));
float offsetZ = (float) (horizDistance * Math.cos(Math.toRadians(theta)));
position.x = Main.selectedEntity.getPosition().x - offsetX;
position.z = Main.selectedEntity.getPosition().z - offsetZ;
position.y = Main.selectedEntity.getPosition().y + verticDistance;
}
private float calculateHorizontalDistance() {
return (float) (distanceFromPlayer * Math.cos(Math.toRadians(pitch)));
}
private float calculateVerticalDistance() {
return (float) (distanceFromPlayer * Math.sin(Math.toRadians(pitch)));
}
private void calculateZoom() {
float zoomLevel = (float) (Input.getDWheel());
distanceFromPlayer -= zoomLevel;
if (distanceFromPlayer < 200) {
distanceFromPlayer = 200;
}
}
private void calculatePitch() {
if (Input.isMouseDown(GLFW_MOUSE_BUTTON_1) || Input.isMouseDown(GLFW_MOUSE_BUTTON_2)) {
float pitchChange = (float) (Input.getDY() * 0.2f);
pitch -= pitchChange;
}
}
private void calculateAngleAroundPlayer() {
if (Input.isMouseDown(GLFW_MOUSE_BUTTON_1) || Input.isMouseDown(GLFW_MOUSE_BUTTON_2)) {
float angleChange = (float) (Input.getDX() * 0.3f);
angleAroundPlayer -= angleChange;
}
}
}
非常感谢您的帮助。 Main.selectedEntity只是鼠标光标下当前选择的实体。 Video link showing example