gpt4 book ai didi

java - 第一人称相机在移动眼睛时有奇怪的行为

转载 作者:行者123 更新时间:2023-11-30 11:23:24 24 4
gpt4 key购买 nike

我正在尝试使用 Java/LWJGL 制作游戏/演示,但在使用第一人称相机时遇到了问题:

我使用 WASD 移动 eye vector 周围,​​无论发生什么奇怪的事情:

  • 当我将视线移开或移向我时,正确的 react 。 View 的左侧部分在我的 View 中被遮挡了。
  • 当我将眼睛移到我的右边或左边时,然后是顶部 resp。对于我的 View , View 的底部被遮挡了。

我已经成功地实现了鼠标移动(环顾四周),现在相关的代码片段:

(注意:Matrix4f 类按列优先顺序工作)

Matrix4f viewMatrix = new Matrix4f();

private void checkKeys() {
if (isKeyCurrentlyDown(Keyboard.KEY_W)) {
eye.updateTranslate(1.0f, 0.0f, 0.0f);
updateView();
}
else if (isKeyCurrentlyDown(Keyboard.KEY_S)) {
eye.updateTranslate(-1.0f, 0.0f, 0.0f);
updateView();
}
else if (isKeyCurrentlyDown(Keyboard.KEY_A)) {
eye.updateTranslate(0.0f, -1.0f, 0.0f);
updateView();
}
else if (isKeyCurrentlyDown(Keyboard.KEY_D)) {
eye.updateTranslate(0.0f, 1.0f, 0.0f);
updateView();
}
}

private void updateView() {
System.out.println("eye = " + eye);
viewMatrix.identity().viewFPS(eye, roll, yaw, pitch);
System.out.println("viewMatrix = " + viewMatrix);
Uniforms.setUniformMatrix4(UNIFORM_VIEW_MATRIX, false, viewMatrix);
}

@Override
protected void render(final double msDelta) {
super.render(msDelta);

glClearColor(0.0f, 0.25f, 0.0f, 1.0f);
glClearDepthf(1f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
program.use();

for (int i = 0; i < 24; i++) {
float fVar = i + currentTime / 1000f * 0.3f;
modelviewMatrix.identity()
.translate(0.0f, 0.0f, -8.0f)
.rotate(currentTime / 1000f * 45.0f, 0.0f, 1.0f, 0.0f)
.rotate(currentTime / 1000f * 21.0f, 1.0f, 0.0f, 0.0f)
.translate(
(float)Math.sin(2.1f * fVar) * 2.0f,
(float)Math.cos(1.7f * fVar) * 2.0f,
(float)Math.sin(1.3f * fVar) * (float)Math.cos(1.5f * fVar) * 2.0f
);
Uniforms.setUniformMatrix4(UNIFORM_MODEL_MATRIX, false, modelviewMatrix.writeToFloatBuffer(modelViewMatrixBuffer));
program.drawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
}
}

#version 440 core

layout(location = 0) in vec4 position;

out VS_OUT {
vec4 color;
} vs_out;

layout(location = 0) uniform mat4 model_matrix;
layout(location = 1) uniform mat4 view_matrix;
layout(location = 2) uniform mat4 proj_matrix;

void main() {
gl_Position = proj_matrix * view_matrix * model_matrix * position;
vs_out.color = position * 2.0 + vec4(0.5, 0.5, 0.5, 0.0);
}

public enum UniformLocation {
UNIFORM_MODEL_MATRIX(0),
UNIFORM_VIEW_MATRIX(1),
UNIFORM_PROJECTION_MATRIX(2)
;

private final int location;

private UniformLocation(final int location) {
this.location = location;
}

public int getLocation() {
return this.location;
}
}

//Column-major order

public Matrix4f viewFPS(final Vector3f eye, final float rollAngle, final float yawAngle, final float pitchAngle) {
//roll = rolling your head, Q&E
//yaw = looking left/right, mouseY
//pitch = looking up/down, mouseX
float sinRoll = (float)Math.sin(Math.toRadians(rollAngle));
float cosRoll = (float)Math.cos(Math.toRadians(rollAngle));
float sinYaw = (float)Math.sin(Math.toRadians(yawAngle));
float cosYaw = (float)Math.cos(Math.toRadians(yawAngle));
float sinPitch = (float)Math.sin(Math.toRadians(pitchAngle));
float cosPitch = (float)Math.cos(Math.toRadians(pitchAngle));

Vector3f xAxis = new Vector3f(
cosYaw * cosPitch,
sinYaw * cosPitch,
-sinPitch
);
Vector3f yAxis = new Vector3f(
cosYaw * sinPitch * sinRoll - sinYaw * cosRoll,
sinYaw * sinPitch * sinRoll + cosYaw * cosRoll,
cosPitch * sinRoll
);
Vector3f zAxis = new Vector3f(
cosYaw * sinPitch * cosRoll + sinYaw * sinRoll,
sinYaw * sinPitch * cosRoll - cosYaw * sinRoll,
cosPitch * cosRoll
);
return multiply(
xAxis.getX(), xAxis.getY(), xAxis.getZ(), -xAxis.dot(eye), //X column
yAxis.getX(), yAxis.getY(), yAxis.getZ(), -yAxis.dot(eye), //Y column
zAxis.getX(), zAxis.getY(), zAxis.getZ(), -zAxis.dot(eye), //Z column
0.0f, 0.0f, 0.0f, 1.0f //W column
);
}

我真的不知道为什么相机表现如此奇怪,甚至意味着什么?
图片的一部分怎么突然变得不可见了?

更新:这可能与 viewFPS() 方法有关,因为那里的翻译看起来有点奇怪,有人可以确认吗?

最佳答案

注意到在 viewFPS 中,您将旋转和平移结合在一个矩阵乘法中。

我不熟悉你构建矩阵的方式,但我可以告诉你什么对我有用。

  • 创建 3 个矩阵,一个用于 X 旋转、Y 旋转和 Z 旋转值。
  • 按照您想要的顺序将这些矩阵相乘。 (我使用 Z、Y 然后 X)
  • 将结果与翻译矩阵相乘。 (即你眼睛的位置)
  • 生成的矩阵就是您的 View 矩阵。

这可能比您正在使用的方法效率低,但让它工作然后从那里优化是不是很棒?拆分旋转轴还带来了一次能够调试 1 个轴的好处。

我使用的旋转矩阵:

enter image description here

enter image description here

enter image description here

其中 、(phi) 和 (psi) 是绕 X、Y 和 Z 轴的旋转。( http://www.fastgraph.com/makegames/3drotation/ )

关于java - 第一人称相机在移动眼睛时有奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21235494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com