# SetWorldMatrix

## Recommended Posts

Hello,

I'd like to know if there is a method to calculate the position and the rotation from a transform matrix.

In my case i calculate a transform matrix and i want to apply it to my mesh. I tried mesh.setPivotMatrix but i don't want to move the pivot, i want to move the mesh. So i have to move my mesh with mesh.position and mesh.rotation. It changes the worldmatrix of my mesh and i want this new worldmatrix equal to my transform matrix. That's why the function setWorldMatrix could be the theoretical solution of my issue.

Thanks

##### Share on other sites

you can just set the mesh._worldMatrix even if this is a bit hacky

##### Share on other sites

Well, the worldMatrix has changed but not the mesh position. How is calculate the worldMatrix? I thought it was the matrix who described a mesh position in the world space, that it was the rotation matrix (the product of the 3 Euler angles rotation matrix), the mesh.position Vector3 as the 4th column,a BABYLON.Vector3.Zero() as the 4th row complet with 1 for m[16].

If i change the mesh position, the word matrix will change but if i change the world matrix, the mesh position won't change.

The behaviour of the member mesh._worldMatrix is the same as mesh._pivotMatrix because this is only the value of the matrix that changed, they are no calculation.

mesh.setPivotMatrix(m) is different that mesh._pivotMatrix = m

I made this for test : http://jsfiddle.net/yuccai/LStwp/

you can move/rotate your mesh, get the world/pivot matrix, set the world matrix with mesh._wordMatrix = m, set the pivotMatrix with mesh.setPivotMatrix(m)

##### Share on other sites

WorldMatrix in babylon.js is built from position/rotation/scaling properties

I do not get what you want to achieve. You need to extract rotation/translation from your matrix? As You mentioned there are ways to do that but babylonjs won't infer position/rotation from a matrix

##### Share on other sites

It is better if i don't need to extract rotation/translation from my matrix, if i could use this matrix like mesh.setPivotMatrix but without moving the pivot.

If i apply 1 0 0 0 to my pivot matrix, the mesh position won't changed but we'll see the mesh in (0,0,5) if its pivot is at its

0 1 0 0                    the center of mass. The problem is that i don't want to change the intrinsic properties of my mesh like

0 0 1 5                    its pivot and if i apply a rotation around y axis, the mesh will rotate around (0,0,0) and not around itself in (0,0,5).

0 0 0 1

So i can't move directly my mesh from a transform matrix in babylonjs? Do i have to extract rotation/translation ? I tried to convert the rotation matrix into 3 euler angles but this is not precise (gimblal lock). I also tried to convert this matrix rotation into quaternion but it isn't precise too.

I've seen the bakeTransformIntoVertices method but i don't understand how it works. Could it solve my problem?

##### Share on other sites

You cannot get both

Either you set the matrix manually but then position and rotation won't be usable or you have to extract rotation/position

ANd you are right the bakeTransformIntoVertices could be a great idea:

This function takes your matrix in parameter and update directly the vertices. This will then allow you to keep using position/rotation afterwards

##### Share on other sites

There is something I don't understand...

You said that the values for mesh.position and mesh.rotation are the same before and after applying a matrix to mesh.setPivotMatrix,

but the mesh is then drawn elsewhere and the pivot point seems to have moved (if the pivot point was at start, say, in the center of the mesh, after mesh.setPivotMatrix the pivot point is elsewhere - you checked this by applying a rotation to the object, which confirm a new pivot point position -).

So it seems that mesh.setPivotMatrix defines a new location and orientation for the pivot point by changing all vertices coordinates inside the mesh.

But DK said that bakeTransformIntoVertices takes matrix in parameter and update directly the vertices. That sounds similar to me.

DK, what mesh.setPivotMatrix does exactly ? And what's the difference with bakeTransformIntoVertices ?

BTW, I'm not sure bakeTransformIntoVertices will be a usefull solution for this problem since yuccai said he do not want to change the intrinsic properties of his mesh.

Is there no simple way to apply this transform matrix directly to the mesh location and orientation without changing the vertices inside the mesh ?

##### Share on other sites

"DK, what mesh.setPivotMatrix does exactly ? And what's the difference with bakeTransformIntoVertices ?"

• bakeTransformIntoVertices : This function updates the vertices it selves
• mesh.setPivotMatrix : This defines an additional matrix which will be multiplied by worldMatrix. Vertices remains unchanged here

Both functions should produce the same visual rendering

##### Share on other sites

I tried once more time to change the world matrix and i found this : there is a visual rendering if the initial mesh position belongs to the camera frustum.

My camera is in ((0,0,0) position ; (0,0,0) rotation) and by default the mesh is in (0,0,0) too. If i change the world matrix, nothing happens but if i put the mesh in (0,0,1), the world matrix changes will affect the mesh and we'll see the mesh where it is described in the world matrix. Then if i change the mesh position or rotation, the mesh moves relative to its previous position If i apply mesh.position.z += 1, the mesh will be in (0,0,2).

I did the same thing but i put initially the mesh in (0,0,1000) and (0,0,1001). In the first case, the mesh belongs to the camera frustum and it moves, in the second case, nothing happens.

Is that behavior normal or not?

Anyway this method does not solve my problem because the world matrix changes does not effect on the mesh position. That's why i'll extract the Euler angles from the transform matrix.

##### Share on other sites

Yep this is expected but as I mentioned earlier setting directly the worldMatrix is not supported

##### Share on other sites

I find a solution to my issue.

I use a handler that i move with the method setPivotMatrix but it is the parent of my mesh. Although the handler pivot is still in (0,0,0), my mesh pivot is in the same position that the handler absolute position and i've got the rendering that i wish.

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

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.