Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'hitTest()' was Depecrated in iOS 14.0

I'm new to this and currently building an AR-related application, on the old version I stated this

let results = self.hitTest(screenPosition, types: [.featurePoint])

and now I have a problem where the hitTest is deprecated in iOS 14.0

hitTest(_:types:)' was deprecated in iOS 14.0: Use [ARSCNView raycastQueryFromPoint:allowingTarget:alignment]

please advise me on how to fix it, thank you :)

like image 440
Zaky Putra Avatar asked Oct 08 '20 07:10

Zaky Putra


People also ask

What determines the behavior of a hit Test?

The behavior of a hit test depends on which types you specify and the order you specify them in. For details, see ARHitTestResult and the various ARHitTestResult.ResultType options.

What is the use of hit Test in ARKit?

If you use ARKit with a SceneKit or SpriteKit view, the ARSCNView hitTest(_:types:) or ARSKView hitTest(_:types:) method lets you specify a search point in view coordinates. The behavior of a hit test depends on which types you specify and the order you specify them in.

What is arscnview hittest () method?

If you use ARKit with a SceneKit or SpriteKit view, the ARSCNView hitTest (_:types:) or ARSKView hitTest (_:types:) method lets you specify a search point in view coordinates. The behavior of a hit test depends on which types you specify and the order you specify them in.

What is HitHit testing in augmented reality?

Hit testing searches for real-world objects or surfaces detected through the AR session's processing of the camera image. A 2D point in the image coordinates can refer to any point along a 3D line that starts at the device camera and extends in a direction determined by the device orientation and camera projection.


2 Answers

You can assign a node with a name and then use hitTest(point:, options:[SCNHitTestOption : Any]?) in touchesBegan. Below is the code I used:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        let touchLocation = touch.location(in: sceneView)
        let results = sceneView.hitTest(touchLocation, options: [SCNHitTestOption.searchMode : 1])
        
        for result in results.filter({$0.node.name != nil}) {
            if result.node.name == "planeNode" {
                print("touched the planeNode")
            }
        }
    }
}
like image 118
Farhad Bagherzadeh Avatar answered Sep 23 '22 11:09

Farhad Bagherzadeh


Yes, use raycastQuery(from:allowing:alignment:)

as suggested by Xcode in this way:

...
let location = gesture.location(in: sceneView)
guard let query = sceneView.raycastQuery(from: location, allowing: .existingPlaneInfinite, alignment: .any) else {
   return
}
        
let results = sceneView.session.raycast(query)
guard let hitTestResult = results.first else {
   print("No surface found")
   return
}
...
like image 32
Peter Karena Avatar answered Sep 24 '22 11:09

Peter Karena