sceneform-android icon indicating copy to clipboard operation
sceneform-android copied to clipboard

scene.hitTest() does not return closest hit

Open josias-milk opened this issue 3 years ago • 0 comments

The documentation of scene.hitTest() says that it returns "the node closest to the screen". However, the returned node is not always the closest one.

I think the problem is caused by a missing distance check in the raycast method of CollisionSystem.java (around line 48):

if (collisionShape.rayIntersection(ray, tempResult)) {
    TransformProvider transformProvider = collider.getTransformProvider();
    if(!onlySelectableNodes 
            || !(transformProvider instanceof Node)
            || ((Node) transformProvider).isSelectable()) {
        resultHit.set(tempResult);
        result = collider;
    }
}

In the original google sceneform library, this check looked like this:

if (collisionShape.rayIntersection(ray, tempResult)) {
  if (tempResult.getDistance() < resultHit.getDistance()) {
    resultHit.set(tempResult);
    result = collider;
  }
}

The check for the distance is missing in sceneform maintained. I know that it's possible to call scene.hitTestAll(ray).firstOrNull() as a workaround.

josias-milk avatar May 09 '22 06:05 josias-milk