i'm having set of 3d objects {obj1,obj2,.....objn}, and a 3D bounding box B . Right now to find the set of objects inside or intersecting the bounding box B i'm using below code,which computes in O(n)
for(int i = 0; i < n; i++){
obj = objectsArray[i];
objBox = obj.BoundingBox;
if ((B.Max.Z > objBox.Max.Z || B.Max.Z > objBox.Min.Z) && (B.Min.Z < objBox.Max.Z || B.Min.Z < objBox.Min.Z) && (B.Max.X > objBox.Max.X || B.Max.X > objBox.Min.X) && (B.Min.X < objBox.Max.X || B.Min.X < objBox.Min.X) && (B.Max.Y > objBox.Max.Y || B.Max.Y > objBox.Min.Y) && (B.Min.Y < objBox.Max.Y || B.Min.Y < objBox.Min.Y)) {
// obj is inside or overlapping box B
ObjectsInsideB.add(obj)
} else{
// obj out of box B
}
}
// objBox.Max bounding box vertex at maximum end
// objBo.min bounding box vertex at minimum end
I'm looking for efficient way to compute this , to reduce O(n) search, is there any way ? As the bounding box B changes per frame, it is very slow for 100000 objects.