我正在尝试围绕它自己的坐标旋转第一人称视图“相机”,而是围绕原点旋转。这是我目前的相机平移和旋转代码。
if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
xMod -= 0.0025f * (float)delta * (float)Math.sin(Math.toRadians(camera.rotation.y));
zMod += 0.0025f * (float)delta * (float)Math.cos(Math.toRadians(camera.rotation.y));
}
if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
xMod += 0.0025f * (float)delta * (float)Math.sin(Math.toRadians(camera.rotation.y));
zMod -= 0.0025f * (float)delta * (float)Math.cos(Math.toRadians(camera.rotation.y));
}
if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
xMod -= 0.0025f * (float)delta * (float)Math.sin(Math.toRadians(camera.rotation.y-90));
zMod += 0.0025f * (float)delta * (float)Math.cos(Math.toRadians(camera.rotation.y-90));
}
if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
xMod -= 0.0025f * (float)delta * (float)Math.sin(Math.toRadians(camera.rotation.y+90));
zMod += 0.0025f * (float)delta * (float)Math.cos(Math.toRadians(camera.rotation.y+90));
}
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
Mouse.setGrabbed(false);
}
if (Mouse.isButtonDown(0)) {
Mouse.setGrabbed(true);
}
if (Mouse.isGrabbed()) {
camera.rotation.y += (Mouse.getDX() * 0.005f) * delta;
camera.rotation.x += (Mouse.getDY() * -0.005f) * delta;
}
if (camera.rotation.x >= 90f) {
camera.rotation.x = 90f;
}
else if (camera.rotation.x <= -90f) {
camera.rotation.x = -90f;
}
if (Mouse.isGrabbed()) {
camera.position.x += xMod;
camera.position.z += zMod;
}
camera.reset();
Matrix4f.translate(camera.rotation, camera.matrix(Camera.VIEWMATRIX), camera.matrix(Camera.VIEWMATRIX));
Matrix4f.rotate(degToRad(camera.rotation.x), new Vector3f(1f,0f,0f), camera.matrix(Camera.VIEWMATRIX), camera.matrix(Camera.VIEWMATRIX));
Matrix4f.rotate(degToRad(camera.rotation.y), new Vector3f(0f,1f,0f), camera.matrix(Camera.VIEWMATRIX), camera.matrix(Camera.VIEWMATRIX));
Matrix4f.rotate(degToRad(camera.rotation.z), new Vector3f(0f,0f,1f), camera.matrix(Camera.VIEWMATRIX), camera.matrix(Camera.VIEWMATRIX));
Matrix4f.scale(camera.scale, camera.matrix(Camera.VIEWMATRIX), camera.matrix(Camera.VIEWMATRIX));
camera.reset()会这样做......
public void reset() {
viewMatrix = new Matrix4f();
}
基本上重置了视图矩阵
另外,camera.rotation是vector3f,camera.matrix返回一个矩阵,Camera.ViewMatrix或Camera.ProjectionMatrix
感谢您的帮助。
答案 0 :(得分:0)
首先
Matrix4f.translate(camera.rotation, camera.matrix(Camera.VIEWMATRIX), camera.matrix(Camera.VIEWMATRIX));
没有意义。我假设你在这里camera.position
(那也是错的,但我稍后会再说。
您实际上是先翻译相机,然后旋转(在原点附近)。
现在,您可能认为这是另一种方式,因为您可能已经知道矩阵运算以相反的顺序应用 - 即是如此,但只有当您将变换视为在对象周围移动时才会这样。使用经典的视图矩阵,您可以将相机放置在世界中 - 这恰恰相反。在一个世界中移动相机与使用固定相机并反向移动世界上所有物体完全相同。
使用marix数学,(A * B) ^ -1
与B^-1 * A^-1
相同。因此,当您想要以这种方式定义摄像机时,您必须使用相反的顺序(相反顺序,在向下写入时按顺序结束),但每个变换都会反转。你将需要一个带有否定角度的旋转,然后是一个具有否定位置的平移,以使其工作。