This site uses strictly necessary cookies. More Information

X- Home /

# Quaternion multiplication order

If I understand correctly multiplying two quaternions together is equivalent to applying the rotation of the first quaternion then the rotation of the second. In the Unity documentation it is stated :

`Rotating by the product lhs * rhs is the same as applying the two rotations in sequence, rhs first and then rhs`

The end of the sentence does not make sense for me. I would except either *rhs first and then lhs* or *lhs first and then rhs*. Can someone tell me which one of the two is correct ?

The documentation has been updated, and now reads: "Rotating by the product `lhs * rhs`

is the same as applying the two rotations in sequence: `lhs`

first and then `rhs`

, relative to the reference frame resulting from `lhs`

rotation." As Owen-Reynolds points out in his answer, below, it is left-to-right if you think of each transform being applied as a *local rotation*. If you want to think about applying transforms 'as if you're using the global rotation tool,' read it right-to-left ins$$anonymous$$d.

**Answer** by Owen-Reynolds
·
Oct 16, 2014 at 04:47 PM

Combining rotations (and using Quaternions) is one of those things where Unity just copies the way everyone else does it. So you can find better descriptions in general gaming-math sites.

The missing piece is local vs. global. If you have rotations `A*B`

you can think of it as applying A as a global rotation to B. Or as applying B as a local rotation to A(*).

In other words, suppose R is the current rotation, and Ry is a small spin on Y, which you would like to apply. To apply it as if you're using the global rotation tool, use `Ry*R`

. To apply the spin as if using the rotation tool set to local, use `R*Ry`

.

In other, other words. `A*B`

is B applied on local A, or global A applied to B, depending on how you want to think of it. Yes, it is very confusing until you settle on the "right" way to think of it for any particular problem.

Robot hand is a common example. To find the rotation of a hand bone, use globalRbotSpin * localShoulderSpin * localElbowSpin ... . All rotations are stored and applied locally.

(*) I always get the orders mixed up, and my Unity machine isn't here. It could be the other way -- left is local and right is global.

Your logic of *“applying A as a global rotation to B. Or as applying B as a local rotation to A”* matches my own findings. I believe you can remove the *“I always get the orders mixed up”* footnote.

Can confirm: You have the order correct. (No need for the final paragraph/disclaimer.)

**Answer** by winxalex
·
Nov 30, 2019 at 05:01 PM

SUPER SIMPLE:

```
//Note: Rotation values in Inspector are in Local space
//Let say Quaternion is something that contain information of how some 3D object is rotated
//for simplicity we will use just rotation aroundY and let say we have one parent game object and one child
//let parent is rotated 60 degree, and child is 30degree.
//as parent is first element in the world and world is not rotated it has same global and local rotation represented by Quaternions
//parentGO.transform.rotation==parentGO.transform.localRotation
//child has 60degree rotation from parent and 30 itself so it has 90 degree global rotation, and 30 local as we said
//childGO.transform.rotation.eulerAngles.y will be 90
//childGO.transform.localRotation.eulerAngles.y will be 30
//When we use multiply 2 Quaternions A * B,
**//Think Quaternions A * B as addition: of A+B=C, if A gives rotation of 60 and B of 30, A*B will give rotation of 90 degree
//Think of A as global rotation (transform.rotation) or as quaternion value, delta offset for B (delta + B)
//Think of B as local rotation (transform.localRotation) or quaternion value, delta offset for A (A + delta)**
//so to find out child global rotation
// parentGO.transform.rotation * childGO.transform.localRotation; // 60+30=90 gives you quaternion that will rotate something 90degree
// if Quaternon gives you rotation, inverse quaternion gives you minus rotation, ex. if A=60, Inverse(A)=-60
//but what if we have child global rotation which is 90 in our case, and we want to find child local rotation
//Quaternon.Inverse(childGO.transform.parent.rotation) * childGO.transform.rotation; // (-60 + 90 = 30)
//(delta + B)
//so what if we want to rotate childGO locally for 10degree more, was 30, so plus 10 will be 40
//childGO.transform.localRotation = Quaternion.AngleAxis(10, Vector3.up) * childGO.transform.localRotation; //(10+30)=40
//(A + delta)
//so what if we want to rotate childGO globally for 10degree more, was 90degree, will be 100
//childGO.transform.rotation = childGO.transform.rotation * Quaternion.AngleAxis(10, Vector3.up); //(90+10)=100
```

You seem to always have both rotations around y. That's a degenerate, easy, case where the order doesn't matter. To see what the question is asking, take any other rotations -- like around y and x.

Quite degenerative when in your answer you also have Ry. which maybe mean rotation around y,x? Right?. Order of what? A * B not equal to B*A where A,B are Quats Good answer can be found here.

If you have both rotations around y, such as Ry1 and Ry2, then Ry1*Ry2 is just adding the y-rotations. It's a super-easy special case. In math. that's called a *degenerate* case. If only 1 is around y, it's as complex as all the rest.

