# How to set mesh.rotation correctly after a previous rotation

## Recommended Posts

Hi All,

I'm facing a problem with rotations of boxes.

Basically I need to rotate a cube by 90° around the system axes, but I noticed that when a box has a rotation set, say on x axis, if you set a rotation on z, actually you're rotating the mesh around the global system Y axis.

Does rotation property refer to a coordinate system that is "local" to the mesh ?

How can I calculate the correct x,y,z rotation with respect to the global axes ?

P.S.

I have created a simple playground for this: http://babylonjs-playground.com/#NJJPP#0

If you click once on "rotat.around x" then when you click "rotat.around.z" (more than one time to notice the rotation) you will get a rotation around global Y axis.

But if you only click on the same button multiple times (refreshing the page to reinitialize when you decide to change the button) the rotations are performed around the right axis.

##### Share on other sites

Use the Rotate function, it allows you to rotate around a space.

Take a look:

##### Share on other sites
1 hour ago, ChrisR said:

Use the Rotate function, it allows you to rotate around a space.

Take a look:

That's great!

But... is it possible to use it in animations ?

Like this?

##### Share on other sites

The example with yaw pitch and roll should work fine, but if you notice it taking strange paths to your final rotation, consider using quarternions. You can actually get something similar to gimbal lock with Euler rotations (yaw pitch roll).

This is an example of someone using quarternions in lue if euler rotation in BABYLON

It may not be necessary though, some 3d programs only use quarternions and just lie about it by input/output them as euler rotations. BABYLON may be one of those programs, i'm not sure. I also have no idea how the math behind them works, thankfully you don't need to in order to make use of them.

##### Share on other sites

All the BJS rotations are converted into Quaternions under the hood.

##### Share on other sites

9 hours ago, ChrisR said:

Like this?

This works because rotations are by 180°, if you change it to 90° the problem remains

5 hours ago, Asor said:

The example with yaw pitch and roll should work fine, but if you notice it taking strange paths to your final rotation, consider using quarternions. You can actually get something similar to gimbal lock with Euler rotations (yaw pitch roll).

This is an example of someone using quarternions in lue if euler rotation in BABYLON

It may not be necessary though, some 3d programs only use quarternions and just lie about it by input/output them as euler rotations. BABYLON may be one of those programs, i'm not sure. I also have no idea how the math behind them works, thankfully you don't need to in order to make use of them.

Thanks, I used your suggestion to use quaternion and I copied the necessary code bit from Mesh.rotate, obtaining this (the desired behavior) :

Thanks to all

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

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.