在SceneKit iOS中使用平移手势旋转节点 [英] Rotate a node using pan gesture in SceneKit iOS

查看:428
本文介绍了在SceneKit iOS中使用平移手势旋转节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码通过平移手势旋转节点.我喜欢只在y轴上旋转节点.

I am using the below code to rotate a node using the pan gesture. I like to rotate my node only in the y-axis.

let translation = gestureRecognize.translation(in: gestureRecognize.view!)

let x = Float(translation.x)
let y = Float(-translation.y)
let anglePan = (sqrt(pow(x,2)+pow(y,2)))*(Float)(Double.pi)/180.0

var rotationVector = SCNVector4()
rotationVector.x = 0.0
rotationVector.y = x
rotationVector.z = 0.0
rotationVector.w = anglePan


node.rotation = rotationVector

if(gestureRecognize.state == UIGestureRecognizerState.ended) {
    let currentPivot = node.pivot
    let changePivot = SCNMatrix4Invert( node.transform)

    node.pivot = SCNMatrix4Mult(changePivot, currentPivot)
    node.transform = SCNMatrix4Identity

}

这适用于Euler设置为(x:0,y:0,z:0)的节点.但是我的节点有Euler(x:-90,y:0,z:0).对于我上面的欧拉值,代码将对象旋转到错误的角度.如何使用不同的Euler值旋转节点?

This works for nodes with Euler set to (x: 0, y: 0, z: 0). But my node has Euler (x: -90, y: 0, z: 0). For my Euler values above code rotates the object in the wrong angle. How can I rotate a node with my/different Euler values?

推荐答案

我认为您可能会使这里要做的事情变得过于复杂.

I think you might be overcomplicating what you need to do here.

在我的示例中,我创建了一个具有SCNBox几何形状的SCNNode,并按照您的示例将其设置为Euler Angles:(x:-90,y:0,z:0).

In my example, I have created an SCNNode with an SCNBox Geometry and set it's Euler Angles as per your example: (x: -90, y: 0, z: 0).

您需要做的第一件事是创建一个变量来存储YAxis周围的rotationAngle:

The 1st thing you need to do, is create a variable to store the rotationAngle around the YAxis:

var currentAngleY: Float = 0.0

然后尝试使用此功能以使节点绕YAxis旋转(顺便说一句,效果很好):

Then try this function in order to rotate your node around the YAxis (which works fine by the way):

 /// Rotates An Object On It's YAxis
 ///
 /// - Parameter gesture: UIPanGestureRecognizer
 @objc func rotateObject(_ gesture: UIPanGestureRecognizer) {

        guard let nodeToRotate = currentNode else { return }

        let translation = gesture.translation(in: gesture.view!)
        var newAngleY = (Float)(translation.x)*(Float)(Double.pi)/180.0
        newAngleY += currentAngleY

        nodeToRotate.eulerAngles.y = newAngleY

        if(gesture.state == .ended) { currentAngleY = newAngleY }

        print(nodeToRotate.eulerAngles)
}

希望有帮助...

这篇关于在SceneKit iOS中使用平移手势旋转节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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