如何旋转SCNBox

我正在尝试旋转我使用swipe gestures创建的SCNBox 。 例如,当我向右滑动时,框应该在Y-axis旋转90度,在向左滑动时旋转-90度。 为了实现这一点,我一直在使用节点的SCNAction.rotateByX方法来执行旋转动画。 现在我遇到的问题是在X-axis旋转后沿X-axisZ-axis旋转,反之亦然是轴的位置发生变化。

我注意到,在X,Y,Z轴上的任何一个旋转都会改变其他轴指向的方向。

示例:默认位置

在此处输入图像描述

然后在Z-axis旋转之后:

在此处输入图像描述

当然这会产生问题,因为现在当我swipe left or right时,我不再获得所需的效果,因为X-axisY-axis现在已经交换了位置。 我想知道的是为什么会发生这种情况? 无论如何都要执行旋转动画而不影响其他轴?

我为这个主题缺乏理解而道歉,因为这是我第一次使用3D图形。

解:

 func swipeRight(recognizer: UITapGestureRecognizer) { // rotation animation let action = SCNAction.rotateByX(0, y: CGFloat(GLKMathDegreesToRadians(90)), z: 0, duration: 0.5) boxNode.runAction(action) //repositoning of the x,y,z axes after the rotation has been applied let currentPivot = boxNode.pivot let changePivot = SCNMatrix4Invert(boxNode.transform) boxNode.pivot = SCNMatrix4Mult(changePivot, currentPivot) boxNode.transform = SCNMatrix4Identity } 

我还没有遇到任何问题,但使用完成处理程序来确保在重新定位之前完成对X,Y,Z轴的任何更改可能更安全。

我有同样的问题,这是我用来给出所需行为的东西:

 func panGesture(sender: UIPanGestureRecognizer) { let translation = sender.translationInView(sender.view!) let pan_x = Float(translation.x) let pan_y = Float(-translation.y) let anglePan = sqrt(pow(pan_x,2)+pow(pan_y,2))*(Float)(M_PI)/180.0 var rotVector = SCNVector4() rotVector.x = -pan_y rotVector.y = pan_x rotVector.z = 0 rotVector.w = anglePan // apply to your model container node boxNode.rotation = rotVector if(sender.state == UIGestureRecognizerState.Ended) { let currentPivot = boxNode.pivot let changePivot = SCNMatrix4Invert(boxNode.transform) boxNode.pivot = SCNMatrix4Mult(changePivot, currentPivot) boxNode.transform = SCNMatrix4Identity } }