# Create a box that exactly matches the bounding box after rotation

Hi. I would like to create a box every end of gizmo rotation. The box should exactly match the actual geometry bounding box after each rotation.

Here is the PG I created: https://playground.babylonjs.com/#4F33I3#632
I wrote some code comments that didn’t work well, but maybe it might help to find a solution. Thank you for your attention

You’re looking for an Object-Oriented Bounding Box (OBB) in opposite to the Axis-Aligned (AABB) Bounding Box that we calculate:

1 Like

The axis aligned bounding box is enough for me. Actually, I need to create a box mesh that matches the AABB of the other object, even if I rotate the object. I will use this box to do a simple csg and delete the box. The AABB already rotates perfectly according to my expectations but I cant make the box fit the bounding box of the merged mesh

If you just need the AABB, then the bounding box of a Mesh: BoundingBox | Babylon.js Documentation (babylonjs.com) should have all the information you need, and you can obtain it with `getBoundingInfo()`. One important detail is to ensure the world matrix is updated when you get it, by calling `computeWorldMatrix` with the `force` parameter set to true: Bounding boxes | Babylon.js Playground (babylonjs.com)

I could improve the code by hit and try since I am not sure which vector I am taking from the bounding info: https://playground.babylonjs.com/#4F33I3#633
And also I have to get the right angle to rotate the box. I feel that what I did was not right. Could you check if the line 54 makes sense? I had to add a correction factor (1.1) BABYLON.Vector3.GetAngleBetweenVectors(merged.getBoundingInfo().boundingBox.vectorsWorld[0],new BABYLON.Vector3(1,0,0),BABYLON.Vector3.Cross(extend.boundingBox.vectorsWorld[0], new BABYLON.Vector3(1,0,0)));

Have you tried using extendSizeWorld like my playground example?

I have tried the extendsizeWorld like in the first PG. If I use this vector, I have to get the components x y and z like the example you showed me. As I rotate, these components behave like a sinusoidal function. The box mesh would not fit the boundingbox after rotation

I found a solution for the angle:
var normalfacebounding = BABYLON.Vector3.Cross(merged.getBoundingInfo().boundingBox.vectorsWorld[2].subtract(merged.getBoundingInfo().boundingBox.vectorsWorld[1]),merged.getBoundingInfo().boundingBox.vectorsWorld[5].subtract(merged.getBoundingInfo().boundingBox.vectorsWorld[1])).normalize();
if(normalfacebounding.z <0){box.rotation.y= BABYLON.Vector3.GetAngleBetweenVectors(normalfacebounding,new BABYLON.Vector3(1,0,0),BABYLON.Vector3.Cross(normalfacebounding, new BABYLON.Vector3(1,0,0)).normalize());}
else{box.rotation.y= -BABYLON.Vector3.GetAngleBetweenVectors(normalfacebounding,new BABYLON.Vector3(1,0,0),BABYLON.Vector3.Cross(normalfacebounding, new BABYLON.Vector3(1,0,0)).normalize());}

However, sometimes, when I drag the rotation gizmo fast, the box mesh gets out of the bounding box. Do you know why?

1 Like