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.
推荐答案
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屋!