Search code examples
iosswiftcollision-detectionscenekithittest

hitTest(_:options:) for SCNNodes without physicsBody swift


I'm trying to determine how to do a 'hitTest' without actually creating a physicsBody for my SCNNode.

What alternative can I use for a hit test method? (that includes nodes without a physics body). I'm trying to select the first node that the screen touch is on

Problem, the 'hit test' requires that the node have a physicsBody This would work if I had a physicsBody, but I need to do stuff with my SCNNode before I add the physicsBody.

My 'environmentContainer' DOES have the first SCNNode in it; however, the 'results.first' returned the first node with a physicsBody, I need the first node (regardless if it has a physicsBody or not)

case .began:
    let results = sceneView.hitTest(firstTouch!, options: [SCNHitTestOption.firstFoundOnly: true])
    if results.first != nil {
        if environmentContainer.childNodes.contains((results.first?.node)!)
        {
            firstDragTapOnSelectedNode = true
            print("firstDragTapOnSelectedNode = true")
        }
        else {
            // always returns false because 'results.first' is picking up the wrong node which is in a different SCNNode container
            print("firstDragTapOnSelectedNode = false")
        }
    }
case .ended:
    firstDragTapOnSelectedNode = false
default: break
}

Note: firstDragTapOnSelectedNode is initialized to false


Solution

  • The SceneKit hit test is testing against geometries by default. You can set SCNHitTestOption.boundingBoxOnly to true to check against the bounding box of the node instead. Hit testing should have nothing to do with the physicsBody just with geometries and bounding boxes.