繁体   English   中英

获取旋转和缩放的长方体顶点的 3D 坐标,在所有轴上具有缩放、中心位置和旋转

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM