Rotation makes object elevated with setPivot

Hello All,

I am trying to rotate an object along its center, for this i tried setting Pivot to center of object and it works well

However when i set Pivot to center, the object elevates to its initial position (you can check this by moving the rack)

and if i comment setPivot (Line number 490) object is not elevated and is placed direct above rack

It is all to do with the calculation of the bounding sphere center based on the passed vertex coordinates.

i see, the logic in second link will not work where y is zero, because in that case the object is not rotating on its center

can we apply some logic in first code where it does elevate but still rotates around center?

Baking the rotation to place customMesh horizontal seems to work.

However as it is a custom mesh it would probably be more appropriate to define your vertices in the horizontal plane in the first place.

Thanks that worked JohnK

I have one more query, I am now trying to rotate an object but want it rotate and move to its origin

end result that I am trying to achieve is

Is that an imported Mesh? If so, easy enough, you only need to set your center of axis to the bottom left corner of the mesh and place your mesh at zero origin before exporting. Else, you need to define/redefine your pivot so that it is placed at bottom left of your mesh before rotating. Alternatively, you can parent it to a node that already has this pivot position. Next, rotate the parent.

it is a custom mesh but cannot do any changes before or while exporting

I see. Well, I suppose the easiest would be to create a parent that will act as a pivot. Or make a pivot and displace the pivot on the mesh. I believe the parent solution might be more versatile to use. Your mesh would still have its pivot at initial if you want to make clones or instances. There’s not much of a difference in fact between one or the other solution. It might be just a matter of your preference.

Edit: Quickly did this very simple PG to illustrate the parent option. It would be about the same using a mesh.
In this scenario, you would still need to know of the width (or length) of your mesh because you need to be able to displace the parent (or pivot). Else if you do not know the width (or if it changes) you will need to rely on bounding boxes to make the calc.

1 Like

Let me try this, will keep you posted

1 Like

Hi @mawa
I somewhat tried to rotate and reposition product as in https://playground.babylonjs.com/#2FCKPI#5 (the z coordinate still needs to be recalculated)

However the boundybox does not give proper values after rotation, hence the x is not calculated properly. it should be calculated based on new width after rotation. any help there

This doesn’t look like my solution (which is a lot easier and also allows you to keep the pivot on the corner by parenting it to the ground and eventually attach other meshes to it).
Here you rely on bounding boxes and you need to account all axis. It looks like for the width you are basing on the groundWidth. But the width you want to account and divide by 2 on rotation is your test mesh (not the ground).

Edit: I also forgot to mention that the solution around the parenting (not relying on bounding boxes from vertices will also avoid any unwanted displacement on the other axis (in your case the Y axis).
I don’t know of your case and why you would choose this solution but if you do so, your imported meshes need to have their vertices and axis aligned (or you will spend your time adjusting these/hacking your own code)

@mawa i tried it, but i guess i am not understanding the parent concept properly

Let me have a look at your PG, but meanwhile for the above, I’ll try to explain:
A parent is a new node (or call it a new ‘local/world origin’) for all meshes attached to it.
When you import a mesh, by default, it zeros at the world origin of the scene and it moves and rotates around it and offsets from it’s local origin (where you place the object axis).
When you attach the mesh to a parent, the local origin of the mesh is unchanged (the pivot/axis from the imported mesh are still at the same position) BUT this local origin now zeros at the parent position and rotation (not the world). Meaning that if you rotate or move the parent on its axis, the ‘child’ will come along with it. It will no longer depend from the world origin but only from the parent.
So, once you have positioned your child relative to the parent, you can use this parent to move and rotate the child around. And you can still offset or rotate your child from its parent. This is why in your case, it can be interesting to use a parent. You can place this parent at a corner of your ground and also parent it to your ground, so that when you rotate or move your ground, the child/parent node axis will always follow and remain placed at the corner of your ground. Next, by attaching your mesh to this parent/corner axis, you can master how your mesh rotates and re-positions relatively to this parent.

Edit: Here is your updated PG using the parenting solution:

Hope this helps,

Hi @mawa, thank you
finally got it working https://playground.babylonjs.com/#2FCKPI#15
Now working on another rotation Babylon.js Playground, but i think the code needs to be tweaked here

You might have copied the wrong PG link, yes? Both links are the same.

yes, i changed now

Yup, I can see it. But then, what is it supposed to do? It seems like this one has 4 steps/iterations…
Edit: I’m sry, I don’t know if it should rotate vertically or invert on steps 3 and 4. May be you can do a mockup (very simple draw) of all 4 positions? or try to explain

nothing @mawa its done https://playground.babylonjs.com/#IR8E3Q#2 , thank you!

2 Likes

Oh, I just luv it :heart_eyes: when people solve their own problems :wink:
In which case, would you mind to just tick the box solution on your own post to let the others know you have successfully managed to solve your issue? Thanks and have an amazing day :sunglasses:

1 Like

Hi @mawa , again need your help.
check this https://playground.babylonjs.com/#SHMC9A#1 , do you see custom mesh at the the both ends? I want to attach,
for top side, top left point of mesh to parent and then for bottom left point. how can i do this?