SceneKit - 围绕 X 轴和 Z 轴旋转对象 [英] SceneKit - Rotate object around X and Z axis
问题描述
我将 ARKit 与 SceneKit 结合使用.当用户按下按钮时,我会创建一个锚点,并在对应的 SCNNode 中添加一个 3D 对象(从项目中的 .scn 文件加载).
I’m using ARKit with SceneKit. When user presses a button I create an anchor and to the SCNNode corresponding to it I add a 3D object (loaded from a .scn file in the project).
3D 对象面向相机放置,与相机的方向相同.我想让它看起来像物体放在一个平面上,如果是这样的话,它不会倾斜.所以,如果我做对了,我需要应用一个旋转变换,使其绕 X 轴和 Z 轴的旋转变为 0.
The 3D object is placed facing the camera, with the same orientation the camera has. I would like to make it look like the object is laying on a plane surface and not inclined if it is that way. So, if I got it right, I’d need to apply a rotation transformation so that it’s rotation around the X and Z axis become 0.
我对此的尝试是:取节点的 x 和 z eulerAngles,反转它们,并围绕每个轴旋转该量
My attempt at this is: take the node’s x and z eulerAngles, invert them, and rotate that amount around each axis
let rotationZ = rotationMatrixAroundZ(radians: -node.eulerAngles.z)
let rotationX = rotationMatrixAroundX(radians: -node.eulerAngles.x)
let rotationTransform = simd_mul(rotationTransformX, rotationTransformZ)
node.transform = SCNMatrix4(simd_mul(simd_float4x4(node.transform), rotationTransform))
这在大多数情况下都可以正常工作,但在某些情况下,对象以完全奇怪的方式旋转.我应该设置旋转角度除了当前欧拉角的倒数之外的其他任何东西?直接将角度设置为 0 根本不起作用.
This works all right for most cases, but in some the object is rotated in completely strange ways. Should I be setting the rotation angle to anything else than just the inverse of the current Euler Angle? Setting the angles to 0 directly did not work at all.
推荐答案
我遇到了这个问题并发现我遇到了 万向节锁.解决方案是围绕一个轴旋转节点,将其作为另一个 SCNNode()
的父节点,然后围绕另一个轴旋转父节点.希望有所帮助.
I've come across this and figured out I was running into gimbal lock. The solution was to rotate the node around one axis, parent it to another SCNNode()
, then rotate the parent around the other axis. Hope that helps.
这篇关于SceneKit - 围绕 X 轴和 Z 轴旋转对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!