2017-04-16 23 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)); 
} 
相關問題