大家好。我需要一些 3d 旋转方面的帮助。我没有做太多的游戏开发,几乎不记得我在学术界学到了什么。我在 3d 世界(Minecraft)中渲染 2d 图像,并使玩家的目标坐标与图像的左下角对齐。
- XZ 的平面是游戏中可行走的表面
- Y 是垂直的
- 图像将始终呈 90 度角
问题是当我尝试水平渲染图像时,它大多不起作用。有时它是水平的,有时是垂直的。当我垂直渲染时,它工作得很好。我敢肯定它的数学不好。在大多数情况下,我反复试验并犯了错误。俯仰和偏航是相对于玩家的视角。这就是我试图用来设置图像位置和方向的东西。
这是一个开源的 Minecraft 服务器插件。随意看看:
https://bukkit-modders.googlecode.com/svn/trunk/
此外,如果您发现可以通过使用更多的矩阵/向量 voodoo 来减少逻辑,请随时发表评论。
private Matrix4d calculateOrientation(Player player) {
Matrix4d orientation = new Matrix4d();
orientation.setIdentity();
Location location = player.getLocation();
Matrix4d rotY = new Matrix4d();
rotY.setIdentity();
double yaw = Math.abs(location.getYaw());
yaw %= 360;
double baseAngle = 0;
int sign = (location.getYaw() < 0) ? -1 : 1;
if (yaw > (90 - 45) & yaw < (90 + 45)) {
baseAngle = 90;
} else if (yaw > (180 - 45) & yaw < (180 + 45)) {
baseAngle = 180;
} else if (yaw > (270 - 45) && yaw < (270 + 45)) {
baseAngle = 270;
}
// Alight the left edge with the targeted location
//I have no idea why this works.
if (sign > 0) {
baseAngle -= 180;
rotY.rotY(Math.toRadians(baseAngle));
} else {
baseAngle += 180;
rotY.rotY(Math.toRadians(baseAngle));
}
Matrix4d rotXZ = new Matrix4d();
rotXZ.setIdentity();
double rotX = 0;
double rotZ = 0;
if (location.getPitch() > 45) {
//Rendering Flat in the plane of XZ
//This block is pretty broken
if (baseAngle == 0) {
rotX = 90;
} else if (baseAngle == 90) {
rotZ = 90;
} else if (baseAngle == 180) {
} else if (baseAngle == 270) {
rotZ = -90;
}
}
rotXZ.rotX(Math.toRadians(rotX));
rotXZ.rotZ(Math.toRadians(rotZ));
orientation.mul(rotXZ);
orientation.mul(rotY);
return orientation;
}
}