Rotate Entity Around Bounds Center NOT Pivot Point

Hi Im trying to rotate an object around its bounds center. my idea was as follows:

1-Calculate the bounds centre of my entity
2-Create an empty entity at that position
3-COnnect my entity to that new object as a child
4- Rotate the new parent pivot entity which will in turn rotate my child entity

This all sounds great but when I get to step 2 it all gets confusing as the bounding box centre always returns a 0,0,0 vector…which is the same as the local pivot position. My code to collect the bounds center is here:


Pickup.prototype.calculateBoundingBox = function () {

    // --- find all mesh instances
    let meshInstances = [];
    const renders = this.entity.findComponents('render');

    //Go through All renderers and combine mesh instance arrays into one meshinstances
    renders.forEach(render => {
        meshInstances = meshInstances.concat(render.meshInstances);
    });

     // --- calculate the total bounding box
    const bounding = new pc.BoundingBox();

    meshInstances.forEach((meshInstance, index) => {

        if (index === 0) {
            bounding.copy(meshInstance.aabb);
        } else {
            bounding.add(meshInstance.aabb);
        }
    });
    console.log("%c"+"Bounds center on PICKUP is "+bounding.center,'background: #222; color: #25FF2E');
    return bounding.center;
};

Maybe there is another (simpler) way to do this? Thanks

Hi @Grimmy,

Try sharing a sample project that reproduces the issue, from the post I shared the above code bounding.center wasn’t zero.

It may be something specific to your scene/setup. This is the post I refer to:

The bounding centre is zero if its a single mesh according to what Im seeing.

It may be correct then, if your code is indeed parsing the single mesh instance. Try stepping through it using the debugger.

Afterwards, try moving your entity and see if the bounding center changes.

Is the bounds.center a local or world position? (I presume its world)

The bounding box is a mathematical representation of an axis aligned box shape. So whatever you are feeding it that would be.

In this case you are consuming the mesh instance aabb which is in world coordinates, so bounding.center would be in world space.

OKay Thanks for the info. Good to know! I notice that the bounds box centre does actually become something other than 0,0,0 but only after the first frame. No idea why, but I can put a delay on it or something so I get a proper reading.

Most likely because the engine needs to calculate the AABB for each model and that may be happening later on the render loop.

Hi, sorry to bring this one back up but I’m attempting to do this again and there doesnt seem to be any way to find the center point of a bounding box. To me it seems that boundingcox.center is only returning the pivot point of the parent model. I have tried everything I can think of.

If I create a default sphere in root and calculate the bounding box then create a new entity box at the center of that bounding box it puts it here. What on earth is going wrong?
image

Here is the cde:

Pickup.prototype.create_rotation_center_entity = function () {
   
    this.bboxCalculation = this.calculateBoundingBox();
    var size = this.bboxCalculation.halfExtents.scale(2.0);
    var center = this.bboxCalculation.center; //the center of the bounding box of this entity

    console.log("%c"+"Bounds center on "+this.entity.name+" is "+center,'background: #222; color: #25FF2E');
    
    this.bounds_center_visual = new pc.Entity();
    this.bounds_center_visual.addComponent('render', {
        type: 'box',
        castShadows: false,
        receiveShadows: false
    });
    this.entity.addChild(this.bounds_center_visual);
   
    this.bounds_center_visual.setLocalPosition(center.x, center.y, center.z);
    this.bounds_center_visual.setLocalScale(size.x*2,size.y*2,size.z*2)

};

Pickup.prototype.calculateBoundingBox = function () {

    // --- find all mesh instances
    let meshInstances = [];
    const renders = this.entity.findComponents('render');

    //Go through All renderers and combine mesh instance arrays into one meshinstances
    renders.forEach(render => {
        meshInstances = meshInstances.concat(render.meshInstances);
    });

     // --- calculate the total bounding box
    const bounding = new pc.BoundingBox();

    meshInstances.forEach((meshInstance, index) => {

        if (index === 0) {
            bounding.copy(meshInstance.aabb);
        } else {
            bounding.add(meshInstance.aabb);
        }
    });

    return bounding;
};

It seems to be adding a relative offset from the cube to the parent sphere the same as the sphere has to the root (0,0,0) position.
How can I stop that? That’s just crazy?

Looks like I need to parent them to the root and not the entity before I position them.