如何旋转 SCNBox [英] How to rotate an SCNBox

查看:34
本文介绍了如何旋转 SCNBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to rotate an SCNBox I created using swipe gestures. For example, when I swipe right the box should rotate 90degs in the Y-axis and -90degs when I swipe left. To achieve this I have been using the node's SCNAction.rotateByX method to perform the rotation animation. Now the problem I'm having is when rotating along either the X-axis or Z-axis after a rotation in the Y-axis and vice-versa is that the positions of the axes change.

我注意到的是,在 X、Y、Z 轴上执行的任何旋转都会改变其他轴指向的方向.

What I have notice is that any rotation perform on either of the X,Y,Z axes changes the direction in which the other axes point.

示例:默认位置

然后在Z轴旋转后:

当然这会带来一个问题,因为现在当我向左或向右滑动时,我不再得到想要的效果,因为X轴Y轴 现在已经交换了位置.我想知道的是为什么会发生这种情况?无论如何,是否可以在不影响其他轴的情况下执行旋转动画?

Of course this pose a problem because now when I swipe left or right I no longer get the desire effect because the X-axis and Y-axis have now swapped positions. What I would like to know is why does this happen? and is there anyway to perform the rotation animation without it affecting the other axes?

对于我对这个主题缺乏了解,我深表歉意,因为这是我第一次接触 3d 图形.

I apologize for my lack of understanding on this subject as this is my first go at 3d graphics.

解决方案:

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 轴的任何更改可能更安全.

I haven't ran into any problems yet but it may be safer to use a completion handler to ensure any changes to X,Y,Z axes are done before repositioning them.

推荐答案

我遇到了同样的问题,这是我用来提供所需行为的内容:

I had the same issue, here's what I use to give the desired behavior:

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
    }
}

这篇关于如何旋转 SCNBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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