简体   繁体   English

旋转动态3D位置数组

[英]Rotating a dynamic 3D array of positions

So I have a system that holds a cluster of items in positions. 所以我有一个系统,可以在位置上放置一簇物品。 The cluster is stored in an array as follows: 群集存储在数组中,如下所示:

int[,,] = int[length, width, height];

Length, width, and height can all be different depending on the cluster. 长度,宽度和高度都可以根据群集而有所不同。 If I wanted to rotate the entire cluster by a set of degrees (ranging 0 to 360): 如果我想将整个群集旋转一组角度(范围为0到360):

double rX, double rZ, double rY

How can I determine the new positions of each item and export in a new array? 如何确定每个项目的新位置并导出到新数组中?


My busted attempts all start like this: 我的失败尝试都是这样开始的:

int iX = Math.Abs(rX / 90), iZ = Math.Abs(rZ / 90), iY = Math.Abs(rY / 90);
if (iY == 1) // 90 or -90 degrees
{
    group.Length = (rY / 90) * back.Width;
    group.Width  = (rY / 90) * back.Length;
}
else if (iY == 2) // 180 degrees
{
    group.Length *= -1;
    group.Width  *= -1;
}
if (iZ == 1) // 90 or -90 degrees
{
    group.Length = (rZ / 90) * back.Height;
    group.Height = (rZ / 90) * back.Length;
}
else if (iZ == 2) // 180 degrees
{
    group.Length *= -1;
    group.Height *= -1;
}
if (iX == 1) // 90 or -90 degrees
{
    group.Width = (rX / 90) * back.Height;
    group.Height = (rX / 90) * back.Width;
}
else if (iX == 2) // 180 degrees
{
    group.Width *= -1;
    group.Height *= -1;
}
 for(int gX = 0; gX < group.Length; gX++)
{
    for (int gZ = 0; gZ < group.Width; gZ++)
    {
        for (int gY = 0; gY < group.Height; gY++)
        {
            //I lose track here.
        }
    }
}

From there I don't know where to go. 从那里我不知道要去哪里。 group is the cluster I'm trying to rotate, and back is a copy of group before these operations. group是我要旋转的群集,而back是执行这些操作之前的group副本。 The array in this cluster is like this: 该群集中的数组如下所示:

Cluster.Items[,,]

And it's sizes are set to the dimensions of the group . 它的大小设置为group的大小。 The array is based on a X (Length) Z (Width) Y (Height) axis. 该数组基于X(长度)Z(宽度)Y(高度)轴。

I'm guessing the answer has something to do with matrices and flipping certain axis. 我猜答案与矩阵和翻转特定轴有关。

You will need a rotation matrix. 您将需要一个旋转矩阵。

A rotation matrix is a matrix which when multiplied with a vector, will result is a rotation of that vector. 旋转矩阵是一个矩阵,当与向量相乘时,将导致该向量的旋转。

there are three types of rotation matrices 旋转矩阵有三种类型

Rotation around x-Axis 绕x轴旋转

Rx(a) =  [ 1   0      0     0,
           0 cos(a) -sin(a) 0,
           0 sin(a)  cos(a) 0,
           0   0      0     1]

around y-Axis 绕y轴

Ry(a) =  [ cos(a)  0 sin(a) 0,
             0     1  0     0,
           -sin(a) 0 cos(a) 0,
             0     0  0     1]

ans rotation around z-Axis 围绕z轴的ans旋转

Rz(a) =  [ cos(a) -sin(a) 0 0,
           sin(a) cos(y)  0 0,
             0      0     1 0,
             0      0     0 0]

More maths about rotation matrices you will find here 您将在此处找到有关旋转矩阵的更多数学信息

I'm still not convinced of your data structure. 我仍然不相信您的数据结构。 But let me just answer your question. 但是,让我回答您的问题。

First, specify an order of rotations. 首先,指定旋转顺序。 In the following, I assume the order x, z, y. 在下文中,我假设顺序为x,z,y。 Then, find the according rotation matrix (eg from here ). 然后,找到相应的旋转矩阵(例如,从此处开始 )。 Then, multiply the position vector with the matrix to get the new vector. 然后,将位置向量与矩阵相乘以获得新向量。

If the old vector has coordinates x, y, z , then the new vector's x-coordinate would be (first row of the matrix): 如果旧向量的坐标为x, y, z ,则新向量的x坐标为(矩阵的第一行):

newX = x * cos(rZ) * cos(rY) - y * sin(rZ) + z * cos(rZ) * sin(rY)

So the first entry in the row is multiplied with x , the second with y and so on. 因此,该行的第一个条目乘以x ,第二个条目乘以y ,依此类推。 Insert the correct angles any you're done. 插入正确的角度即可。

Since cosine and sine are always -1, 0, or 1 for degrees that are multiples of 90°, the according calculation can be improved to not use the actual sine and cosine functions. 由于余弦和正弦对于90度的倍数始终为-1、0或1,因此可以改进相应的计算以不使用实际的正弦和余弦函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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