Hit testing in SceneKit

To find a 3D node pointed to by the user in SceneKit, add a tap gesture recogniser in ViewDidLoad()


  let fingerTap = UITapGestureRecognizer(target: self, action: "handleTap:")
  self.view.addGestureRecognizer(fingerTap)

 

And here’s a simple handler:


func handleTap(sender: UIGestureRecognizer)
{
    let p = sender.locationInView(self.sceneView)

    let hitResults = self.sceneView.hitTest(p, options: nil)

    if let results = hitResults {
        if (results.count > 0) {
            let result: SCNHitTestResult = results[0] as! SCNHitTestResult
             println("Hit \(result)")
        }

     }
}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s