iosswiftaugmented-realityhittestrealitykit

iOS ARView.hitTest limited to a max of 100 meters


I found that ARView.hitTest is limited to 100 meters distance. All objects located further than 100m, regardless of size, are not recognized.

Is there any way to unblock this?

let hitTest = arView.hitTest(point, query: .any, mask: .all)

Solution

  • You're right, a distance limitation of hitTest(_:query:mask:) instance method is 100 meters at the moment. But you can implement method raycast(from:to:) allowing you to shoot a ray from any scene point to any direction, even if a distance is 1000 meters.

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    
        let cameraPosition = arView.cameraTransform.translation
        
        let castHits = arView.scene.raycast(from: cameraPosition, to: [0, 0,-1000])
        
        guard let hitTest: CollisionCastHit = castHits.first else { return }
        
        print(hitTest.distance)
    }
    


    Update:

    Additionally, as @Sacha suggested, we can use instance method ray(through:) that determines the position and direction of a ray through the given point in the 2D space of the view. It returns optional tuple (origin: SIMD3<Float>, direction: SIMD3<Float>)? that suits for arguments of raycast(from:to:) method.