RealityKit – Proper Hit-Testing in ARView

Viewed 446

I have a code like this one:

@objc func handleTap(_ sender: UITapGestureRecognizer) {
    
    let tapLocation = sender.location(in: arView)
    
    let hitResult0 = scnView?.hitTest(tapLocation)

    if let hitResult = arView?.entity(at: tapLocation) {
         // ...
    }
}

hitResult gives a bad precision. It returns the same entity (WheelbarrowHandles) even if I tapped on a different smaller one. hitResult0 gives all object near to the tap location (to much).

ModelLoading:

self.theModel = try! Entity.load(named: "wheelborrow")
self.theModel?.generateCollisionShapes(recursive: true)

Is it bad collision shapes generated?


Updated

I tried to do the next instead of generation:

modelEntity.collision = CollisionComponent(shapes: [ShapeResource.generateConvex(from: modelEntity.model!.mesh)])

and it works.

1 Answers

Generated collision shapes:

generateCollisionShapes(recursive: true)

enter image description here

And with the next code:

modelEntity.collision = CollisionComponent(shapes: [ShapeResource.generateConvex(from: modelEntity.model!.mesh)])

enter image description here

Related