如何使用 Apple 的 SIMD 库围绕 Y 旋转 arkit 4x4 矩阵? [英] How do I rotate an arkit 4x4 matrix around Y using Apple's SIMD library?
问题描述
我正在尝试基于 ARKit 演示实现一些代码,其中有人使用此辅助函数来放置航点
I am trying to implement some code based on an ARKit demo where someone used this helper function to place a waypoint
let rotationMatrix = MatrixHelper.rotateAboutY(
degrees: bearing * -1
)
如何使用 SIMD 库而不使用 GLKit 来实现 .rotateAboutY 函数?为了方便起见,我可以从原点开始.
How can I implement the .rotateAboutY function using the SIMD library and not using GLKit? To make it easier, I could start from the origin point.
我对矩阵数学不太熟悉,所以更基本的解释会有所帮助.
I'm not too handy with the matrix math so a more basic explanation would be helpful.
推荐答案
可能是利用内置 SIMD 库进行此类操作的最佳方式(至少,最好"的意思是自己做最少的数学运算")是用四元数表示旋转并在需要时转换为矩阵.
Probably the best way to leverage the built-in SIMD library for such operations (at least, with "best" meaning "do the least math yourself") is to express rotations with quaternions and convert to matrices when needed.
- 构造一个
simd_quatf
表示要应用的轴角旋转. - 将该四元数转换为 4x4 矩阵.
- Construct a
simd_quatf
representing the axis-angle rotation you want to apply. - Convert that quaternion to a 4x4 matrix.
您可以通过单线电话做到这一点:
You can do that with a one-line call :
let rotationMatrix = float4x4(simd_quatf(angle: radians, axis: axis))
扩展到您的rotateAboutY"案例:
Expanding to your "rotateAboutY" case:
let radians = degreesToRadians(-bearing) // degrees * .pi / 180
let yAxis = float3(0, 1, 0)
let rotationMatrix = float4x4(simd_quatf(angle: radians, axis: yAxis))
当然,一旦有了旋转矩阵,就可以使用 *
或 *=
运算符来应用它:
Of course, once you have a rotation matrix, you can apply it using the *
or *=
operator:
someNode.simdTransform *= rotationMatrix
如果你发现自己经常做这样的事情,你可能想在 float4x4
上写一个扩展.
If you find yourself doing something like this often, you might want to write an extension on float4x4
.
提示:顺便说一下,在 Swift(或 C++ 或 Metal 着色器语言)中,大多数(但不是全部)SIMD 类型和全局函数不需要 simd_
前缀.
这篇关于如何使用 Apple 的 SIMD 库围绕 Y 旋转 arkit 4x4 矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!