我正在尝试围绕其 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/