How to rotate an object in a scene using pan gestures - SceneKit

I am contacting to create a plot suite application for a Rubix Cube solution. I have my own dae file for the cube. this is my installation 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) 

when detecting rotation 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 does not save previous gestures. how to use "rotationvector" or

 SCNMatrix4MakeRotation(anglePan, -y, x, 0) 

to rotate the cube

+5
source share
2 answers

Problem resolved

 if(gestureRecognize.state == UIGestureRecognizerState.Ended) { let currentPivot = geometryNode.pivot let changePivot = SCNMatrix4Invert( geometryNode.transform) geometryNode.pivot = SCNMatrix4Mult(changePivot, currentPivot) geometryNode.transform = SCNMatrix4Identity } 
+6
source

This solution works if the initial position of the object is not (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 are assigning a conversion or rotation inversion value to changePivot. Is it right, we are trying to rotate the rotation axis to our current rotation values ​​and by setting the .transform property to a single matrix, which resets the node value to the rotation value. We do not see any changes, because now the rotation axis has the same values ​​as our node. But why do we use inverted values, usually you use this to turn something back to its beginning. Can someone tell me where I think wrong?

+5
source

Source: https://habr.com/ru/post/1242274/


All Articles