我在 OpenGL 中有一个摄像头,它可以在X轴和Z轴上移动。
您可以左右箭头按钮 旋转,向前,向后,向左和向右移动 WASD按钮。这是移动的方法。
public void move(float amount, float dir) {
z += amount * Math.sin((Math.toRadians(ry + 90 * dir)));
x += amount * Math.cos((Math.toRadians(ry + 90 * dir)));
}
" 量"是速度和" dir "是" 0"或" 1"所以它设置" 90"到" 0"或者将其留作" 90"。 " ž"是z轴上的位置。所以" x "和" y "。 " RZ "是z轴上的旋转。因此" rx "和" ry "。
有了这些,我不能在Y轴上移动,即向上和向下。我设法添加旋转代码以使相机 LOOK UP 和 DOWN 但我无法让相机移动到您所在的位置正在寻找。这是旋转方法:
public void rotate(float amount, float way) {
if (way == ROTATE_RIGHT)
ry += amount;
else if (way == ROTATE_LEFT)
ry -= amount;
if (way == ROTATE_UP && rx >= -90)
rx -= amount;
else if (way == ROTATE_DOWN && rx <= 90)
rx += amount;
}
这就是我调用move()方法的方法。
if (Keyboard.isKeyDown(Keyboard.KEY_W))
cam.move(0.01f, Camera.MOVE_STRAIGHT);
if (Keyboard.isKeyDown(Keyboard.KEY_S))
cam.move(-0.01f, Camera.MOVE_STRAIGHT);
if (Keyboard.isKeyDown(Keyboard.KEY_A))
cam.move(0.01f, Camera.MOVE_STRAFE);
if (Keyboard.isKeyDown(Keyboard.KEY_D))
cam.move(-0.01f, Camera.MOVE_STRAFE);
Camera.MOVE_STRAIGHT = 1 且 Camera.MOVE_STRAFE = 0 且 cam是相机对象。
答案 0 :(得分:1)
编辑1 :忘记更改X / Z移动。这应该解决这些问题。
修改2 :针对两种类型的水平移动,将amount
更改为walkAmount
和strafeAmount
。 y
现在也使用walkAmount
。
修改3 :添加了move()
public void move(float amount, float dir) {
/* If you are moving straight ahead, or in reverse, modify the Y value for ascent/descent
* Since you do not change your elevation if you are strafing, dir will be 0. sin(0) = 0, and the elevation will not change (regardless of whether rx is 0).
* When you are moving forward or in reverse, dir will be 1, so the rotation about the x axis will be used, and the elevation will change (unless rx is 0).
*/
y += amount * Math.sin(Math.toRadians(rx) * dir);
/* If you are moving straight ahead, or in reverse, scale the delta X/Z (what comes after the +=) so that you don't move as far.
* For example, if you are aimed straight up, and you move straight ahead (W/S), Math.cos(Math.toRadians(rx) * dir) will evaluate to 0, since you only want to move in the Y direction.
* If you are aimed perfectly straight ahead (rotation about the x axis is 0), and you move straight ahead (A/D), Math.cos(Math.toRadians(rx) * dir) will evalute to 1, and your X/Z
* movement would use the entire amount, since you are no longer moving in the Y direction.
* If you are strafing (A/D), dir will be 0, and Math.cos(Math.toRadians(rx) * dir) will evalute to 1 (cos(0) == 1)). You will ONLY move in the X/Z directions when strafing.
*/
z += amount * Math.cos(Math.toRadians(rx) * dir) * Math.sin((Math.toRadians(ry + 90 * dir)));
x += amount * Math.cos(Math.toRadians(rx) * dir) * Math.cos((Math.toRadians(ry + 90 * dir)));
}
如果这有助于您或您有任何问题,请告诉我。