我一直在尝试围绕Unity3d中的指定中心创建旋转矩阵,但是Matrix4x4类没有提供任何允许我这样做的函数,即使C#确实提供了一个称为的函数:
public void RotateAt(double angle,double centerX,double centerY);
哪个位于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;