获取旋转和缩放的长方体顶点的 3D 坐标,具有缩放、中心位置和所有轴上的旋转
Get 3D coordinates of vertices of rotated and scaled cuboid with scale, center position and rotation on all axis
我一直在绞尽脑汁想弄清楚我遇到的这个问题。
我有一个长方体,它从中心开始相对于世界的所有 3 个轴的旋转(它在 3D space 上),长方体的中心位置和立方体在所有轴上的比例(宽度,高度和深度) .我需要找到长方体所有顶点的坐标。
在网上浏览时,我只找到了 2D 案例的示例,并不知道如何推进到 3D space。
谁能帮帮我吗?我将在使用 LWJGL(轻量级 Java 游戏库)制作的游戏引擎中使用它。
编辑:(对于@httpdigest):
public Vector3f[] getExtents(){
Matrix4f m = new Matrix4f();
m.translate(getPosition());
m.rotate(getRotation().x, new Vector3f(1, 0, 0));
m.rotate(getRotation().y, new Vector3f(0, 1, 0));
m.rotate(getRotation().z, new Vector3f(0, 0, 1));
m.scale(new Vector3f(getScaleX(), getScaleY(), getScaleZ()));
Vector3f[] corners = new Vector3f[8];
for (int i = 0; i < corners.length; i++) {
int x = i % 2 * 2 - 1;
int y = i / 2 % 2 * 2 - 1;
int z = i / 4 % 2 * 2 - 1;
Vector4f corner = Matrix4f.transform(m, new Vector4f(x, y, z, 1), null);
corners[i] = new Vector3f(corner.x, corner.y, corner.z);
}
return corners;
}
这仍然不准确,有人能找出问题所在吗?
编辑:解决方案:
角度需要以弧度为单位,感谢支持!
如果您使用的是 LWJGL,您也可以使用 JOML,在这种情况下,以下是可能您可能想要的内容:
import org.joml.*;
public class CubePositions {
public static void main(String[] args) {
/* Cuboid center position */
float px = 10, py = 0, pz = 0;
/* Euler angles around x, y and z */
float ax = 0, ay = 0, az = (float) java.lang.Math.PI / 2.0f;
/* Scale factor for x, y und z */
float sx = 1, sy = 3, sz = 1;
/* Build transformation matrix */
Matrix4f m = new Matrix4f()
.translate(px, py, pz) // <- translate to position
.rotateXYZ(ax, ay, az) // <- rotation about x, then y, then z
.scale(sx, sy, sz); // <- scale
/* Compute cube corners and print them */
Vector3f[] corners = new Vector3f[8];
for (int i = 0; i < corners.length; i++) {
int x = i % 2 * 2 - 1;
int y = i / 2 % 2 * 2 - 1;
int z = i / 4 % 2 * 2 - 1;
corners[i] = m.transformPosition(x, y, z, new Vector3f());
System.out.println(String.format(
"Corner (%+d, %+d, %+d) = %s",
x, y, z, corners[i]));
}
}
}
它计算一个变换矩阵M = T * Rx * Ry * Rz * S
给定中心位置,欧拉绕x,然后y,z和给定的单位轴缩放因子旋转,然后变换单位立方角的位置通过该矩阵 P' = M * P
.
我一直在绞尽脑汁想弄清楚我遇到的这个问题。
我有一个长方体,它从中心开始相对于世界的所有 3 个轴的旋转(它在 3D space 上),长方体的中心位置和立方体在所有轴上的比例(宽度,高度和深度) .我需要找到长方体所有顶点的坐标。
在网上浏览时,我只找到了 2D 案例的示例,并不知道如何推进到 3D space。
谁能帮帮我吗?我将在使用 LWJGL(轻量级 Java 游戏库)制作的游戏引擎中使用它。
编辑:(对于@httpdigest):
public Vector3f[] getExtents(){
Matrix4f m = new Matrix4f();
m.translate(getPosition());
m.rotate(getRotation().x, new Vector3f(1, 0, 0));
m.rotate(getRotation().y, new Vector3f(0, 1, 0));
m.rotate(getRotation().z, new Vector3f(0, 0, 1));
m.scale(new Vector3f(getScaleX(), getScaleY(), getScaleZ()));
Vector3f[] corners = new Vector3f[8];
for (int i = 0; i < corners.length; i++) {
int x = i % 2 * 2 - 1;
int y = i / 2 % 2 * 2 - 1;
int z = i / 4 % 2 * 2 - 1;
Vector4f corner = Matrix4f.transform(m, new Vector4f(x, y, z, 1), null);
corners[i] = new Vector3f(corner.x, corner.y, corner.z);
}
return corners;
}
这仍然不准确,有人能找出问题所在吗?
编辑:解决方案: 角度需要以弧度为单位,感谢支持!
如果您使用的是 LWJGL,您也可以使用 JOML,在这种情况下,以下是可能您可能想要的内容:
import org.joml.*;
public class CubePositions {
public static void main(String[] args) {
/* Cuboid center position */
float px = 10, py = 0, pz = 0;
/* Euler angles around x, y and z */
float ax = 0, ay = 0, az = (float) java.lang.Math.PI / 2.0f;
/* Scale factor for x, y und z */
float sx = 1, sy = 3, sz = 1;
/* Build transformation matrix */
Matrix4f m = new Matrix4f()
.translate(px, py, pz) // <- translate to position
.rotateXYZ(ax, ay, az) // <- rotation about x, then y, then z
.scale(sx, sy, sz); // <- scale
/* Compute cube corners and print them */
Vector3f[] corners = new Vector3f[8];
for (int i = 0; i < corners.length; i++) {
int x = i % 2 * 2 - 1;
int y = i / 2 % 2 * 2 - 1;
int z = i / 4 % 2 * 2 - 1;
corners[i] = m.transformPosition(x, y, z, new Vector3f());
System.out.println(String.format(
"Corner (%+d, %+d, %+d) = %s",
x, y, z, corners[i]));
}
}
}
它计算一个变换矩阵M = T * Rx * Ry * Rz * S
给定中心位置,欧拉绕x,然后y,z和给定的单位轴缩放因子旋转,然后变换单位立方角的位置通过该矩阵 P' = M * P
.