How to rotate object in a scene with pan gesture - SceneKit

荒凉一梦 提交于 2019-12-19 03:01:10

问题


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)

to rotate the cube


回答1:


The problem is solved

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

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

    geometryNode.pivot = SCNMatrix4Mult(changePivot, currentPivot)

    geometryNode.transform = SCNMatrix4Identity
}



回答2:


This solution works if your object original position isn't (0,0,0).

    if(gestureRecognize.state == UIGestureRecognizerState.Ended) {
       let currentPivot = geometryNode.pivot
       let currentPosition = geometryNode.position
       let changePivot = SCNMatrix4Invert(SCNMatrix4MakeRotation(geometryNode.rotation.w, geometryNode.rotation.x, geometryNode.rotation.y, geometryNode.rotation.z))

       geometryNode.pivot = SCNMatrix4Mult(changePivot, currentPivot)
       geometryNode.transform = SCNMatrix4Identity
       geometryNode.position = currentPosition
    }

But I don't understand why we assign the invert value of transform or rotation to changePivot. Is it correct, we are trying to rotate the pivot to our current rotation values and with setting the .transform property to the identity matrix it resets the node to the pivot value. We don't see any change, because the pivot already has the same values as our node right then. But why we use the invert values, normally you use this to rotate something back to its origin. Can somebody tell me, where i'm thinking wrong?



来源:https://stackoverflow.com/questions/35194914/how-to-rotate-object-in-a-scene-with-pan-gesture-scenekit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!