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
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.
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?
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?