相机沿y轴旋转的运动

时间:2019-03-29 14:57:01

标签: camera lwjgl

位置沿x和z轴更新良好。但是当我转弯时它停止工作;

https://streamable.com/2eabe

我尝试重新编写结构,但是没有用。

private static final float RUN_SPEED = 20;
private static final float TURN_SPEED = 20;

private float currentSpeed = 0;
private float currentSidewaysSpeed = 0;
private float currentTurnSpeed = 0;

public void checkInputs(){
    if(Keyboard.isKeyDown(Keyboard.KEY_W)) {
        this.currentSpeed = -(RUN_SPEED);
    }else if(Keyboard.isKeyDown(Keyboard.KEY_S)) {
        this.currentSpeed = RUN_SPEED/2;
    }else{
        this.currentSpeed = 0;
    }

    if(Keyboard.isKeyDown(Keyboard.KEY_D)){
        this.currentTurnSpeed = -TURN_SPEED;
    }else if(Keyboard.isKeyDown(Keyboard.KEY_A)){
        this.currentTurnSpeed = TURN_SPEED;
    }else{
        this.currentTurnSpeed = 0;
    }

}

public void move(){
    checkInputs();
    float xDistance = currentSpeed * MainGameHandler.getFrameTimeSeconds();
    float zDistance = currentTurnSpeed * MainGameHandler.getFrameTimeSeconds();
    float distance = xDistance + zDistance;
    float dx = (float) (xDistance * Math.sin(Math.toRadians(-getRotY())));
    float dz = (float) (zDistance * Math.sin(Math.toRadians(-getRotY())));
    System.out.println(dx + " " + dz);
    increasePosition(dx, 0, dz);
}

最重要的是要朝任何方向努力。我99.9%的人确定我缺少一两行代码。感谢您的提前帮助

1 个答案:

答案 0 :(得分:0)

好吧,我使用旧运动中的一些代码弄清楚了,这里是最终版本:

private static final float RUN_SPEED = 20;
private static final float TURN_SPEED = 20;

private float currentSpeed = 0;
private float currentSidewaysSpeed = 0;
private float currentTurnSpeed = 0;

public void checkInputs(){
    if(Keyboard.isKeyDown(Keyboard.KEY_A)) {
        this.currentSpeed = -(RUN_SPEED);
    }else if(Keyboard.isKeyDown(Keyboard.KEY_D)) {
        this.currentSpeed = RUN_SPEED;
    }else{
        this.currentSpeed = 0;
    }

    if(Keyboard.isKeyDown(Keyboard.KEY_W)){
        this.currentTurnSpeed = -TURN_SPEED;
    }else if(Keyboard.isKeyDown(Keyboard.KEY_S)){
        this.currentTurnSpeed = TURN_SPEED/2;
    }else{
        this.currentTurnSpeed = 0;
    }

}

public Vector3f move(){
    Vector4f movement = new Vector4f();
    Vector3f position = new Vector3f();
    checkInputs();
    float xDistance = currentSpeed * MainGameHandler.getFrameTimeSeconds();
    float zDistance = currentTurnSpeed * MainGameHandler.getFrameTimeSeconds();
    float distance = xDistance + zDistance;
    float dx = xDistance;
    float dz = zDistance;
    System.out.println(dx + " " + dz);
    movement.x += dx;
    movement.z += dz;
    Matrix4f invertedViewMatrix = new Matrix4f();
    Matrix4f.invert(viewMatrix,invertedViewMatrix);
    Matrix4f.transform(invertedViewMatrix,movement,movement);
    position.x+=movement.x;
    position.z+=movement.z;
    return position;
}

只需为也开始使用opengl lwjgl并需要帮助的每个人发布=)