Jump to content

Rotating a Mesh around a Pivot


ChrisR
 Share

Recommended Posts

I have searched these forums high and low. And i see alot of people ask about rotating a mesh around a set pivot point. They seem to have success, but i don't. I think the pivots were messed in the file i got, but regardless, in Babylon i want to set them.

 

When i set:

 

BABYLON.Matrix.TranslationToRef(middle.x, middle.y, middle.z, currentMesh.getPivotMatrix());
It moves the mesh on me. I haven't even rotated it yet, and the mesh changes its position. If i move the mesh back to its original position after this, the pivot is not correct. 
 
Heres my playground attempt: http://www.babylonjs-playground.com/#H7V1D
 
Could someone please tell me what i am doing wrong (I am really bad with Matrix Everything :( )
 
 
Thanks,
Link to comment
Share on other sites

Yea, when i change the pivot of the mesh, all it seems to do is change the pivot's location, but the pivot relitive to the mesh has not changed.

 

So if i have a mesh that has:

 

x = vertice

1 = pivot location

 

x  x  x         1

x      x

x      x

x      x

x  x  x

 

 

and if i change the pivots location using mesh.setPivot... all that really happens is the mesh and the pivot move together so that they are still relative distance appart. like

 

1

 

 

x  x  x  x  x

x             x

x  x  x  x  x

 

what i want it to do, is rotate around the meshes middle location. So that the pivot is now in the middle of the mesh. Like

 

x  x  x

x      x

x  1  x

x      x

x  x  x

 

I have tried to place a cube at the position i want the rotation to occur to see that the rotation spot is correct, but the mesh has moved so the pivot is still in the same spot as it was relitive to the mesh like such:

currentMesh = pickInfo.pickedMesh;													var middle = getMiddlePosition(currentMesh);cube.parent = currentMesh.parent;	cube.setAbsolutePosition(middle);cube.computeWorldMatrix(true);								var matrix = BABYLON.Matrix.Translation(cube.position.x, cube.position.y, cube.position.z);currentMesh.setPivotMatrix(matrix);

You can see it working in this example:

 

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

 

The green box will move to the new pivot location, but the rest of the mesh that you click on moves. I want the mesh to stay where it is, but pivot around its center location.

Link to comment
Share on other sites

One more example. For simplicity. 

 

http://www.babylonjs-playground.com/#H7V1D#4

 

I want the blue cube to stay still, just move its pivot location to rotate around. (rotation not include, rotation point symbolized by the green cube)

 

But when i set the blue cubes pivot to the green cubes location, the blue moves there. I don't want it to move.

Link to comment
Share on other sites

Ok, maybe i have a different thought of what a pivot is. Basically i want the mesh to rotate around that point, the pivot? In 3DS Max, you can move an objects Pivot. when doing this that is the point that the object will rotate around. So on a door you can put a pivot on the side of the object so it swings open. On a spinning top, you put the pivot in the middle so it rotates around its middle.

 

So in my example, i want the Cube to just represent my pivot point. so when i move the pivot point. i want the mesh to just set that as its pivot point.

 

Basically so it looks exactly like: http://www.babylonjs-playground.com/#H7V1D#5

 

However i didn't want to have to create a parent pivot mesh for an object every time i wanted to rotate it. I thought perhaps thats what pivot point did?

Link to comment
Share on other sites

I think my problem is it is working the way its intended. However, can i move the pivot without affecting the mesh. Like lock the mesh, move the pivot, then unlock the mesh? So that the mesh doesn't move when i move the pivot? So when i decide to rotate the pivot its in the location i set?

 

If i save position then move the meshpivot , the reapply the position, the pivot is back at its original location.

Link to comment
Share on other sites

Thanks Wingnut, It sort of is what i am looking for, but the blue mesh has moved when you set the pivot location.

 

Notice when i bring the mesh back into it. (i have temporarly disabled rotation)

 

http://www.babylonjs-playground.com/#H7V1D#7

 

So we have moved the pivot, but in doing so moves the mesh initially. I want to move the pivot, without affecting the mesh's location. So that later i can rotate it.

 

The reason is, if i rotate it without moving the pivot, it rotates around a corner of the mesh. I want to rotate around the center of the mesh.

Link to comment
Share on other sites

Might be wrong (and when it comes to BJS I often am) but I see it like this.

 

You have a mesh and a cube you want to act as a pivot for the mesh; ie you want the mesh to rotate relative to the cube but want the mesh's and cube's translations to be independent of each other.  Now I think that pivoting works because the cube is made parent to the mesh so that the mesh will rotate with respect to the cube. However this also means that any translation of the cube results in the same translation of the mesh.

 

Solution (?) only have cube as parent of mesh during rotation phase.

Rotation Phase    mesh.parent = cube    do the rotation    remove cube as parent of mesh (do not know how this is achieved mesh.parent = null ??????)
Link to comment
Share on other sites

@Deltakosh It looked promising, but doing that removes alot of information from my mesh. All textures and their UVmaps are removed along with alot of other data for the mesh.

 

@JohnK Yea, that works, its just unfortunate that to save the pivot location (the cube mesh) i will just not remove the cube as a parent. This results in me having to create a new mesh for every object in the scene that i want to rotate at a different pivot location than set in the mesh.

Link to comment
Share on other sites

@johnk that is what i had to do to get it working, but adding a new mesh for every object was something i wanted to avoid. making them instances would save some overhead i suppose. 

 

@iiceman This is exactly what i need. There is one problem that i am sure there is a solution for, my matrix understanding is bad. 

 

So when we do:

 mesh.setPivotMatrix(BABYLON.Matrix.Translation(-middle.x, -middle.y, -middle.z)); mesh.position = mesh.position.add(new BABYLON.Vector3(middle.x, middle.y, middle.z));

It works great! But what i am doing here is moving the pivot by a distance. Is there anyway to set it's location rather than translate it a distance?

 

The reason for this is if i want to rotate this object again, but perhaps at a different pivot point location, how can i determine how to translate it to the right spot? I'll try to explain using vectors :P

 

Mesh.position = 0,0,0

mesh.pivotLocation = 0,0,0

 

i move the pivot location to the middle point of the mesh which lets say is 10,10,10 in the world space so i translate it

 

Mesh.position = 0,0,0

mesh.pivotLocation = 10,10,10

 

now i want to move the mesh by 20 units on the x

 

Mesh.position = 20,0,0

mesh.pivotLocation = 30,10,10

 
now i want to rotate it again but rotate it with another object. I want the pivot point to be at that new point in the middle of the two objects. Using Vectors i could easily find out how far to translate the mesh to get it there. 
For example, new pivot is going to be at 50,50,50
 
using vectors i could easly translate the mesh by 20,40,40
 
How can this be done using Matrix's? In my example, i always know where i want to be rotating the mesh as a Vector, but the current pivotLocation is actually always stored as a Matrix.
Link to comment
Share on other sites

I don't have any considerable knowledge about the matrix stuff either, but I think you can calculate it the same way as you do with vectors (are vectors not just small matrices?) Check this out and tell me if it's already what you need, because I am not all sure I got you right :P

 

http://www.babylonjs-playground.com/#H7V1D#11

Link to comment
Share on other sites

Thanks iicenman. I looked at this for over an hour. It seemed like the perfect solution. But beyond me it won't work. For now im just going to go with adding my own pivot mesh as a parent. This has caused to many head aches.

 

Here is my headache. It still jumps around, no idea why, cause i really liked your solution. 

 

http://www.babylonjs-playground.com/#1273GZ

Link to comment
Share on other sites

http://www.babylonjs-playground.com/#1273GZ#2 

 

Any better?  (what the heck, I have to give it a shot, too)  :)  Turn off .showBoundingBox's at will, of course.

 

