Object.raycast: Also test distance from BoundBox
authorGermano Cavalcante <germano.costa@ig.com.br>
Sat, 15 Apr 2017 04:31:24 +0000 (01:31 -0300)
committerGermano Cavalcante <germano.costa@ig.com.br>
Sat, 15 Apr 2017 04:31:24 +0000 (01:31 -0300)
If `isect_ray_aabb_v3_simple` provides this information, why not take advantage of it?

source/blender/makesrna/intern/rna_object_api.c

index 3b2e57a06e58aa9b01b8f6b57e0360514176514b..42d1b78784f6dc0d0ef29246e144b8f8941c1403 100644 (file)
@@ -326,10 +326,19 @@ static void rna_Object_ray_cast(
                return;
        }
 
-       /* Test BoundBox */
+       /* Test BoundBox first (efficiency) */
        BoundBox *bb = BKE_object_boundbox_get(ob);
-       if (bb && !isect_ray_aabb_v3_simple(origin, direction, bb->vec[0], bb->vec[6], NULL, NULL)) {
-               goto finally;
+       if (bb) {
+               float distmin, distmax;
+               if (isect_ray_aabb_v3_simple(origin, direction, bb->vec[0], bb->vec[6], &distmin, &distmax)) {
+                       float dist = distmin >= 0 ? distmin : distmax;
+                       if (dist > distance) {
+                               goto finally;
+                       }
+               }
+               else {
+                       goto finally;
+               }
        }
 
        BVHTreeFromMesh treeData = {NULL};