【问题标题】:How to make the camera move in the same direction in where your facing in with this type of method如何使用这种方法使相机朝着您所面对的方向移动
【发布时间】:2017-02-11 21:28:22
【问题描述】:

我在制作第一人称相机时遇到了麻烦 我正在尝试在谷歌上找到的不同方法,但我不明白该怎么做,这就是我的相机类的样子

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

public Camera() {
    Mouse.setGrabbed(true);
}

public void move() {
    if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
        position.x += (float) (Math.sin(Math.toRadians(rotY)) * 0.5);
    }
    if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
        position.x += 0.05f;
    }
    if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
        position.x -= 0.05f;
    }
    if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
        position.z += 0.05f;
    }
    if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
        position.y += 0.05f;
    }
    if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
        position.y -= 0.05f;
    }
    yaw += Mouse.getDX();
    pitch += -Mouse.getDY();
}

public Vector3f getPosition() {
    return position;
}

public float getPitch() {
    return pitch;
}

public float getYaw() {
    return yaw;
}

public float getRoll() {
    return roll;
}

我在另一个名为 maths 的类中计算旋转和东西,然后将它交给着色器进行渲染,但我不知道如何让它在俯仰偏航的方向上移动。

【问题讨论】:

  • 阅读Understanding 4x4 homogenous transform matrices,尤其是最后两个链接。要修正您的代码,您需要从欧拉角计算指向 forwardright 的向量。这如何取决于您的角度顺序......然后在向前/向后移动时,您将forward 向量添加/子到相机位置,在向右/左移动时,您将right 向量添加/子到相机位置。您只是增加/减少单个坐标,仅当您处于特定旋转时才有效....
  • 我不明白他们这就是我问这个的原因

标签: java opengl 3d lwjgl


【解决方案1】:

相机运动的旋转实际上并不来自相机轴本身(x,y,z)的旋转。相机的运动实际上可以通过基于相机的偏航角在 x 和 z 方向上的增量来计算。

对于您的伪示例:

dx (change in x)  = -1*Math.sin(yaw)*speed (where speed is a constant the camera moves at.)

dz (change in z) = Math.cos(yaw)*speed

position.z+=dz;

position.x+=dx;

这通过基于偏航轴 (360) 度的旋转增加 x 和 z 位置值来工作,尽管如果您还没有将这些转换为弧度,java 会要求您将它们转换为弧度!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-29
    • 2016-12-14
    • 2014-06-21
    相关资源
    最近更新 更多