我目前正在尝试实现一个与设置向量相关的 FPS 相机功能。在这种情况下,它将是从行星中心到玩家位置的法线。这将使您能够在地球上行走。相机不会因行星的曲率而扭曲。
目前,我使用它来设置相机方向。
Main.getMap().getLocalizedUpVector(shootPos, up);
Main.getMap().getLocalizedAngle(shootPos, angle);
matrice.rotate(angle.y, RenderElement.AXIS_YAW);
matrice.rotate(angle.x, RenderElement.AXIS_PITCH);
matrice.rotate(yaw,up);
right.set(matrice.m00, matrice.m10, matrice.m20);
if(right.length()>0){
right.normalise();
}
matrice.rotate(pitch, right);
eye.set(matrice.m02, matrice.m12, matrice.m22);
它调用这些函数。
public void getLocalizedUpVector(Vector3f pos, Vector3f res){
res.set(pos.x - center.x, pos.y - center.y, pos.z - center.z);
if(res.length() > 0){
res.normalise();
}
}
public void getLocalizedAngle(Vector3f pos, Vector3f angle){
float deltaX = pos.x - center.x;
float deltaY = pos.y - center.y;
float deltaZ = pos.z - center.z;
float distance = (float)Math.sqrt(deltaX*deltaX + deltaY*deltaY + deltaZ*deltaZ);
float yaw = -(float)Math.atan2(deltaX, deltaZ);
float pitch = (float)Math.asin(deltaY/distance);
angle.set(pitch, yaw, 0);
}
这种实现在地球的一半上运行良好,但在另一边却搞砸了。我怀疑这是因为我的 getLocalizedAngle 函数返回欧拉范围内的值。但我不确定,有什么解决办法吗?