Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 矩阵。在Unity3d中旋转_C#_Unity3d_Math_Matrix - Fatal编程技术网

C# 矩阵。在Unity3d中旋转

C# 矩阵。在Unity3d中旋转,c#,unity3d,math,matrix,C#,Unity3d,Math,Matrix,我一直试图在Unity3d中围绕指定的中心创建旋转矩阵,但是Matrix4x4类没有提供任何允许我这样做的函数,即使C#提供了一个名为: 公共空间旋转(双角度、双中心X、双中心Y) 它位于System.Windows.Media命名空间中,但在Unity3d中无法访问,是否有任何方法可以在Unity3d中创建相同的旋转矩阵?谢谢。围绕点创建旋转矩阵可以按照以下步骤完成: 将该矩阵转换为您希望它旋转的点 旋转矩阵 将矩阵转换回原点 这大致可以理解为: // Set the following

我一直试图在Unity3d中围绕指定的中心创建旋转矩阵,但是Matrix4x4类没有提供任何允许我这样做的函数,即使C#提供了一个名为:

公共空间旋转(双角度、双中心X、双中心Y)


它位于System.Windows.Media命名空间中,但在Unity3d中无法访问,是否有任何方法可以在Unity3d中创建相同的旋转矩阵?谢谢。

围绕点创建旋转矩阵可以按照以下步骤完成:

  • 将该矩阵转换为您希望它旋转的点
  • 旋转矩阵
  • 将矩阵转换回原点
这大致可以理解为:

// Set the following variables according to your setup
Vector3 centerOfRotation = ...;
float angleOfRotation = ...;
Vector3 rotationAxis = ...;

// This should calculate the resulting matrix, as described in the answer
Matrix4x4 translationToCenterPoint = Matrix4x4.Translate(centerOfRotation);
Matrix4x4 rotation = Matrix4x4.Rotate(Quaternion.AngleAxis(angleOfRotation, rotationAxis));
Matrix4x4 translationBackToOrigin = Matrix4x4.Translate(-centerOfRotation);

Matrix4x4 resultMatrix = translationToCenterPoint * rotation * translationBackToOrigin;

即使您可以访问“System.Windows.Media”,Unity的Matrix4x4也无法与之兼容。如果我是你,我会给Matrix4x4写一个扩展方法,它可以做到这一点,并读一点关于仿射变换的知识。是的,这就是我想要的,但当涉及到数学时,我不是最擅长的。肯定有什么我可以做的。现在我只是用一堆偏移量来获得一个不精确的旋转矩阵,这不是问题的一个很好的解决方案。没有单位矩阵的同一个方程不就足够了吗?@Wuszt It!谢谢你的注意。