gpt4 book ai didi

c# - Unity3d 中的 Matrix.RotateAt

转载 作者:行者123 更新时间:2023-11-30 15:52:41 24 4
gpt4 key购买 nike

我一直在尝试在 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;

关于c# - Unity3d 中的 Matrix.RotateAt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53935540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com