C++将 3 维向量(点)旋转到不同的对象空间中

发布于 2024-10-12 12:04:20 字数 356 浏览 8 评论 0原文

我正在研究 OBB 算法的碰撞检测算法,并且已经达到了它工作的地步,但是我通过一些破解代码(使用 Ogre3d 节点)以一种非常无效的方式找到了盒子空间中等效的行,但是我真的希望通过一些小算法尽可能轻松地完成它。

基本上我有 2 个点和一个框(为了简单起见,每个点可以使用 1 点解决方案),这两个点组成一条线。该盒子可以以任何想要的方式旋转,所以我需要旋转盒子,使其轴对齐。为此,我还需要将点旋转到同一轴对齐空间。当我在 2 维中工作时,我能够做到这一点,但我在 3d 中找不到解决方案。

我很容易理解这个概念,通过考虑将铅笔插入粘土盒,然后将盒子旋转到轴对齐,然后进行轴对齐的计算,这使得它变得更容易,但是让线旋转背后的代码给我带来了麻烦。任何帮助将不胜感激:)

I was working on a collision detection algorithms for an OBB algorithm and I've reached the point where it works but I am finding the lines equivalent in box space in a really ineffective way by means of some hacked away code (using Ogre3d nodes) but I would really like it to be done as easily as possible with some small algorithms.

Basically I have 2 points and a box (for simplicity purposes a 1 point solution can be used for each) and these two points make up a line. The box can be rotated any way it wants so I need to rotate the box so it is axis aligned. To do this I also need the points to rotate to the same axis aligned space. I was able to do this when I worked in 2 dimensions but I am having trouble finding a solution with 3d.

I understand the concept easily illustrated by thinking about sticking a pencil into a clay box then rotating the box to be axis aligned and then doing calculations of it being axis aligned making it much easier but the code behind getting the lines to rotate are giving me trouble. Any help would be appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

開玄 2024-10-19 12:04:20

如果你有 3D 盒子,那么你可以将其视为由三个单位向量 a、b 和 c 生成,每个向量都存在于 3 维空间中,并带有某个原点 O。作为预处理步骤,让我们开始假设 O 是原点,因此框中的点由两个向量 u = (x0, y0, z0) 和 v = (x1, y1, z1) 定义。您现在有兴趣回答的问题是 - 假设我们应用旋转变换来旋转盒子,使 a、b 和 c 分别与 x、y 和 z 轴对齐,那么这些点是什么你和v是吗?

我可能是错的,但我认为这可以通过一些简单的数学来完成。您可以首先考虑从正常的规范基到由框向量定义的基的转换。该矩阵由下式给出

      | | | 
M = ( a b c )
      | | | 

即,列为 a、b 和 c 的矩阵。

现在我们有了矩阵 M,我们可以考虑将这个变换反转的矩阵(即将单位向量 a、b 和 c 映射到轴对齐的单位向量)作为 M 的逆矩阵,M- 1..但是,如果您选择将边界框定义为正交的向量 a、b 和 c(假设向量是正交的,您可以通过对它们进行归一化来实现),则 M-1 = MT,M 的转置,由下式给出

        - a -
M^T = ( - b - )
        - c -

即第一行为 a、第二行为 b、第三行为 c 的矩阵。给定这些向量,如果将框旋转为 MTu 和 MTv,您可以算出点 u 和 v 的位置,这两个表达式都应该计算起来不太困难。

希望这有帮助!

If you have the box in 3D, then you can think of it as generated by three unit vectors a, b, and c, each of which exists in 3-space, along with some origin point O. As a preprocessing step, let's begin by assuming that O is the origin and therefore that the points you have in the box are defined by two vectors u = (x0, y0, z0) and v = (x1, y1, z1). The question you're now interested in answering is - assuming that we applied a rotation transformation to rotate the box so that a, b, and c were axis-aligned to the x, y, and z axes, respectively, what would the points u and v be?

I could be wrong about this, but I think that this can be done with some simple math. You can begin by thinking about the transformation you would do to get from the normal canonical basis into the basis defined by the vectors of your box. This matrix is given by

      | | | 
M = ( a b c )
      | | | 

That is, the matrix whose columns are a, b, and c.

Now that we have the matrix M, we can think about the matrix that would invert this transformation (i.e. mapping the unit vectors a, b, and c to the axis-aligned unit vectors) as the inverse of M, M-1. However, if you've chosen the vectors a, b, and c defining your bounding box to be orthonormal (which, assuming that the vectors are orthogonal, you can do by normalizing them), then M-1 = MT, the transpose of M, which is given by

        - a -
M^T = ( - b - )
        - c -

That is, the matrix whose first row is a, whose second row is b, and those third row is c. Given these vectors, you can figure out where points u and v would be if you rotated the box as MTu and MTv, both of which are expressions that shouldn't be too hard to compute.

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文