如何解决RealityKit – transform.rotate() 使对象的比例更大
我正在尝试旋转 RealityKit 入门代码中给出的钢盒,我使用此代码
steelBox.transform.rotation += simd_quatf(angle: radians,axis: SIMD3<Float>(0,1,0))
尝试旋转对象。
然而,当物体旋转时,它也会在一维上变大。
这是为什么?
解决方法
如果它是您正在累积的四元数旋转,您可能想要使用:
steelBox.transform.rotation =
simd_mul(steelBox.transform.rotation,simd_quatf(angle: radians,axis: SIMD3<Float>(0,1,0)) )
或
steelBox.transform.rotation =
simd_mul(simd_quatf(angle: radians,0)),steelBox.transform.rotation )
取决于您是要在初始方向之前还是之后“添加”旋转(我不确定是哪个,因为这取决于 simd_mul 是否执行 parent * child
或 child * parent
-苹果文档没有说明哪个?)。
四元数遵循与变换矩阵相同的规则。因此,如果您在谈论“添加”(累积) 轮换,您实际上是指“乘以”。在四元数上使用加法运算符将传递缩放操作。
,使用复合乘法运算符 *=
而不是复合加法 +=
。
steelBox.transform.rotation *= simd_quatf(angle: .pi / 4,// 45 degrees
axis: SIMD3<Float>(0,0))
让我们看看当您使用复合加法和乘法时,您的旋转在 4x4 矩阵中的样子。
您应该打印矩阵的值:
print((boxScene.steelBox?.transform.matrix)!)
身份矩阵。
┌ ┐
| 1 0 0 0 |
| 0 1 0 0 |
| 0 0 1 0 |
| 0 0 0 1 |
└ ┘
Compound Addition
后的矩阵存储结果(绕 Y 轴逆时针旋转 45 度):
┌ ┐
| 1.4 0 2.9 0 |
| 0 2 0 0 |
| -2.9 0 1.4 0 |
| 0 0 0 1 |
└ ┘
Compound Multiplication
后的矩阵存储结果(绕 Y 轴逆时针旋转 45 度):
┌ ┐
| 1.4 0 1.4 0 |
| 0 2 0 0 |
| -1.4 0 1.4 0 |
| 0 0 0 1 |
└ ┘
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。