# Bounding box and rotation behaviour

## Recommended Posts

Hi together,

can someone explain me the behaviour of the following:

Step 1: creation of 4 boxes:

``````let box = new BABYLON.MeshBuilder.CreateBox("0", 1, scene);
let box1 = new BABYLON.MeshBuilder.CreateBox("1", 1, scene);
let box2 = new BABYLON.MeshBuilder.CreateBox("2", 1, scene);
let box3 = new BABYLON.MeshBuilder.CreateBox("3", 1, scene);

box1.position = new BABYLON.Vector3(7,0,0);
box2.position = new BABYLON.Vector3(8,0,0);
box3.position = new BABYLON.Vector3(9,0,0);``````

Step 2: now lets group boxes 1, 2 & 3 and apply rotateAround:

``````box3.setParent(box2);
box2.setParent(box1);

box1.rotateAround(new BABYLON.Vector3(7,0,0),BABYLON.Axis.Y,Math.PI/2);``````

For me it seems that the grouped boxes are moved to (0,0,0) after the rotation. Or am I getting something wrong?

https://playground.babylonjs.com/indexStable.html#T99QF6#1

--> Also: I want to create two abstract methods

(1) Rotate an arbitrary mesh (having children) around it's "Center".

(2) Position an arbitrary mesh (having children) sucht that it's top left corner is at some target coordinates.

I always encounter problems with getting the current Boundingbox (even if I calculate it recursively by merging the bounding boxes of the children).

I almost feel like I am not understanding something entirely. How would you implement such a method?

Thanks for helping ;)

##### Share on other sites
3 hours ago, Doitr said:

I always encounter problems with getting the current Boundingbox (even if I calculate it recursively by merging the bounding boxes of the children).

) - it's what helped me figure things out.  Might be a built-in way now as that's an older PG:
http://www.babylonjs-playground.com/#QHMT1#18

From there you can rotate from anywhere.  One easy way without pivot matrices is to put a node/mesh at the desired pivot point.  Parent the container to that (container.setParent(node) then rotate that node).  Then unparent again - container.setParent(null).

##### Share on other sites
3 hours ago, Doitr said:

For me it seems that the grouped boxes ﻿are moved to (0,0,0) after the rotation. Or am I getting something wrong?

They are at (0,0,0)...  Here is the code in BabylonJS:
https://github.com/BabylonJS/Babylon.js/blob/master/src/Mesh/babylon.transformNode.ts#L679
I think it's the line this.position.addInPlace() not having a counterpart.

The way rotation on pivot points work is really interesting and the matrix math can be mind bending.  First you translate to the pivot point.  Then you rotate.  Then you translate back.  It can all be done by matrix multiplication in order, which is really important.

Here is a PG that seems to show it not working as expected - I would expect rotate to have the same result as rotateAround (since your rotateAround point and box1 center are the same..)
https://playground.babylonjs.com/indexStable.html#T99QF6#2