我一直在寻找很长时间,无法找到这个看似简单的问题的答案。我有一个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教程)
因此,如何根据偏航,俯仰和俯仰使摄像机向前移动。