[英]Get 3D coordinates of vertices of rotated and scaled cuboid with scale, center position and rotation on all axis
我一直在絞盡腦汁想弄清楚我遇到的這個問題。 我有一個長方體,它在所有 3 軸上相對於世界的中心旋轉(它在 3D 空間上),長方體的中心位置和立方體在所有軸上的比例(寬度、高度和深度)。 我需要找到長方體所有頂點的坐標。
在網上瀏覽時,我只找到了2D案例的例子,無法弄清楚如何推進到3D空間。
有人可以幫我嗎? 我將在用 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
。
我們是否擁有與 Eigen 相同的 API 或 C++ 矩陣?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.