Jump to content

Absolute positioning


Topper
 Share

Recommended Posts

Hi folks,

I´ve been going through a lot of PGs and the BJS docu as well to figure out the best (most easy way) to place meshes in a scene.

I still don´t understand why it´s necessary to distinguish between world space an local space, and I´ve seen a lot of posts of people struggling with the same topic.

What is the easiest (and most elegant) way to position and rotate a created mesh based on one coord system (probably world) always based on a mesh´s CoG?

I just want to spend a Vector3(x, y, z) and perform some rotations after, always based on a mesh´s CoG.

I studied this PG, but I don´t understand why I have to do all the calcs (transformations) by myself: http://www.babylonjs-playground.com/#M0UJB

Is there some standard functionality i didn´t come across yet?

BR,

Markus

Link to comment
Share on other sites

Probably worth reading the documentation on rotations and pivots. Start here http://doc.babylonjs.com/babylon101/position and follow the further reading links.

35 minutes ago, Topper said:

I still don´t understand why it´s necessary to distinguish between world space an local space

In a way you answer your own question.

36 minutes ago, Topper said:

What is the easiest (and most elegant) way to position and rotate a created mesh based on one coord system (probably world) always based on a mesh´s CoG?

I just want to spend a Vector3(x, y, z) and perform some rotations after, always based on a mesh´s CoG

If you want to position a mesh based on its CoG it position will always be (0, 0, 0) since the CoG moves with the mesh and the positions of everything else in the world would have to be recalculated, so you have changed the coord system. So for positioning it is best to maintain one world coord system and allow the CoG to move in the world.

On the other hand since rotations generally take place about a CoG is is best to form axes local to the CoG, those in local space to calculate and perform rotations.

So you need two coordinate systems for the calculations. Of course Babylon.js does all this internal work for you.

 

Consider the two boxes in the following PG https://www.babylonjs-playground.com/#PBNUN7

In World Space the CoG for box1 is (0, 0, 0) is its position and the CoG for box2 is (4, 2, 3) also its position.  To rotate box2 based on box2's CoG means to rotate box2 about the point (4, 2, 3). Take a rotation about box2's CoG of PI/4 about an axis parallel to the world axis. How do you calculate the new positions of the box corners in the world space?

Well it is more straight forward to do rotations about the origin rather than an arbitrary point.  In practice the only way to do rotations about an arbitrary point is to treat that point as an origin and use a different set of axes, ie those in local space, through this point as origin.  Do the rotation in the local space and then calculate the world space for the corners.

In Babylon.js rotations are about the local axes in local space and the engine does the corner calculations for you. Hence box2.rotation.y = Math.PI/4 is a rotation about the CoG in local space.

When you need to rotate a box about some other point and not its position you need to set and use a pivot.

 

 

Link to comment
Share on other sites

Hi JohnK,

thanks for your reply.

I went throug your PG and I´ve a question on it:

When I spawn a mesh like a box or a sphere it will always be in (0, 0, 0). But when i create a more complex mesh based on an extruded 2D-shape (bunch of vector2() in an array) the mesh´s reference point is still the 2D´s reference point and not the new mesh´s (3D) CoG.

Since my workflow is like this:

- make 2D shape

- extrude 2D shape to 3D shape

