给定x,y,z坐标,你如何确定一个节点是否存在,如果存在,哪一个?

时间:2015-02-03 16:40:19

标签: scenekit

我有一个带有2个球体的.dae文件,名为" ball1"和" ball2"。

  • ball1 at x:0 y:0 z:0
  • ball2 at x:1 y:0 z:0

在代码中的某一点,给出了x,y,z坐标。给定的坐标可以与其中一个球相同但不一定。如果一个节点存在,那么如何检查给定坐标上存在哪个节点?

 let sceneBall1 = scnView.scene!.rootNode.childNodeWithName("ball1", recursively: true)
 let sceneBall2 = scnView.scene!.rootNode.childNodeWithName("ball2", recursively: true)

 var posball1:SCNVector3 = SCNVector3(x: 0, y: 0, z: 0)
 var posball2:SCNVector3 = SCNVector3(x: 1, y: 0, z: 0)

如果我想知道我点击的节点,我可以这样做:

 if result.node!.name!.hasPrefix("ball1") {
      println("You tapped ball1.")
 } else {
      println("You tapped ball2.")

但是我没有给出一个抽头结果节点,只有x,y,z。

2 个答案:

答案 0 :(得分:0)

mnuages 向我展示了方向。归结为:

 if ball1!.position.x == givenX && ball1!.position.y == givenY {
            println("ball1 is here.")
        }

答案 1 :(得分:0)

@objc func tappedNode(_ sender:UITapGestureRecognizer){

    let location = sender.location(in: sceneView)

    // When tapped on the object, call the object's method to react on it
    let sceneHitTestResult = sceneView.hitTest(location, options: nil)
    if !sceneHitTestResult.isEmpty {
        // We only have one content, so we know which node was hit.
        // If the scene contains multiple objects, you would need to check here if the right node was hit
        let lastNode = sceneHitTestResult.last?.node

        print(lastNode?.name)
    }

}