如何在 RealityKit 中仅围绕一个轴旋转对象? [英] How do I rotate an object around only one axis in RealityKit?

查看:28
本文介绍了如何在 RealityKit 中仅围绕一个轴旋转对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to rotate a cube around its z-axis but I can't find how.

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

Is there a way in RealityKit to do this?

推荐答案

在 RealityKit 中,至少有三种方法可以绕单轴旋转对象.

在每个示例中,我们逆时针 (CCW) 旋转一个对象.

In each example we rotate an object counterclockwise (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 */

pitchyawroll 是以弧度表示的绕 X、Y 和 Z 轴的旋转.

pitch, yaw and roll are rotations about X, Y and Z axis expressed in radians.


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)

旋转矩阵的视觉表示如下:

Visual representation of rotation matrix looks like this:

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  |
 └              ┘

如果您想了解更多关于旋转矩阵的信息,请阅读 这篇 帖子.

If you wanna know more about Rotation Matrices, read this post.

这篇关于如何在 RealityKit 中仅围绕一个轴旋转对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