2014-02-08 28 views
0

我一直在寻找很长时间,无法找到这个看似简单的问题的答案。我有一个3d空间,我的相机有x,y,z,yaw,pitch和roll变量,我希望能够将相机向前移动到我正在看的任何地方。大多数相机类有这样的事情:LWJGL向您所看到的方向移动相机

//moves the camera forward relitive to its current rotation (yaw) 
public void walkForward(float distance) 
{ 

    position.x -= distance * (float)Math.sin(Math.toRadians(yaw)); 
    position.z += distance * (float)Math.cos(Math.toRadians(yaw)); 

} 

这非常适用于前进根据您的偏航,但你的y位置永远不会改变。这怎么能改变成也是相对于你的音调而言的,这样你总是会朝着相机看的方向前进?

编辑:

通过向前走,我的意思是通过移动相机上的每个轴有一定量,这取决于你的偏航,俯仰和滚转,无论是在你的面前的金额,会出现得大。这可用于:

position.x += -Math.sin(Math.toRadians(rotation.y)) * Math.cos(Math.toRadians(rotation.x)) * distance; 
    position.z += Math.cos(Math.toRadians(rotation.y)) * Math.cos(Math.toRadians(rotation.x)) * distance; 
    position.y -= -Math.sin(Math.toRadians(rotation.x)) * distance; 

注:rotation.y =偏航,rotation.x =间距,rotation.z会滚动。距离只是多少移动,比方说0.1。

如果你不改变摄像头的转动(在z轴上旋转),这将工作,否则它不会。这个职位现在大概非常相似this,但我不知道如何应用矩阵乘法这是解决了我的相机类,它看起来像这样

public class Camera 
{ 
// Field Of View 
private float fov; 
// Aspect Ratio 
private float aspect; 
// Near Plane 
private float zNear; 
// Far Plane 
private float zFar; 

// Projection matrix 
private Matrix4f projection; 
// View matrix 
private Matrix4f view; 

// Camera position 
private Vector3f position; 
// Camera rotation 
private Vector3f rotation; 

// Vectors for axes 
private Vector3f xAxis, yAxis, zAxis; 

/** 
* Creates a simple 3D Perspective Camera. 
* 
* @param fov The field of view in degrees. 
* @param aspect The aspect ratio. 
* @param zNear The near clipping plane. 
* @param zFar The far clipping plane. 
*/ 
public Camera(float fov, float aspect, float zNear, float zFar) 
{ 
    // Set the local variables 
    this.fov = fov; 
    this.aspect = aspect; 
    this.zNear = zNear; 
    this.zFar = zFar; 

    // Create matrices 
    projection = MatrixUtil.createPerspectiveProjection(fov, aspect, zNear, zFar); 
    view = MatrixUtil.createIdentityMatrix(); 

    // Initialize position and rotation vectors 
    position = new Vector3f(0, 0, 0); 
    rotation = new Vector3f(0, 0, 0); 

    // Create normalized axis vectors 
    xAxis = new Vector3f(1, 0, 0); 
    yAxis = new Vector3f(0, 1, 0); 
    zAxis = new Vector3f(0, 0, 1); 

    // Enable depth testing 
    glEnable(GL_DEPTH_TEST); 
} 

/** 
* Apply the camera's transformations. 
*/ 
public void apply() 
{ 
    // Make the view matrix an identity. 
    view.setIdentity(); 

    // Rotate the view 
    Matrix4f.rotate((float) Math.toRadians(rotation.x), xAxis, view, view); 
    Matrix4f.rotate((float) Math.toRadians(rotation.y), yAxis, view, view); 
    Matrix4f.rotate((float) Math.toRadians(rotation.z), zAxis, view, view); 

    // Move the camera 
    Matrix4f.translate(position, view, view); 
} 

//moves the camera forward relitive to its current rotation (yaw and pitch) 
public void moveForward(float distance) 
{ 
    position.x += -Math.sin(Math.toRadians(rotation.y)) * Math.cos(Math.toRadians(rotation.x)) * distance; 
    position.z += Math.cos(Math.toRadians(rotation.y)) * Math.cos(Math.toRadians(rotation.x)) * distance; 
    position.y -= -Math.sin(Math.toRadians(rotation.x)) * distance; 
} 

(本照相机类是基于this教程)

因此,如何根据偏航,俯仰和俯仰使摄像机向前移动。

回答

1

您有3次旋转:滚动,偏航和俯仰。这些还可以想象为在三维空间中的平面上绘制的圆盘上的点:xy平面(roll),xz平面(yaw),zy平面(pitch)。

为了帮助你想象这一点,假设你在一个立方体内。立方体有6个面,每个面上都有一个圆。下面和上面的2个面是xz平面;你的右边和左边是zy飞机;前面和后面是熟悉的xy飞机。

您的示例是围绕xz平面上绘制的圆的中心的旋转。当一架飞机偏航时,它显然不会改变它的音高。一个卷(xy)看起来是这样的:

position.x -= distance * (float)Math.cos(Math.toRadians(amount)); 
position.y += distance * (float)Math.sin(Math.toRadians(amount)); 

您可能必须更改标志,因为我不确定这是如何实现的。

在xy平面上(一个典型的二维视图),如果绘制一个半径为r的圆,则圆上任意一点在角度theta上的点可以由P(r * cos(theta),r * sin(theta ))。在3D空间中,您有3个平面,并且您必须在所有平面上应用相同的逻辑,才能在该平面上找到适当的旋转值。

虽然我不确定“前移摄像头”是什么意思。