Jump to content

How to "reset" rotations?


darcome
 Share

Recommended Posts

Hello everyone!

 

I hope, a simple question!

 

How can I "reset" the rotations applied to a mesh, but keep the new "position"?

 

Or in other words, how to reset the axis but keep the rotations?

 

With reset I mean back to the initial situation where as stated in a tutorial from the wiki

 

"z-axis is in front of you, y-axis on the top and x-axis on the right."

 

or even better, be able to assign new axis (just in case i may need it)

 

I hope I've been clear!

 

Thank you very much in advance!

Link to comment
Share on other sites

Let's see if this can help:

 

http://www.babylonjs-playground.com/#MHZVZ

 

in this PG I've created a cylinder, but I didn't like the way it is created, because I wanted the longest part lying on the ground, so I made some rotations to achieve it.

 

After, I created a torus, that I want as a children of the box. Well, if I do it, the rotations applied to the box are applied to the torus...

 

So, I'd like to know if there is a way to reset the rotations of the box but keeping it on the ground, because that way the torus too would be lying on the ground

 

I know that there may be other ways to do it, but this is just an example of what I mean

 

Thank you in advance for your precious help

Link to comment
Share on other sites

Yeah, that is definitely what I need!

 

And alone I would never be able to find that solution!!!

 

So, RaananW, thank you very much

 

Do you think there could be a faster solution?

 

Do you think it could be useful to create a function to add to BabylonJS?

Something like Mesh.resetAxis ()?

Link to comment
Share on other sites

Glad I could help!

 

This function actually exists! VertexData.transform (https://github.com/BabylonJS/Babylon.js/blob/master/Babylon/Mesh/babylon.mesh.vertexData.ts#L131). The only problem is... I didn't find a way to reach the geometry's vertex data object  :D If you find a way, you can use this function. But both do the same thing. 

Maybe I will try finding a way to actually store a reference to the vertex data object when creating a geometry. Must ask DK what he thinks about it.

 

Don't forget - updating the mesh's Vertex Data has its side effects. For example, and instance or cloned object you created using this mesh will also have those new position data. 

Link to comment
Share on other sites

Darcome, you get how rotations work right? Inside the mesh there are a bunch of (x/y/z) positions for each vertex, and for rendering they get multiplied by the mesh's transform, then by the mesh's parent's transform, then the grandparent's, then the world transform, camera, viewport, etc. So the best way to dynamically do stuff with rotations is usually to work with that system - adding or removing parents, or copying a transform from the parent to the child, etc.

 

So the method Raanan showed amounts to going in and replacing the mesh's data with different (rotated) data. Which is sometimes what you want, but probably not every frame, and it's naturally going to be slower for larger meshes. But depending on your scene's real needs you can probably achieve what you want just with parenting etc. For example, in the PG example the box and torus could be given a common parent container, allowing them to be rotated together without the torus inheriting the box's transformation.

 

Or alternately, if you want to parent the cylinder to the box but keep its old rotation you could give it a rotation that cancels out the box's rotation:

http://www.babylonjs-playground.com/#MHZVZ#2

Link to comment
Share on other sites

Just to be sure I'm doing the right thing:

 

for example:

mesh.rotate (BABYLON.Axis.X, 		Math.PI / 2, 		BABYLON.Space.LOCAL);mesh.rotate (BABYLON.Axis.Y, 		Math.PI / 4, 		BABYLON.Space.LOCAL);		mesh.bakeTransformIntoVertices (mesh.getWorldMatrix (true));

I tried it and the results are what I expect, but you never know... better have an answer from the master :)

Link to comment
Share on other sites

Well, I made a mistake...

 

here is the correct code, but I don't know if it is optimal:

mesh.rotate (BABYLON.Axis.X, Math.PI / 2, BABYLON.Space.LOCAL);mesh.rotate (BABYLON.Axis.Y, Math.PI / 4, BABYLON.Space.LOCAL);		var m = that.carBox.computeWorldMatrix (true);		mesh.rotate (BABYLON.Axis.Y, -(Math.PI / 4), BABYLON.Space.LOCAL);mesh.rotate (BABYLON.Axis.X, -(Math.PI / 2), BABYLON.Space.LOCAL);mesh.bakeTransformIntoVertices (m);
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...