Opengl LWJGL将相机移向您所看到的方向

Opengl LWJGL将相机移向您所看到的方向,opengl,camera,lwjgl,forward,Opengl,Camera,Lwjgl,Forward,我已经搜索了很长一段时间,但找不到这个看似简单的问题的答案。我有一个3d空间,我的相机有x、y、z、偏航、俯仰和滚动变量,我希望能够将相机朝着我所看到的方向移动。大多数相机类都有类似的功能: //moves the camera forward relitive to its current rotation (yaw) public void walkForward(float distance) { position.x -= distance * (float)Math.sin(

我已经搜索了很长一段时间,但找不到这个看似简单的问题的答案。我有一个3d空间,我的相机有x、y、z、偏航、俯仰和滚动变量,我希望能够将相机朝着我所看到的方向移动。大多数相机类都有类似的功能:

//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;
注:旋转。y=偏航,旋转。x=俯仰,旋转。z将是滚动。距离就是移动的距离,比如说0.1

如果不更改摄影机的滚动(沿z轴旋转),则此操作将有效,否则将无效。这篇文章可能和现在很相似,但我不知道如何将矩阵乘法应用到我的camera类中,就像这样

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;
}
(此摄影机类基于教程)


因此,如何使相机根据偏航、俯仰和滚动向前移动。

您有3个旋转:滚动、偏航和俯仰。这些也可以想象为在三维空间中平面上绘制的磁盘上的点:xy平面(滚动)、xz平面(偏航)、zy平面(俯仰)

为了帮助你形象化,想象你在一个立方体里。立方体有6个面,每个面上都画了一个圆。下面和上面的两个面是xz平面;在你的右边和左边是zy平面;前面和后面是熟悉的xy平面

您的示例是围绕xz平面上绘制的圆的中心旋转。当飞机偏航时,它显然不会改变它的俯仰。滚动(xy)看起来像:

position.x -= distance * (float)Math.cos(Math.toRadians(amount));
position.y += distance * (float)Math.sin(Math.toRadians(amount));
你可能必须改变标志,因为我不确定这是如何实现的

在xy平面(典型的2d视图)上,如果绘制半径为r的圆,则圆上角度θ处的任何点都可以由p(r*cos(θ),r*sin(θ))给出。在三维空间中,有3个平面,必须在所有平面上应用相同的逻辑,以便找到该平面上旋转的适当值

不过,我不知道你说的“向前移动摄影机”是什么意思