Sign in to follow this  
Doitr

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);

 

image.png.aa9adc592e8fb518b6ab41d08a399093.png

 

 

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);

 

image.png.3ff487a29aaff74c5184d5b93715a1a8.png

 

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

See your self:

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 this post


Link to post
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).

Here is a PG from adam (thread:

) - 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 this post


Link to post
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

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.