Problems with 3D object orientation during rotation (Euler Angles)

198 Views Asked by At

I'm currently working on adding a multi-object-selection feature for a game world editor. I want to add a function that allows the user to rotate an object around the middle of all selected objects.

My problem is about calculating the orientation of the objects during the rotation. It works fine on the Y-Axis (Up) by calculating the angle between the new position, the old position and the middle of the two objects.

I add the calculated value to the Y-rotation of the object and it works!

Since this was succesful i guessed that this would work without any problems on the X and Z axis aswell! And well, it does! Except when the object is already rotated.

You'll see what i mean in this gif: https://i.imgur.com/dnzAdL0.gif

(i first rotate around the Y-axis and then around the X-axis)

It rotates by the correct amount, but around the wrong axis because the object is already rotated by 90° on the Y-axis.

How do i calculate which axis has to be rotated by which amount?

The values are in euler angles if i understood it correctly. (I'm not familar with any 3D math at all)