swift - 如何在 RealityKit 中仅围绕一个轴旋转对象?

标签 swift rotation augmented-reality arkit realitykit

我正在尝试围绕其 z 轴旋转一个立方体,但我找不到方法。

RealityKit 有没有办法做到这一点?

最佳答案

在 RealityKit 中,至少有三种方法可以绕单轴旋转对象 .
在每个示例中,我们逆时针 (CCW) 旋转对象。

第一种方法:

let boxScene = try! Experience.loadBox()

boxScene.steelBox?.orientation = simd_quatf(angle: .pi/4,    /* 45 Degrees */
                                             axis: [0,0,1])  /* About Z axis */

第二种方法:
boxScene.steelBox?.transform = Transform(pitch: 0, 
                                           yaw: 0, 
                                          roll: .pi/4)      /* Around Z axis */
间距 , 偏航是以弧度表示的绕 X、Y 和 Z 轴的旋转。

第三种方法:
let a: Float = cos(.pi/4)
let b: Float = sin(.pi/4)

let matrix = float4x4([ a, b, 0, 0 ],        /* column 0 */
                      [-b, a, 0, 0 ],        /* column 1 */
                      [ 0, 0, 1, 0 ],        /* column 2 */
                      [ 0, 0, 0, 1 ])        /* column 3 */

boxAnchor.steelBox?.setTransformMatrix(matrix, relativeTo: nil)
旋转矩阵的视觉表示如下所示:
let a: Float = cos(.pi/4)
let b: Float = sin(.pi/4)

//  0  1  2  3
 ┌              ┐
 |  a -b  0  0  |
 |  b  a  0  0  |
 |  0  0  1  0  |
 |  0  0  0  1  |
 └              ┘
如果您想了解更多关于旋转矩阵的信息,请阅读 this邮政。

关于swift - 如何在 RealityKit 中仅围绕一个轴旋转对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59294602/

相关文章:

Swift 获取 Facebook 用户电子邮件不起作用

swift - 在 Xcode11 中导入 Swift 包

Android Activity 不会在手机上旋转(但在平板电脑上会)

c# - 如何从矢量或 ImageTargets 列表生成随机目标

ios - 如何在 PDFView 中 swift 适应来自 UIImage 的 PDF?

ios - 使用 Model 中的 Alert 类访问 UIAlert TextField 中的文本

rotation - 无法读取未定义的 : Three. js 错误的属性 'rotation'

rotation - 网站可以强制设备旋转锁定吗?

ios - 为什么 ARKit 应用程序在几天后停止工作?

swift - ARAnchor 和 AnchorEntity 有什么区别?