我正在尝试基于 ARKit 演示实现一些代码,其中有人使用此辅助函数来放置航路点
let rotationMatrix = MatrixHelper.rotateAboutY(
degrees: bearing * -1
)
如何使用 SIMD 库而不是使用 GLKit 实现 .rotateAboutY 函数?为了方便起见,我可以从原点开始。
我对矩阵数学不太熟悉,所以更基本的解释会很有帮助。
最佳答案
利用内置 SIMD 库进行此类操作的最佳方法(至少,“最佳”意味着“自己做最少的数学运算”)可能是用四元数表示旋转,并在需要时转换为矩阵。
- 构造一个
simd_quatf
来表示您要应用的轴角度旋转。 - 将该四元数转换为 4x4 矩阵。
您可以通过单线调用来做到这一点:
let rotationMatrix = float4x4(simd_quatf(angle: radians, axis: axis))
扩展到您的“rotateAboutY”案例:
let radians = degreesToRadians(-bearing) // degrees * .pi / 180
let yAxis = float3(0, 1, 0)
let rotationMatrix = float4x4(simd_quatf(angle: radians, axis: yAxis))
当然,一旦有了旋转矩阵,您就可以使用 *
或 *=
运算符应用它:
someNode.simdTransform *= rotationMatrix
如果您发现自己经常做这样的事情,您可能需要在 float4x4
上编写扩展。
Tip: By the way, in Swift (or C++ or Metal shader language) you don't need the
simd_
prefix on most (but not all) SIMD types and global functions.
关于swift - 如何使用 Apple 的 SIMD 库围绕 Y 旋转 arkit 4x4 矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45463627/