我有一个问题,我不知道如何继续使用Java / LWJGL计算方向向量来渲染OpenGL。
我有以下系统:
因此我走在XZ飞机上,现在我想实施/实施WASD运动,它应该与我目前的方向有关。 (W =向摄像机前方方向,S =向后方向等)
我有一个偏航角度,其定义如下:
现在我只想要一个代表 yaw 方向的3D矢量,我该怎么做?
我使用以下Java代码,包括答案,但似乎还有另一个错误:
@Override
protected void mouseMoved(final int dx, final int dy) {
float yawDelta = dx / 10f;
float pitchDelta = dy / 10f;
yaw += yawDelta;
pitch += pitchDelta;
System.out.println("yaw = " + yaw);
direction.updateZero().updateTranslate((float)Math.sin(Math.toRadians(yaw)), 0f, (float)Math.cos(Math.toRadians(yaw))).updateNormalized();
System.out.println("direction = " + direction);
updateView();
}
和
private void checkKeys() {
if (isKeyCurrentlyDown(Keyboard.KEY_W)) {
eye.updateTranslate(direction);
updateView();
}
if (isKeyCurrentlyDown(Keyboard.KEY_S)) {
eye.updateTranslate(direction.negated());
updateView();
}
if (isKeyCurrentlyDown(Keyboard.KEY_A)) {
eye.updateTranslate(direction.cross(Vector3f.Y.negated()));
updateView();
}
if (isKeyCurrentlyDown(Keyboard.KEY_D)) {
eye.updateTranslate(direction.cross(Vector3f.Y));
updateView();
}
if (isKeyCurrentlyDown(Keyboard.KEY_Q)) {
eye.updateTranslate(0.0f, -1.0f, 0.0f);
updateView();
}
if (isKeyCurrentlyDown(Keyboard.KEY_Z)) {
eye.updateTranslate(0.0f, 1.0f, 0.0f);
updateView();
}
}
和
private void updateView() {
viewMatrix.identity().fpsView(eye, roll, yaw, pitch);
Uniforms.setUniformMatrix4(UNIFORM_VIEW_MATRIX, false, viewMatrix);
}
随后
public Matrix4f fpsView(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));
//TODO cannot roll yet
Vector3f xAxis = new Vector3f(
cosYaw,
-sinPitch * sinYaw,
-cosPitch * sinYaw
);
Vector3f yAxis = new Vector3f(
0.0f,
cosPitch,
-sinPitch
);
Vector3f zAxis = new Vector3f(
sinYaw,
sinPitch * cosYaw,
cosPitch * cosYaw
);
return multiply(
xAxis.getX(), xAxis.getY(), xAxis.getZ(), 0.0f, //X column
yAxis.getX(), yAxis.getY(), yAxis.getZ(), 0.0f, //Y column
zAxis.getX(), zAxis.getY(), zAxis.getZ(), 0.0f, //Z column
0.0f, 0.0f, 0.0f, 1.0f //W column
).translate(eye);
}
不知何故eye.updateTranslate()
无效,只是将操作数的值添加到eye
坐标。我的逻辑是否存在缺陷?
答案 0 :(得分:1)
y始终为0
x =罪(偏航)
z = cos(偏航)
答案 1 :(得分:1)
旋转部分比你有点复杂。
R = yawMat.pitchMat.rollMat
其中:
yawMat={ { cosZ, -sinZ, 0 }, { sinZ, cosZ , 0 }, {0,0,1 } };
pitchMat = { { cosY , 0 , sinY }, { 0, 1 , 0 }, { -sinY, 0, cosY } };
rollMat = { {1,0,0 }, {0,cosX,-sinX }, {0,sinX,cosX } };
三者的点积是均匀变换中3x3旋转矩阵R的组成部分。点积的顺序很重要,所以要保持一致。
Here是我的参考。
最终的4x4矩阵应该看起来像
T = {{R00,R01,R02,X},{R10,R11,R12,Y},{R20,R21,R22,Z},{0,0,0,1}}
编辑如果你想一次进行一次旋转,那么另外两个3x3矩阵会转到身份,所以你只能在偏航中旋转一次:
R = yawMat.I.I = yawMat
所以:
R = { { cosZ, -sinZ, 0 }, { sinZ, cosZ , 0 }, {0,0,1 } }
同样适用于其他人。
正如您为了构建转换矩阵而编写的那样,它应该是:
Vector3f xAxis = new Vector3f(
cosYaw*cosPitch,
cosYaw* sinPitch*sinRoll - sinYaw*cosRoll,
cosYaw*sinPitch*cosRoll + sinYaw*sinRoll
);
Vector3f yAxis = new Vector3f(
sinYaw*cosPitch,
sinYaw*sinPitch*sinRoll + cosYaw*cosRoll,
sinYaw*sinPitch*cosRoll - cosYaw*sinRoll
);
Vector3f zAxis = new Vector3f(
-sinPitch,
cosPitch*sinRoll,
cosPitch * cosYaw
);
假设固定旋转顺序x - > y - > z后跟翻译X,Y,Z