获取旋转和缩放的长方体顶点的3D坐标,包括缩放、中心位置和在所有轴上的旋转 [英] Get 3D coordinates of vertices of rotated and scaled cuboid with scale, center position and rotation on all axis

查看:50
本文介绍了获取旋转和缩放的长方体顶点的3D坐标,包括缩放、中心位置和在所有轴上的旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在绞尽脑汁,试图解决我的这个问题。 我有一个长方体,它在所有3个轴上相对于世界的中心旋转(它在3D空间上),长方体的中心位置和立方体在所有轴上的比例(宽度,高度和深度)。我需要找到长方体所有顶点的坐标。

上网时,我只找到了2D案例的例子,不知道如何进入3D空间。

有谁能帮帮我吗?我将在LWJGL(轻量级Java游戏库)制作的游戏引擎中使用它。

编辑:(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;
}

这仍然不准确,有人能发现问题吗?

编辑:解决方案: 角度需要弧度,谢谢你的支持!

推荐答案

如果您正在使用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通过该矩阵变换单位立方体角点的位置。

这篇关于获取旋转和缩放的长方体顶点的3D坐标,包括缩放、中心位置和在所有轴上的旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