Rotation quaternions limit (block) degrees

Hello everyone,

I need to rotate a mesh on pointer move (not with ArcRotateCamera because I don’t want the lights to move too). So that part is working, I found a PG that uses mouse listeners to change the rotation of my mesh using quaternions.

But, I’d like to limit (block) the rotation so the mesh is never rotated more than 90° (in the example, on the y axis, the cube should stop at the pink side). That limit should work front and back.

If I try various conditions (if meshmodel.rotationQuaternion.x > 0.7 && angleX > 0), it doesn’t work on all sides. Should I use euler angles instead for my conditions?

PG here : https://www.babylonjs-playground.com/#K4UGI8#48

Any ideas how to do it?

Welcome aboard!

Yes, it will probably be easier to use Euler angles, because you only need to limit the value of rotation around the Y axis. With quaternions, you don’t have as direct a relationship between the components of the quaternion and the rotation about each axis.

Thanks @Evgeni_Popov!

I changed my PG to apply quaternions to another box, convert the transformation to euler angles but I still have trouble to block the axis y on all “x axis” : https://www.babylonjs-playground.com/#K4UGI8#51

We should not be able to go further than the pink side of the cube, it works only if you rotate slowly and it doesn’t work if you rotate “on the side” and behind. Somehow I find myself with negative values for the rotation…

Any ideas how to know easily if the cube is upside down?

Is something like that not working for you?

Unfortunately no… I’ve tried that before but the rotation is “broken” without quaternions : on the blue side, if you mouse drag down you have the pink face. But if you slide to the right to the red side, when you mouse drag down, the red side is rotating on itself.

In another words, I need an ArcRotateCamera : Babylon.js Sandbox - View glTF, glb, obj and babylon files but with the model rotating, not the camera…

One solution would be to store the angle x and update it as the angle changes but the result is not very accurate, as you can see it here: https://www.babylonjs-playground.com/#K4UGI8#53
:expressionless:

Anyone has another idea?

An awesome coworker found the solution (use addRotation instead), here is the fixed PG for those who might have the same problem : https://www.babylonjs-playground.com/#K4UGI8#85