我有一个带有偏航和俯仰角的相机。
相机的位置是x,y和z。
我需要一种平移偏航和按下按钮(WASD)来沿x和z移动的方法。
因此,我按W键,摄像机矩阵向后移动,看起来像是根据偏航角向前移动,但是我不知道这样做的公式。
相机类(我认为这不是必需的):
public static Vector3f position = new Vector3f(0, 1, 0);
public static double sensitivity;
private float pitch;
private float yaw;
private float roll;
private boolean escape;
private boolean fullscreen;
public Camera() {
escape = false;
fullscreen = false;
Mouse.setGrabbed(true);
}
public void move() {
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
if (escape == true) {
Mouse.setGrabbed(true);
Mouse.setCursorPosition(Display.getWidth()/2,
Display.getHeight()/2);
escape = false;
} else if (escape == false) {
Mouse.setGrabbed(false);
escape = true;
}
}
//Not working rn
if (Keyboard.isKeyDown(Keyboard.KEY_GRAVE) && escape == false) {
if (fullscreen == true) {
try {
Display.setFullscreen(false);
} catch (LWJGLException e) {
e.printStackTrace();
}
fullscreen = false;
} else {
try {
Display.setFullscreen(true);
} catch (LWJGLException e) {
e.printStackTrace();
}
fullscreen = true;
}
}
//Change pitch based off of mouse y
if (Mouse.getY() != Display.getHeight() / 2 && escape == false) {
pitch += (Display.getHeight()/2 - Mouse.getY())/2;
if (pitch > 360) {
pitch = pitch % 360;
}
}
//Change yaw with mouse movement
if (Mouse.getX() != Display.getWidth() / 2 && escape == false) {
yaw -= (Display.getWidth()/2 - Mouse.getX())/2;
if (yaw > 360) {
yaw = yaw % 360;
}
}
//get out of the game
if (escape == false) {
Mouse.setCursorPosition(Display.getWidth() / 2, Display.getHeight()
/2);
}
//Here is the movement code, currently just moves along x/z normally
if (Keyboard.isKeyDown(Keyboard.KEY_S) && escape == false) {
position.x -= (Math.sin(yaw)*.2)*sensitivity;
}
if (Keyboard.isKeyDown(Keyboard.KEY_W) && escape == false) {
position.x += (Math.sin(yaw)*.2)*sensitivity;
}
if (Keyboard.isKeyDown(Keyboard.KEY_A) && escape == false) {
position.z -= (Math.cos(yaw)*.2)*sensitivity;
}
if (Keyboard.isKeyDown(Keyboard.KEY_D) && escape == false) {
position.z += (Math.cos(yaw)*.2)*sensitivity;
}
if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) && escape == false) {
position.y -= 0.015;
}
if (Keyboard.isKeyDown(Keyboard.KEY_SPACE) && escape == false) {
position.y += 0.015;
}
}
public Vector3f getPosition() {
return position;
}
public float getPitch() {
return pitch;
}
public float getYaw() {
return yaw;
}
public float getRoll() {
return roll;
}
感谢阅读。
编辑:
在这里[How to move my camera straight to mouse cursor, with any rotation?
找到了解决方案由于某种原因,如果我向前看,我的W向前移动,如果我向后看,我向后移动,但是两者之间没有。
新代码:
公共静态最终Vector3f getRotationVector(){ float r =(float)Math.toRadians(roll); Matrix4f mat = GameMath.createTransformationmatrix(new Vector3f(),pitch,yaw,0,1); Vector3f vector =新的Vector3f(mat.m20,mat.m21,-mat.m22); 返回新的Vector3f((float)(vector.x * Math.cos(r)+ vector.y * Math.sin(r)),(float)(vector.y * Math.cos(r)-vector.x * Math.sin(r)),vector.z); }
运动:
if (Keyboard.isKeyDown(Keyboard.KEY_S) && escape == false) {
position.x -= getRotationVector().x;
position.z -= getRotationVector().z;
}
if (Keyboard.isKeyDown(Keyboard.KEY_W) && escape == false) {
position.x += getRotationVector().x;
position.x += getRotationVector().z;
}
if (Keyboard.isKeyDown(Keyboard.KEY_A) && escape == false) {
position.z -= getRotationVector().z;
position.x -= getRotationVector().x;
}
if (Keyboard.isKeyDown(Keyboard.KEY_D) && escape == false) {
position.z += getRotationVector().z;
position.x += getRotationVector().x;
}