在 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 */
pitch、yaw 和 roll 是围绕 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 帖子。