1

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)
Sasha
  • 764
  • 1
  • 13
  • 21

1 Answers1

0

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.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • thanks @Andy, will the vector [0, 0,-10000] be orthogonal to the screen in this case? Actually what I'm trying to do is to implement the ability to touch the object without distance restrictions – Sasha Apr 11 '21 at 23:51
  • 1
    Found the way to do it using: let ray = arView.ray(through: tapPoint), let results = arView.scene.raycast(origin: ray.origin, direction: ray.direction, length: 1000, query: .nearest) – Sasha Apr 12 '21 at 00:48