3

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.

Sam
  • 145
  • 5
  • 1
    To find to how hit-testing and ray-casting works, look at this post – https://stackoverflow.com/questions/56466773/what-is-the-real-benefit-of-using-raycast-in-arkit-and-realitykit/56467767#56467767, at this post – https://stackoverflow.com/questions/56736645/hittest-prints-ar-entity-name-even-when-i-am-not-tapping-on-it/59866900#59866900 and at this one – https://stackoverflow.com/questions/60294367/how-to-use-raycast-methods-in-realitykit – Andy Jazz Jul 03 '20 at 12:17
  • 1
    Tnx, @AndyFedoroff . I'll try raycasting and writeback. – Sam Jul 07 '20 at 14:49

1 Answers1

2

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

Sam
  • 145
  • 5