Convert Rotation from world to local

Hi all!

I’m trying to convert rotation from world to local.

In details, there are 3 meshes, parentBox, childBox and anotherBox.
And parentBox is the parent of childBox.

What I want to do is,

1. get center world rotation using parentBox’s world rotation and anotherBox’s world rotation
2. convert center rotation from world to local (local to parentBox because I want to apply it to the childBox)
3. apply the converted local rotation to childBox

I tried the same logic with positions, and it worked well.
But with rotations, it didn’t work like I wanted…

In the playground below, the position of the childBox will be changed after 1000 ms in the way I want.
And after 2000 ms, the rotation will be changed in the way I don’t intend…

I want the childBox have green front and red up…

https://playground.babylonjs.com/#MAC3I2#10

Can somebody help me to solve this problem??

You can’t transform a vector of euler angles by a matrix. In your case, to “undo” the rotation of the parent you simply need to negate its euler angles (the `rotation` property) and then add the rotation you want for your child cube:

https://playground.babylonjs.com/#MAC3I2#15

2 Likes

Thank you @Evgeni_Popov!

Then, in the case of rotationQuaternion, do I have to convert quaternion to euler value and follow the same way above as well?

Yes, or you can do as for the translation case (where you multiply the final translation by the inverse transformation of the parent), multiplying the final quaternion you want to apply by the inverse quaternion of the parent.

1 Like

Thank you so mush for your help, @Evgeni_Popov !