**Answer** by EricHFrazer
·
Apr 19, 2020 at 07:27 AM

I posted this earlier, then got so annoyed at the comments, I deleted it. I'm going to repost and try to be more concise... I'm going to get to the point first, then explain some things afterwards.

Suppose you have an object "O" and want to rotate it by a quaternion Q. You need to decide if you want it to rotate around the world axis, or the object's own local axis. (Most of the time I want it to rotate around its local axis)

```
W: O.transform.rotation = Q * O.transform.rotation <- this rotates around world axis
L: O.transform.rotation = O.transform.rotation * Q <- this rotates around its local axis
```

That's it. Those are the two forms you need to remember. The way I remember the order is because (as the docs say), when Unity runs across A mul B, the * operator override performs the A rotation first (LHS), then applies the B operation next (RHS). In my head I phrase it "gives it an extra spin at the end of everything". Iif you give it the extra spin before its normal spin, that's not going to happen "locally". That's a global (world) spin.

Another sort of non-obvious result of this is that if you have a cube at a distance from 0,0,0, and you want to rotate it in a giant circle around say the Y axis, like a planet, you can't do that by applying varying rotations on the transform, without that object having a parent, and even then you have to rotate the parent, not the object itself. If you were to try to rotate the object "around the world axis Y", it would simply spin in place around the up and down axis in the world. Not spin around 0,0,0's Y axis. Obvious to some, but not to everybody.

Somebody wrote me a private message and said, "you don't know what you're talking about wrt local rotations and parents. Nothing in Unity when it comes to setting rotations and positions matters with regards to if it has a parent or not". So polite. Well, it's true and it ain't true. Let me explain...

Put two identical objects into a scene. One of them has a parent, one of them does not. The one with a parent, the parent has some non-zero position and rotation offset. I set both the objects' *world* position and rotation to something interesting. I can do it via independently setting position and then rotation, or I could call SetPositionAndRotation. Both the objects end up in exactly the same world position and world rotation. So what's the difference? The difference is that under the hood, during the = assignment for both position and rotation, Unity peeks at where you want it to be, and calculates where it already is (by looking at the parent chain), and it calculates deltas, and sets the *local* component to whatever it needs to be in order to get the object to where it should be. This applies to position AND it applies to rotation. In fact, rotation is "a bit special". When you assign a world rotation to an object that has a parent, it will change *both* the local position and the local rotation to get the object to the exact spot you've told it to be at. I find this interesting...

the thing that is killing me personally, comprehension wise, is this: I have some stereo camera calibration rotation matrices. One for the L camera, one for the R. Both the rotation matrices are in LH coordinate systems... They tell the cameras which way to rotate compared to the headset, in order to have the images be perfect. Now, I run the images through some openCV software, which is RH-based, and determine according to openCV, that the L camera needs to be rotated an extra $$anonymous$$atrix4x4, call it $$anonymous$$, in order to be correct. I need to figure out how to translate this RH-based OpenCV matrix's angles to angles Unity can understand. I don't know the best way to decompose the $$anonymous$$ matrix into rotation angles around X, Y, and Z, and in what order. And once I have those values, I'm not quite sure how to construct a quaternion out of them in order to get that applied to the L camera object in Unity. It's a real stumper. I think OpenCV calculates 4x4 $$anonymous$$atrixes with Rout = Rz * Ry * Rx. And Unity calculates Quaternions and its 4x4 matrices using Ry * Rx * Rz. (?)

Try asking this as a separate Q. Possibly break it down, for example "convert euler zyx into Unity eulers". Forums also has a dedicated area for VR. "How to use VR software package X" seems fine for that.

You're assuming that we care about either the object's current rotation or a possible parent's. Often we don't -- we have 2 arbitrary rotations to combine. We often aren't even using the result to rotate a gameObject -- it's part of some other equation.

Your examples of "rotate an object" and "set child rotation" are just 2 of the many things we might use lhs*rhs to do.

"that by applying varying rotations on the transform, without that object having a parent, and even then you have to rotate the parent, not the object itself. If you were to try to rotate the object "around the world axis Y", it would simply spin in place around the up and down axis in the world. Not spin around 0,0,0's Y axis."

I don't think I agree? Ie if an object is at transform.position away from 0,0,0. Say its walking at the surface of a planet. Then to make it spin along the Y axis (or Vector3.up) you just need to do

```
newPosition = Quaternion.Euler(Vector3.up * angle) * transform.position;
```

You don't need a parent?

Sure, but now you're explicitly changing position, and bringing in a vector. I *think* the point was that by themselves, rotations are only around the gameObject's origin.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Quaternoin difference. (Tolerance for out of sync) 0 Answers

How do I calculate current x-axis rotation of a configurable joint and set it as target position? 0 Answers

2D Rotation of a Sprite - Quaternion.FromToRotation Smooth 1 Answer

preventing errors by calling Debug.Log on a property 1 Answer

Quaternion.Slerp not smooth! 0 Answers