简体   繁体   中英

SceneKit Panning child node local with world coordinates

I have two nodes

 var rotatable : SCNNode!
 var bluePeg : SCNNode!
...
rootnode.addchildNode(rotatable)
rotatable.addChildNode(bluePeg)

both add to the scene correctly, when panning the rotatable both nodes rotate correctly. The problem when panning the bluePeg with the following code in the viewController

if gestureRecognize.state == .changed {
            if rotating {
                angle = rotatePanel(xTranslation: Float(gestureRecognize.translation(in: gestureRecognize.view).x))
            }
            else {
                let touchLocation = gestureRecognize.location(in: sceneView)//sceneView
                let hitResults = sceneView.hitTest(touchLocation, options: [.searchMode: 1, .ignoreHiddenNodes: false])

                for result in hitResults {
                    if result.node.name == "xzPlane" {
                        let newRotation = SCNVector3Make(0,-1.0 * (self.scene?.rotatable.eulerAngles.y)!, 0)
                        selectedNode.eulerAngles = newRotation //selectedNode is bluePeg Node
                        selectedNode.position = scene!.rotatable.convertVector((selectedNode.position), to: nil)//rotatable is the parent of selectedNode
                        os_log("rotatble Euler: %f, %f, %f",((scene?.rotatable.eulerAngles.x)!), ((scene?.rotatable.eulerAngles.y)!),((scene?.rotatable.eulerAngles.z)!))
                        os_log("selectedNode Euler: %f, %f, %f",(selectedNode.eulerAngles.x), (selectedNode.eulerAngles.y),(selectedNode.eulerAngles.z))
                        selectedNode.position.x = result.worldCoordinates.x
                        selectedNode.position.z = result.worldCoordinates.z
                        //selectedNode.position = selectedNode.convertPosition(selectedNode.position, to: nil)
                        return;
                    }

                }
            }
        }

To clarify further, rotatable node rotates around the Y-axis with the children. To pan the bluePeg I tried removing the amount of rotation from the local coordinates and then pan using the world coordinates since the rotatable node position is always the zero vector.

What am I doing wrong here? Please help

I got it figured out after I took a break and slept :).

if gestureRecognize.state == .changed {
            if rotating {
                angle = rotatePanel(xTranslation: Float(gestureRecognize.translation(in: gestureRecognize.view).x))
            }
            else {
                let touchLocation = gestureRecognize.location(in: sceneView)//sceneView
                let hitResults = sceneView.hitTest(touchLocation, options: [.searchMode: 1, .ignoreHiddenNodes: false])

                for result in hitResults {
                    if result.node.name == "xzPlane" {
                       selectedNode.position = result.node.convertVector(result.worldCoordinates, to: scene?.rotatable) //rotatble is the parent of bluePeg
                        os_log("rotatble Euler: %f, %f, %f",((scene?.rotatable.eulerAngles.x)!), ((scene?.rotatable.eulerAngles.y)!),((scene?.rotatable.eulerAngles.z)!))
                        os_log("selectedNode Euler: %f, %f, %f",(selectedNode.eulerAngles.x), (selectedNode.eulerAngles.y),(selectedNode.eulerAngles.z))

                    }

                }
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM