如何使用平移手势旋转场景中的对象 - SceneKit [英] How to rotate object in a scene with pan gesture - SceneKit

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

问题描述

我想用 Scene 工具包创建一个应用程序来解决 Rubix Cube.我有自己的多维数据集 dae 文件.这是我在 viewDidLoad 中的设置代码

I'm tying to create an app with Scene kit to solve Rubix Cube. I've my own dae file for the cube. this is my setup code in viewDidLoad

    let myscene = SCNScene(named: "Rubik1.scnassets/Rubiks_Cube.dae")
    scene.rootNode.addChildNode((myscene?.rootNode)!)

    // retrieve the SCNView
    let scnView = self.view as! SCNView

    // set the scene to the view
    scnView.scene = scene

    geometryNode = (scnView.scene?.rootNode.childNodeWithName("Cube",recursively: true))!

    let panRecognizer = UIPanGestureRecognizer(target: self, action: "panGesture:")
    scnView.addGestureRecognizer(panRecognizer)

在识别平移手势以旋转立方体时

upon recognising a pan gesture to rotate the cube

func panGesture(gestureRecognize: UIPanGestureRecognizer){

    let translation = gestureRecognize.translationInView(gestureRecognize.view!)

    let x = Float(translation.x)
    let y = Float(-translation.y)

    let anglePan = sqrt(pow(x,2)+pow(y,2))*(Float)(M_PI)/180.0

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

    geometryNode.rotation = rotationVector

    //geometryNode.transform = SCNMatrix4MakeRotation(anglePan, -y, x, 0)

    if(gestureRecognize.state == UIGestureRecognizerState.Ended) {
        //
    }
}

上面的代码不保留以前的平移手势.如何使用旋转向量"或

above code doesn't preserve the previous pan gestures. how do I use "rotationvector" or

SCNMatrix4MakeRotation(anglePan, -y, x, 0)

旋转立方体

推荐答案

问题解决了

if(gestureRecognize.state == UIGestureRecognizerState.Ended) {

    let currentPivot = geometryNode.pivot
    let changePivot = SCNMatrix4Invert( geometryNode.transform)

    geometryNode.pivot = SCNMatrix4Mult(changePivot, currentPivot)

    geometryNode.transform = SCNMatrix4Identity
}

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

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