3

I'm currently building an AR app with Swift and would like to convert this type of thing: let arHitTestResults : [ARHitTestResult] = sceneView.hitTest(screenCentre, types: [.featurePoint]) (which is no longer available in iOS 14) to this: let arResults: [ARRaycastResult] = sceneView.raycastQuery(from: tapLocation, allowing: existingPln, alignment: alignment)

but systematically get this message: Cannot convert value of type 'ARRaycastQuery?' to specified type '[ARRaycastResult]'

What can I do?

Thanks for your answers!

@objc func handleTap(gestureRecognize: UITapGestureRecognizer) {

    let screenCentre: CGPoint = CGPoint(x: self.sceneView.bounds.midX, 
                                        y: self.sceneView.bounds.midY)
    let tapLocation: CGPoint = screenCentre
    let existingPln: ARRaycastQuery.Target = .existingPlaneGeometry
    let alignment: ARRaycastQuery.TargetAlignment = .vertical
    
    let arResults: [ARRaycastResult] = sceneView.raycastQuery(from: tapLocation, 
                                                          allowing: existingPln, 
                                                         alignment: alignment)
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
loicg
  • 59
  • 6

1 Answers1

3

You needn't convert a deprecated ARHitTestResult class into ARRaycastResult because these classes are not interchangeable. Instead use the following methodology when using raycasting in your scene:

@IBAction func tapped(_ sender: UITapGestureRecognizer) {

    let tapLocation: CGPoint = sender.location(in: arView)
    let estimatedPlane: ARRaycastQuery.Target = .estimatedPlane
    let alignment: ARRaycastQuery.TargetAlignment = .any

    let result = arView.raycast(from: tapLocation,
                            allowing: estimatedPlane,
                           alignment: alignment)

    guard let raycast: ARRaycastResult = result.first
    else { return }

    let anchor = AnchorEntity(world: raycast.worldTransform)
    anchor.addChild(model)
    arView.scene.anchors.append(anchor)
 
    print(raycast.worldTransform.columns.3)
}

If you need more examples of raycasting, look at this SO post.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220