SceneKit平移带有世界坐标的局部子节点

时间:2018-11-17 16:05:45

标签: ios scenekit uipangesturerecognizer

我有两个节点

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

都正确添加到场景中,平移可旋转时,两个节点都正确旋转。 在viewController中使用以下代码平移bluePeg时出现的问题

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;
                    }

                }
            }
        }

为进一步说明,可旋转节点与子节点一起绕Y轴旋转。为了平移bluePeg,我尝试从局部坐标中删除旋转量,然后使用世界坐标进行平移,因为可旋转的节点位置始终为零矢量。

我在这里做错了什么? 请帮助

1 个答案:

答案 0 :(得分:0)

我休息一会儿就睡了:)。

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))

                    }

                }
            }
        }