LWJGL 3相机问题

时间:2018-11-19 07:14:56

标签: java lwjgl glfw

问题是当我在项目中创建第三人称相机时,我开始遇到问题。我使用LWJGL 2没问题,所以我遵循与LWJGL 3相同的路径,并针对版本之间的更改进行了调整。我遇到的问题是:

  1. 围绕我的对象旋转时,旋转会倾斜,并且旋转速度非常快。我必须将鼠标拖离屏幕才能以相反的方式旋转。
  2. 旋转时,我的模型不在相机的视线范围内(但是模型仍然是旋转的中心点)。
  3. 当我放大或缩小时,它会连续放大。马不停蹄。 (很确定我可以在几个星期内自行解决此问题)。

我认为其中一些问题是由于版本不同所致。例如,在LWJGL 2中,我将使用Mouse.getDWheel();对于LWJGL 3,我使用了GLFW的scrollCallback来检索Y滚动值。我注意到向上滚动时它返回1.0-5.0(可能会更高,但是5是我见过的最高值),而负值则下降。

以下是我的相机课:

package entities;

import org.joml.Vector3f;
import org.lwjgl.glfw.GLFW;

import input.Input;

public class Camera {

private float distanceFromPlayer = 50;
private float angleAroundPlayer = 0;

private Vector3f position = new Vector3f(0,0,0);
private float pitch = 20;
private float yaw;
private float roll;

private Player player;

public Camera(Player player) {

    this.player = player;

}

public void move(Input input) {
    calculateZoom(input);
    calculatePitch(input);
    calculateAngleAroundPlayer(input);
    float horizontalDistance = calculateHorizontalDistance();
    float verticalDistance = calculateVerticalDistance();
    calculateCameraPosition(horizontalDistance, verticalDistance);
}

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 = player.getRotY() + angleAroundPlayer;
    float offsetX = (float) (horizDistance * Math.sin(Math.toRadians(theta)));
    float offsetZ = (float) (horizDistance * Math.cos(Math.toRadians(theta)));
    position.x = player.getPosition().x - offsetX;
    position.z = player.getPosition().z - offsetZ;
    position.y = player.getPosition().y + verticDistance;
    this.yaw = 180 - (player.getRotY() + angleAroundPlayer);
}

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(Input input) {
    float zoomLevel = input.getMouseWheelVelocity() * 0.1f;
    distanceFromPlayer -= zoomLevel;

}

private void calculatePitch(Input input) {
    if(input.isMouseDown(GLFW.GLFW_MOUSE_BUTTON_1)) {
        //possible issue
        float pitchChange = (float) (input.getMouseY() * 0.01f);
        pitch -= pitchChange;
    }
}

private void calculateAngleAroundPlayer(Input input) {
    if(input.isMouseDown(GLFW.GLFW_MOUSE_BUTTON_2)) {
        float angleChange = (float) (input.getMouseX() * 0.01f);
        angleAroundPlayer -= angleChange;
    }
}
}

这是我的输入类,在这里可以获取鼠标滚轮的速度:

package input;

import java.nio.DoubleBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GL11;

import renderEngine.Window;

public class Input {


private long window;
private boolean[] keys = new boolean[GLFW.GLFW_KEY_LAST];
private boolean[] mouseButtons = new boolean[GLFW.GLFW_MOUSE_BUTTON_LAST];
private float mouseWheelVelocity = 0;


public Input(Window win) {
    super();
    this.window = win.getID();
}

public void init() {

    GLFW.glfwSetScrollCallback(window, (long win, double dx, double dy) -> {
        System.out.println(dy);
        setMouseWheelVelocity((float) dy);


    });
}


public boolean isKeyDown(int keyCode) {

    return GLFW.glfwGetKey(window, keyCode) == 1;

}

public boolean isMouseDown(int mouseButton) {

    return GLFW.glfwGetMouseButton(window, mouseButton) == 1;

}

public boolean isKeyPressed(int keyCode) {

    return isKeyDown(keyCode) && !keys[keyCode];
}

public boolean isKeyReleased(int keyCode) {

    return !isKeyDown(keyCode) && keys[keyCode];

}

public boolean isMousePressed(int mouseButton) {

    return isMouseDown(mouseButton) && !mouseButtons[mouseButton];

}

public boolean isMouseReleased(int mouseButton) {

    return !isMouseDown(mouseButton) && mouseButtons[mouseButton];

}

public double getMouseX() {
    DoubleBuffer buffer = BufferUtils.createDoubleBuffer(1);
    GLFW.glfwGetCursorPos(window, buffer, null);
    return buffer.get(0);
}

public double getMouseY() {
    DoubleBuffer buffer = BufferUtils.createDoubleBuffer(1);
    GLFW.glfwGetCursorPos(window, null, buffer);
    return buffer.get(0);

}

public float getMouseWheelVelocity() {
    return mouseWheelVelocity;
}

public void setMouseWheelVelocity(float mouseWheelVelocity) {
    this.mouseWheelVelocity = mouseWheelVelocity;
}

public void updateInput() {
    for (int i = 0; i < GLFW.GLFW_KEY_LAST; i++) {
        keys[i] = isKeyDown(i);
    }
    for (int i = 0; i < GLFW.GLFW_MOUSE_BUTTON_LAST; i++) {
        mouseButtons[i] = isMouseDown(i);
    }
}

public void currentKeyBind() {
    if(isKeyPressed(GLFW.GLFW_KEY_Q)) {
        GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);
    }
    if(isKeyReleased(GLFW.GLFW_KEY_Q)) {
        GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
    }
}

}

仅就可能出问题的地方提出一些意见,而我应该设法解决的方向将是很棒的。 我到处都在寻找有关glfw和LWJGL的教程和文档,以寻求答案,但如果不完全重建我的大部分项目,我似乎找不到一个。

0 个答案:

没有答案