Jump to content

how do I change the center of merged meshes?


satguru
 Share

Recommended Posts

If I merge meshes using the Mesh.MergeMeshes() function I get a merged mesh whose "center" / "origin"  is located at world location (0,0,0).

I would like to change this "center" to the center of its bounding box.

How do I do this?

 

Link to comment
Share on other sites

@Raggar

That does not work.

setPivotMatrix() just changes the position of the mergedMesh..

It does not change the center of the mergedMesh.

See this playground example

http://www.babylonjs-playground.com/index2_5.html#HX58BU

Here I have two spheres.

I want to merge them together and set the center of the merged mesh to a point exactly between them . In other words set it to (0,0,1)

setPivotMatrix() just moves the merged mesh forward to (0,0,1).

 

 

Link to comment
Share on other sites

@JohnK

Nice.

The article and its playground example was very helpful.

So it seems that the pivot point of a mesh is also the center/origin point of the mesh.

And to change the pivot point

1) find the relative location of the old pivot point with respect to the new pivot point,

2) create a translation matrix using that relative location

3) set the mesh's pivot matrix to this translation matrix.

In my case I had created a new translation matrix using (0,0,1) . I should have used (0,0,-1) instead.

 

Still have a question though. Wonder if this is a bug

After setting the mesh's pivot matrix, the mesh's "position" and "absolutePosition"  becomes different.

How come?

I thought a mesh's  "position" was its location with respect to its parent frame of reference and its "absolutePosition" was its location with respect to the world frame of reference.

In my case the mesh does not have any parent. Therefore both "position" and "absolutePosition" should be the same. Right?

 

Link to comment
Share on other sites

mmmmm possibly because setting a pivot point is similar to providing a parent? Its position is the position of the pivot whereas its absolute position is its position of its original centre? Something I have not thought through yet.

Link to comment
Share on other sites

Hi guys.  I've been working an issue lately, but when I saw Satguru's 1) 2) 3) above... I immediately recognized it.... from that project.  It is a code-section involving aligning the vertical-Z axes origin-at-corner Cannon heightMap... with the vertical-Y axes origin-at-center BJS heightMap.

Anyway... here it is... starting with line 1025 - var c = center.clone();  (center is established in line 998).  Then examine code all the way down to line 1073.  It does 1) 2) 3) or close to it.

Cooooool.  None of it is MY code.  I think it is Raanan (which is better than snowin', eh?)  (ahem).

Come to think of it, there are mods by me, in there.  You may wish to ge un-modified code from preview distribution of BJS max.

Anyway, I see a substantial amount of mesh.computeWorldMatrix(true); in there, so that might be important, somehow.  Good luck.

Link to comment
Share on other sites

1 hour ago, satguru said:

Still have a question though. Wonder if this is a bug

After setting the mesh's pivot matrix, the mesh's "position" and "absolutePosition"  becomes different.

How come?

I thought a mesh's  "position" was its location with respect to its parent frame of reference and its "absolutePosition" was its location with respect to the world frame of reference.

In my case the mesh does not have any parent. Therefore both "position" and "absolutePosition" should be the same. Right?

 

I found that after setting the pivot matrix that I needed to compensate the position afterwards.  I saw that you were using a translation matrix of (0,0,1) instead of (0,0,-1).  I needed to use different translation matrices depending where I wanted to pivot on the mesh.  Change var shrinkLeft in PG from true/false to see the difference.

Does not look like mesh.computeWorldMatrix(..) has any effect on the absolute position, which I would kind of expect if it was effectively reparenting.  What I found instead was that I needed to adjust the mesh position to counter the pivot (line 76).  My next step is to test this at positions other than (0,0,0)!

http://www.babylonjs-playground.com/#7F54UH#1

Link to comment
Share on other sites

@JohnK I think you are right regards "Its position is the position of the pivot whereas its absolute position is its position of its original centre"

I did some more testing and this is what I found. Somebody let me know if I got anything wrong.

I was wrong to equate the mesh origin point to mesh pivot point.
They are different.

The mesh origin point is located at the origin of the mesh local frame of reference.
Its location in the mesh is fixed and cannot be changed(?)

The mesh pivot point is also located at the origin of the mesh local frame of reference.
But, unlike origin point, its location can be changed.
It can be changed using
a. mesh.setPivotPoint(vector3, space)  
b. mesh.setPivotMatrix(matrix)
I am assuming with setPivotMatrix(), in addition to changing location, we can also change the rotation of the pivot axes(?)

To get the pivot point location we can use
a. mesh.getPivotPosition() - will return position relative to mesh origin (not mesh parent)
b. mesh.getAbsolutePivotPosition() - will return position relative to world origin.


Now the location of the mesh itself is always (?) set using the mesh pivot point rather than the mesh origin point.
There are two ways we can set the location of the mesh
a. mesh.position = Vector3
b. mesh.setAbsolutePosition(Vectors3)
The first moves the mesh  so that its pivot point is located at the given position relative to the mesh's parent
The second moves the mesh so that its pivot point is located at the given position relative to the world origin.
Note:
a. there is no mesh.setPosition()
b. there is no mesh.absolutePosition = Vector3

To get the mesh location we can use
a. mesh.position
b. mesh.getPositionExpressedInLocalSpace()
c. mesh.absolutePosition
d. mesh.getAbsolutePosition()
The first two returns the location relative to the mesh's parent.
The last two returns the location relative to the world origin BUT
the location they return is the location of the mesh based on the mesh origin point rather than the mesh pivot point.
This is confusing because
"mesh.setAbsolutePosition(Vectors3)" sets the location using the mesh pivot point BUT
"mesh.getAbsolutePosition()" gets the location using the mesh origin point

This, thus, also explains why, even when the mesh does not have any parent, its "postion" is different form its "absolutePosition".
The mesh "position" is its location based on its pivot point
The mesh "absolutePosition" is its location based on its origin point. !!
Of course the "position" and "getPivotAbsolutePosition()" will be the same in case of no parent.

Link to comment
Share on other sites

Excellent!!  Great play-by-play reporting from the battlegrounds! 

I can foresee this becoming a playground movie. :o 

thx satgrue ;)  Nice analysis... pretty well-worded.  (Wingy slaps a "Docs Curator" badge on S's lapel, and shakes his hand.)

@JohnK you worked this issue pretty hard, once upon a time.  Did you need medical care after writing that?  :)  Spacewalking.  erf.  Makes my head hurt. 

We have localSpace and worldSpace... and I heard... next year... they might add galaxySpace and universeSpace.  wow!

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...