我正在尝试绕z轴旋转一个立方体,但是我找不到如何实现。
在RealityKit中是否有这样的方法?
我正在尝试绕z轴旋转一个立方体,但是我找不到如何实现。
在RealityKit中是否有这样的方法?
在 RealityKit 中,至少有三种方法可以围绕单个轴旋转对象。
在每个示例中,我们逆时针旋转一个对象(CCW)。
使用具有角度(以弧度表示)和轴参数的simd_quatf
初始化器:
let scene = try! Experience.loadBox()
scene.steelBox?.orientation = simd_quatf(angle: .pi/4, /* 45 Degrees */
axis: [0,0,1]) /* About Z axis */
使用Transform的pitch、yaw和roll,它们是关于X、Y和Z轴的旋转,以弧度表示。
scene.steelBox?.transform = Transform(pitch: 0,
yaw: 0,
roll: .pi/4) /* Around Z axis */
float4x4
初始化器,水平表示每列有4个插槽: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 */
scene.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 |
└ ┘
playAnimation(_:transitionDuration:startsPaused:)
实例方法来实现。此外,阅读这篇有用的文章:https://medium.com/twinkl-educational-publishers/how-to-animate-ar-objects-with-swiftui-and-realitykit-b14730c4fad9 - Andy Jazz对于也在寻找这个的人,你需要使用transform和rotation。这需要一个simd_quatf,其中你提供了角度和轴。
在我的情况下,我必须使用以下内容:
"object".transform.rotation = simd_quatf(angle: GLKMathDegreesToRadians(90), axis: SIMD3(x: 0, y: 0, z: 1))