本文介紹了獲取旋轉(zhuǎn)和縮放的長(zhǎng)方體頂點(diǎn)的3D坐標(biāo),包括縮放、中心位置和在所有軸上的旋轉(zhuǎn)的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問題描述
我一直在絞盡腦汁,試圖解決我的這個(gè)問題。
我有一個(gè)長(zhǎng)方體,它在所有3個(gè)軸上相對(duì)于世界的中心旋轉(zhuǎn)(它在3D空間上),長(zhǎng)方體的中心位置和立方體在所有軸上的比例(寬度,高度和深度)。我需要找到長(zhǎng)方體所有頂點(diǎn)的坐標(biāo)。
上網(wǎng)時(shí),我只找到了2D案例的例子,不知道如何進(jìn)入3D空間。
有誰(shuí)能幫幫我嗎?我將在LWJGL(輕量級(jí)Java游戲庫(kù))制作的游戲引擎中使用它。
編輯:(for@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;
}
這仍然不準(zhǔn)確,有人能發(fā)現(xiàn)問題嗎?
編輯:解決方案:
角度需要弧度,謝謝你的支持!
推薦答案
如果您正在使用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]));
}
}
}
它計(jì)算給定中心位置的變換矩陣M = T * Rx * Ry * Rz * S
,歐拉繞x旋轉(zhuǎn),然后繞y旋轉(zhuǎn),然后繞z旋轉(zhuǎn),以及給定的單位軸比例因子,然后通過P' = M * P
通過該矩陣變換單位立方體角點(diǎn)的位置。
這篇關(guān)于獲取旋轉(zhuǎn)和縮放的長(zhǎng)方體頂點(diǎn)的3D坐標(biāo),包括縮放、中心位置和在所有軸上的旋轉(zhuǎn)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,