I think I need to re-read CR's explanation about what is wanted, though.  pivotLocation(v3) is an interesting idea, I think.  Probably setPivotPosition(v3) would be best.  hmm.  I bet there's some power coders nearby that can make that happen.

 

The solution is nearby... I can smell it.  I'm thinking on it (which usually makes projects back-slide, unfortunately).  heh  Where's that Euclid guy when we need him?  BJS provides and opens a beautiful door to the world of computer geometry.  Now if we only knew anything about geometry, eh?  (at least that's the situation for me)  :)

Link to comment
Share on other sites

@ice... I forgot!  :)  Nah, I just pulled every "move" action out of the onPointerDown...  took CR's "compensators" out.  But, I think CR has a good point.  Let's try to somehow code a setPivotPosition(vector3position) for this gentleman.  He said he could make his goals happen... if he had that.  So... hmm.  I'll ponder it.  Maybe it can be done.  If anyone else wants to run with it... go for it.   Why not?   We've written far uglier methods.   :D

 

Maybe translateTo(vector3position) ?

 

I hereby offer 13 cents and my left sock... to anyone who codes this.  Power-incentives, eh?  Maybe CR will kick-in a beer can and a ciggy butt.  How can anyone refuse an offer like that?  :)

 

Keep in mind that he doesn't want a translate DISTANCE... but instead a "put it here" (and don't move the mesh when ya do).  *shrug*

 

But, I think I understand DK's thoughts, too.  If you move the pivot point to another point in space... the mesh will have to re-orient and maybe translate... UNLESS...  you re-transform the points of the mesh... back to their prior location (before the pivot move).  (a re-bake?).  But we can do this.

 

(I'm so free-wheeling with my use of "we", eh?)  heh

 

PS: Chris... with a model that massive, I don't think you want to be doing mesh.setVertexData(originalVertexData) for every frame of a drag-move.  (Just in case you were thinking that.  That would be boggy, I think.)  You would want to drag a gizmo... and onPointerUp... kill the gizmo, place the pivot point where the gizmo was, and then do the model vertices re-baking.  Maybe.  I'm newbie at this stuff.  :)

Link to comment
Share on other sites

Well, that link still doesn't have the mesh rotating around its middle point. So it still doesn't work for me. 

 

Here is my solution that i am using in case anyone is curious: http://www.babylonjs-playground.com/#UCJPU

 

And as for @Wingnut, i agree. I will throw in a beer, ciggy butt, and on top of that i will also match is 13 cents AND a right sock to go with the left.

 

As far as I feel. Setting a pivot point position before you want to use it, seems like something that would be useful to more than just me? Whats the point of having access to the pivot point otherwise? Just rotating/translating the pivot is the same thing as rotating/translating the mesh. 

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