- perform first rotation (PI / 2 in z (world axes)

- perform second rotation (calculated angle in y (world axis))

- move mesh in z (world axes)

- move mesh in y (world axes)

I kept sticking with the idea that it may be easier to calculate arbitrary points as "rotation axes" then using anything else.

Here you can find the example I´ve been working on:

https://www.babylonjs-playground.com/#FY8S2X#1

My challenge is to position the diagonal braces in between the two blue uprights.

I already calculated  the "rotation axes" for every singe diagonal brace (green spheres), but I keep failing in using these points as "rotation axes"

My plan was to do this "pivot point thing", since a mesh's CoG is not in (0, 0, 0) when I extrude a 2D shape.

Would it help to clone my mesh to get one with its CoG in (0, 0, 0)?

 

Thanks in advance guys,

Markus

Link to comment
Share on other sites

I think you are calculating diagonals wrong, as your beams are not long enough for the angle you have.  not to send you in the wrong direction, but here I calculated diagonals with purely Pythagorean (a^2 + b^2 = c^2).  I don't want to throw you off target and hope this is not too unrelated, but it's just moving and rotating like you have done already.
https://www.babylonjs-playground.com/#R555G1#0

Link to comment
Share on other sites

Hi,

the angels of the diagonal braces are correct and so are the braces lengths (they are still not in the middle though). Believe me. Math is not my pain. :lol::lol:

They are supposed to meet the inner side holes of the uprights  and future screws.

 

Here´s my pain:

https://www.babylonjs-playground.com/#FY8S2X#2

I position seven diagonal braces along Y.

I make BJS return the centers (Line 298) of each:

 

var meshLocalCenter = braceD.getBoundingInfo().boundingBox.center;
var meshWorldCenter = braceD.getBoundingInfo().boundingBox.centerWorld;
 
 
For every single diagonal brace I get the same values although their positions (at least in Y) are definitely different:
 
center: {X: 0 Y:-6.448263332091828 Z:-0.15}
centerWorld: {X: 0 Y:-6.448263332091828 Z:-0.15}
 
How´s that? Check it yourself. That´s what I´m not getting. :(
 
BR,
Markus

 

Link to comment
Share on other sites

That's because the world matrix isn't updated, you can force by calling mesh.computeWorldMatrix(true);

Your meshes position is not centered.  I've set the circle to be the position and added mesh.showBoundingBox = true, so you can see:
https://www.babylonjs-playground.com/#FY8S2X#3
I think you just need to adjust the positions accordingly.

Link to comment
Share on other sites

brianzinn,

you´re my man. That simple line of code did the trick.

Can you explain to me what this exactly does? Is it like "freezing prior transformations"?

I came across "setAbsolutePosition / getAbsolutePosition ". What´s the sense of this? Is the "absolute position" always in relation to the world axes?

Thanks a lot, man.

BR,

Markus

Link to comment
Share on other sites

alright - glad you got it!!  it's an optimization to evaluate world matrix once per frame.  You can read more about it here on freezing your matrices:
https://doc.babylonjs.com/how_to/optimizing_your_scene -> Reducing World Matrices Computation

absolute position is always in relation to world co-ordinates, yes.  If a mesh is not parented then it's position is already in world co-ordinates, otherwise in relation to parent mesh.  It's a bit of a learning process, so glad you got that one :) 

also, there's a sticky thread for marking solved.  cheers.

Link to comment
Share on other sites

On 2/17/2018 at 4:29 PM, JohnK said:

When you need to rotate a box about some other point and not its position you need to set and use a pivot

I used the term CoG before as that is what you were using. However a mesh will always rotate about its 'local origin' given by its position. You want it to rotate about some other point. When you work through the documentation as I suggested before you will come across pivots. http://doc.babylonjs.com/how_to/pivots3.2

Set the pivot as suggested to the CoG and it will then rotate about this point.

EDIT As you will also realise there is a difference between the 'centre' of the object as given by the bounding box and the actual CoG which because of the shape off the cross section may not be the same as its bounding box centre. There is no way in BJS to calculate the CoG you would have to do this by hand.

Link to comment
Share on other sites

Can I try it, JK?

https://www.babylonjs-playground.com/#12YQ6V#3

Hey, that's close.  :)  Look at line 80... the positioning before the bake.  divide-by 50's?  Holy cow.  I think I am pretty far lost.  :D

A guy needs to be a nuclear scientist to get THIS mesh baked into submission (into being squared-up with its world).  heh.

Unreal.  I have no idea why it is acting that way, but it sure is fascinating.  (PS:  I changed camera to z-facing)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...