[SOLVED] Where to put my AABB

I’ve got a hierarchy of objects under a top level entity.
I’ve made a bbox to fit:

var option = new pc.BoundingBox();
    option.copy(top_node.model.meshInstances[0].aabb);
    this.aabb.push([option, top_node]);

and it all works perfectly (correct size too) when I loop through the bboxes doing a ray test:

...
var sel = [];
	for(var i=0; i < bboxes.length; i++) {
		bbox = bboxes[i];
        if (bbox[1].enabled) {
		    entity = bbox[0].intersectsRay(this.ray, thispoint);
		    if (entity) {
    			sel.push(bbox[1]);
            }
        }
	}

(which is really great btw.)

However - if I translate the group above my top_nodes then my bbox does not move.
Ideally I should attach the bbox somehow under the top_node but its not an Entity.
I have tried adding it as a collision component but not working. I could remake the bboxes whenever I move (but lots of garbage).

What’s the best way to have my pc.BoundingBox() be transformed in the scene tree ?

If any of your objects rotate you will have to recalculate the AABBs anyway. I’d say the best plan is to have an AABB as a property of a parent entity and then update it each frame. No garbage then.

Something like (untested)

var ParentAABB = pc.createScript('parentaabb')

ParentAABB.prototype.postInitialize = function() {
    this.aabb = new pc.BoundingBox();
    var meshInstances = this.meshInstances = [];

     this.entity.findAll(function(child) {
        if(child.model) {
           child.model.meshInstances.forEach(function(instance) {
             meshInstances.push(instance);
           });
        }
        return false;
     });
}

ParentAABB.prototype.postUpdate = function() {
     this.aabb.center.copy(pc.Vec3.ZERO);
     this.aabb.halfExtents.copy(pc.Vec3.ZERO);
     this.meshInstances.forEach(function(instance) {
        this.aabb.add(instance.aabb);
     });
}

very helpful thankyou - the overhead of remaking the bboxes is not high. Excellent solution thanks.