Adding up Quaternion rotating around incorrect axis

Hi everyone,

I’m trying to rotate an object by multiplying 2 quaternions with each other. The issue is that when I would like to rotate it, it rotates in the wrong axis.

I’ve added a snippet of code and hope that someone would be able to help me with this issue.

        public void UpdateTransform()
        {
            Vector3 desiredPosition = attachedMove.transform.position;
            Quaternion desiredRotation = attachedMove.transform.rotation;

            bool positionAltered = false;
            bool rotationAltered = false;

            while (movementQueue.Count > 0)
            {
                positionAltered = true;
                PositionSet translation = movementQueue.Dequeue();

                if (translation.TranslationType == TranslationType.Additive)
                    desiredPosition += translation.Translation;
                else if (translation.TranslationType == TranslationType.Override)
                    desiredPosition = translation.Translation;
            }

            while (rotationQueue.Count > 0)
            {
                rotationAltered = true;
                RotationSet rotation = rotationQueue.Dequeue();

                Debug.Log("[Maarten] Adding Rotation: " + rotation.Rotation.eulerAngles + " | Old Rotation: " + desiredRotation.eulerAngles);

                if (rotation.TranslationType == TranslationType.Additive)
                    desiredRotation *= rotation.Rotation; // This is executing.
                else if (rotation.TranslationType == TranslationType.Override)
                    desiredRotation = rotation.Rotation;

                Debug.Log("[Maarten] Current Rotation: " + desiredRotation.eulerAngles);
            }

            if (attachedMove.Rigidbody)
            {
                if (positionAltered)
                    attachedMove.Rigidbody.MovePosition(desiredPosition); // This is also executing.

                if (rotationAltered)
                    attachedMove.Rigidbody.MoveRotation(desiredRotation);
            }
            else
            {
                if (positionAltered)
                    attachedMove.transform.position = desiredPosition;

                if (rotationAltered)
                    attachedMove.transform.rotation = desiredRotation;
            }
        }

Thanks in advance!

  • Maarten

When working with Quaterions, order matters. A * B != B * A

Yeah, that’s what I read as well in another post and that’s what I did as you can see in the code.

If I remember it correctly, A is the rotation base of the object and B is the rotation that should be added, right?

Sorry, just looked at your debug.log and it looks correct. Take the first one, if it is rotated globally 90 on the X and you add a small amount to its local Y axis, that will affect the global x.

1 Like

Suppose that R is your current rotation and dY is a small rotation on the Y axis.

dY * R will slightly rotate around the global Y axis

R * dY will slightly rotate around your object’s local Y axis (which could be anywhere, depending on your starting rotation R). Imagine that you were applying the rotation to a child object that was “inheriting” the rotation R from its parent and then using dY as its local rotation value.

2 Likes

Interesting. Will try that out tomorrow. Will keep you posted!

It’s working. Great! Thanks a lot, guys!