2017-04-16 87 views
0

我有一个具有X,Y和Z坐标的相机。将相机移动到它正面对的方向

该相机还具有偏航和俯仰。

int cameraX = Camera.getX(); 
int cameraY = Camera.getY(); 
int cameraZ = Camera.getZ(); 
int cameraYaw = Camera.getYaw(); 
int cameraPitch = Camera.getPitch(); 

偏航具有2048个单位360度,所以在160度getYaw()方法将返回1024

目前我仅通过设置Y + 1中的每个环向前移动相机。

Camera.setY(Camera.getY() + 1); 

如何将相机X和Y设置为我面对的方向(偏航)? 我不想在这种情况下使用球场,只是偏航。

+0

矩阵数学一般是这样做的,旋转也可以用四元数完成。你想看看创建一个lookAt函数。看看这个问题的答案http://stackoverflow.com/questions/19740463/lookat-f​​unction-im-going-crazy – DanielCollier

+0

问题是不改变相机的旋转。我只需要转到相机所面对的方向。 – Frunk

+0

这涉及到旋转,你必须旋转向前和向上的向量。然后你沿着向前的向量移动 – DanielCollier

回答

2

如果我正确理解你的问题,你正试图让相机朝着你所看到的方向移动(在2D空间中,你只是水平移动)。

我为C++制作了一个小型的LookAt头文件库,但这里是用Java重写的一部分。这段代码所做的是需要旋转和距离,然后计算需要移动多远(在x和y坐标中)以达到目的。

// Returns how far you need to move in X and Y to get to where you're looking 
// Rotation is in degrees, distance is how far you want to move 
public static double PolarToCartesianX(double rotation, double distance) { 
    return distance * Math.cos(rotation * (Math.PI/180.0D)); 
} 

public static double PolarToCartesianY(double rotation, double distance) { 
    return distance * Math.sin(rotation * (Math.PI/180.0D)); 
} 
相关问题