I want to get the BoundingBox of an object, but if the Object is scaled smaller than 1 the BoundingBox halfextends won’t go smaller than 0.5.
Here is a test scene with a default cube:
https://playcanvas.com/project/965368/overview/boundingboxtest
I want to get the BoundingBox of an object, but if the Object is scaled smaller than 1 the BoundingBox halfextends won’t go smaller than 0.5.
Here is a test scene with a default cube:
https://playcanvas.com/project/965368/overview/boundingboxtest
Hmm, not sure if this is deliberate where any BoundingBox for a mesh instance can’t be smaller than 0.5.
@mvaligursky may know more as I can’t find reference to this in the engine code base
Wait, I know what’s wrong. The default size of the bounding box is 0.5 in half extents so this code is starting with a bounding box that is already 0.5 big:
et aabb = new pc.BoundingBox(this.entity.getPosition().clone());
this.entity.render.meshInstances.forEach(function (el) {
aabb.add(el.aabb);
});
And therefore, it is never going to be smaller than 0.5 as you are adding a smaller bounding box to it.
Here’s the fixed code https://playcanvas.com/editor/scene/1490614
DrawBoundingBox.prototype.update = function (dt) {
let aabb = new pc.BoundingBox(this.entity.getPosition().clone(), pc.Vec3.ZERO.clone());
this.entity.render.meshInstances.forEach(function (el) {
aabb.add(el.aabb);
});
this.app.drawWireSphere(aabb.center, 0.01, pc.Color.MAGENTA, 20, false);
this.app.drawLine(aabb.center.clone().add(new pc.Vec3(aabb.halfExtents.x, aabb.halfExtents.y, aabb.halfExtents.z)), aabb.center.clone().add(new pc.Vec3(aabb.halfExtents.x, aabb.halfExtents.y, -aabb.halfExtents.z)), pc.Color.MAGENTA, false);
this.app.drawLine(aabb.center.clone().add(new pc.Vec3(aabb.halfExtents.x, aabb.halfExtents.y, aabb.halfExtents.z)), aabb.center.clone().add(new pc.Vec3(-aabb.halfExtents.x, aabb.halfExtents.y, aabb.halfExtents.z)), pc.Color.MAGENTA, false);
this.app.drawLine(aabb.center.clone().add(new pc.Vec3(-aabb.halfExtents.x, aabb.halfExtents.y, aabb.halfExtents.z)), aabb.center.clone().add(new pc.Vec3(-aabb.halfExtents.x, aabb.halfExtents.y, -aabb.halfExtents.z)), pc.Color.MAGENTA, false);
this.app.drawLine(aabb.center.clone().add(new pc.Vec3(aabb.halfExtents.x, aabb.halfExtents.y, -aabb.halfExtents.z)), aabb.center.clone().add(new pc.Vec3(-aabb.halfExtents.x, aabb.halfExtents.y, -aabb.halfExtents.z)), pc.Color.MAGENTA, false);
this.app.drawLine(aabb.center.clone().add(new pc.Vec3(aabb.halfExtents.x, -aabb.halfExtents.y, aabb.halfExtents.z)), aabb.center.clone().add(new pc.Vec3(aabb.halfExtents.x, -aabb.halfExtents.y, -aabb.halfExtents.z)), pc.Color.MAGENTA, false);
this.app.drawLine(aabb.center.clone().add(new pc.Vec3(aabb.halfExtents.x, -aabb.halfExtents.y, aabb.halfExtents.z)), aabb.center.clone().add(new pc.Vec3(-aabb.halfExtents.x, -aabb.halfExtents.y, aabb.halfExtents.z)), pc.Color.MAGENTA, false);
this.app.drawLine(aabb.center.clone().add(new pc.Vec3(-aabb.halfExtents.x, -aabb.halfExtents.y, aabb.halfExtents.z)), aabb.center.clone().add(new pc.Vec3(-aabb.halfExtents.x, -aabb.halfExtents.y, -aabb.halfExtents.z)), pc.Color.MAGENTA, false);
this.app.drawLine(aabb.center.clone().add(new pc.Vec3(aabb.halfExtents.x, -aabb.halfExtents.y, -aabb.halfExtents.z)), aabb.center.clone().add(new pc.Vec3(-aabb.halfExtents.x, -aabb.halfExtents.y, -aabb.halfExtents.z)), pc.Color.MAGENTA, false);
this.app.drawLine(aabb.center.clone().add(new pc.Vec3(aabb.halfExtents.x, aabb.halfExtents.y, aabb.halfExtents.z)), aabb.center.clone().add(new pc.Vec3(aabb.halfExtents.x, -aabb.halfExtents.y, aabb.halfExtents.z)), pc.Color.MAGENTA, false);
this.app.drawLine(aabb.center.clone().add(new pc.Vec3(aabb.halfExtents.x, aabb.halfExtents.y, -aabb.halfExtents.z)), aabb.center.clone().add(new pc.Vec3(aabb.halfExtents.x, -aabb.halfExtents.y, -aabb.halfExtents.z)), pc.Color.MAGENTA, false);
this.app.drawLine(aabb.center.clone().add(new pc.Vec3(-aabb.halfExtents.x, aabb.halfExtents.y, aabb.halfExtents.z)), aabb.center.clone().add(new pc.Vec3(-aabb.halfExtents.x, -aabb.halfExtents.y, aabb.halfExtents.z)), pc.Color.MAGENTA, false);
this.app.drawLine(aabb.center.clone().add(new pc.Vec3(-aabb.halfExtents.x, aabb.halfExtents.y, -aabb.halfExtents.z)), aabb.center.clone().add(new pc.Vec3(-aabb.halfExtents.x, -aabb.halfExtents.y, -aabb.halfExtents.z)), pc.Color.MAGENTA, false);
};
Welp that did the trick. Sometimes you just have to read the docs more carefully
I do have another question though. I also have a box with an offset pivot to the bottom left. How would i go about getting the correct bounding box for it?
I did try adjusting the center of the box based on the sacling factor and this works quite well if the box is not rotated.
If it is rotated this center point won’t be correct though.
Is there a simple way of doing this? Or do i have to correctly calculate the center point of the rotated box?
Edit: You can have a look at the same project, maybe then it’s more clear what I mean.
Yes exactly, i think this is the result of the center of the Bounding Box not matching the center of the Box Mesh
Nevermind, working as intended now. I can just use the entity position as center for the object.