当我在x和z轴上移动相机时,Everythinig正常工作,但是当我在y轴上移动时(向上/向下)..它不会一直看着(0,0,0)。
我有以下代码,Vector3f和Matrix4f来自javax.vecmath。*
private void updateCameraMatrix() {
Vector3f z = new Vector3f();
z.sub(center, lookAt);
z.normalize();
Vector3f x = new Vector3f();
x.cross(z, up);
x.normalize();
Vector3f y = new Vector3f();
y.cross(x, z);
y.normalize();
cameraMatrix.set(new float[] {
x.x, y.x, z.x, 0.0f,
x.y, y.y, z.y, 0.0f,
x.z, y.z, z.z, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
});
cameraMatrix.mul(new Matrix4f(new float[] {
1.0f, 0.0f, 0.0f, -center.x,
0.0f, 1.0f, 0.0f, -center.y,
0.0f, 0.0f, 1.0f, -center.z,
0.0f, 0.0f, 0.0f, 1.0f
}));
}
提前感谢您的帮助!
答案 0 :(得分:2)
我不明白为什么你按照你的方式计算x, y, z,
。虽然要计算一个4x4矩阵并对其应用相机,但我会这样做:
public final Matrix4 lookAt(Vector3 eye, Vector3 center)
{
float dx = eye.x - center.x;
float dy = eye.y - center.y;
float dz = eye.z - center.z;
float pitch = (float) Math.atan2(dy, Math.sqrt(dx * dx + dz * dz));
float yaw = (float) Math.atan2(dz, dx);
pitch = -pitch;
yaw = yaw - 1.57079633f;
Matrix4 camera_matrix = new Matrix4();
matrix.identity();
matrix.rotate(pitch, -1f, 0f, 0f);
matrix.rotate(yaw, 0f, 1f, 0f);
matrix.translate(-eyex, -eyey, -eyez);
return matrix;
}
默认情况下,此处的功能“使用”up
(0, 1, 0)
。
正如您所见,我写的是Vector3
,Matrix4
而非Vector3f
,Matrix4f
,因为我不使用javax.vecmath.*
类。我已经为计算以下内容制作了自定义类,虽然它基本相同,但vecmath可能/应该包含我使用的相同方法。虽然如果它不把它写成评论,我也可以提供它们。