darcome Posted April 22, 2015 Share Posted April 22, 2015 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! Quote Link to comment Share on other sites More sharing options...
RaananW Posted April 22, 2015 Share Posted April 22, 2015 I assume you don't meanmesh.rotation = BABYLON.Vector3.Zero()I would need an explanation or a playground demo to understand... Quote Link to comment Share on other sites More sharing options...
Kilombo Posted April 22, 2015 Share Posted April 22, 2015 Do you want to keep the mesh rotated in the world and trick the system so that he thinks that those are in fact 0,0,0 ? Quote Link to comment Share on other sites More sharing options...
darcome Posted April 22, 2015 Author Share Posted April 22, 2015 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 Quote Link to comment Share on other sites More sharing options...
RaananW Posted April 22, 2015 Share Posted April 22, 2015 The only way (That I see, at least :-)) to do that is to actually update the positions and normals of the object and "reset" its properties.This is a quick-and-dirty solution, I guess this is what you were looking for?http://www.babylonjs-playground.com/#MHZVZ#1 Quote Link to comment Share on other sites More sharing options...
darcome Posted April 22, 2015 Author Share Posted April 22, 2015 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 ()? Quote Link to comment Share on other sites More sharing options...
RaananW Posted April 22, 2015 Share Posted April 22, 2015 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 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. Quote Link to comment Share on other sites More sharing options...
fenomas Posted April 23, 2015 Share Posted April 23, 2015 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 jerome 1 Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted April 23, 2015 Share Posted April 23, 2015 Alternatively, you can use bakeTransformIntoVertices:https://github.com/BabylonJS/Babylon.js/blob/master/Babylon/Mesh/babylon.mesh.ts#L856 Quote Link to comment Share on other sites More sharing options...
darcome Posted April 23, 2015 Author Share Posted April 23, 2015 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 Quote Link to comment Share on other sites More sharing options...
darcome Posted April 23, 2015 Author Share Posted April 23, 2015 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); NasimiAsl 1 Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted April 24, 2015 Share Posted April 24, 2015 Perfect! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.