Search Unity

Adding up Quaternion rotating around incorrect axis

Discussion in 'Scripting' started by MaartenB, Sep 23, 2019.

  1. MaartenB

    MaartenB

    Joined:
    Nov 6, 2014
    Posts:
    67
    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.

    Code (CSharp):
    1.  
    2.         public void UpdateTransform()
    3.         {
    4.             Vector3 desiredPosition = attachedMove.transform.position;
    5.             Quaternion desiredRotation = attachedMove.transform.rotation;
    6.  
    7.             bool positionAltered = false;
    8.             bool rotationAltered = false;
    9.  
    10.             while (movementQueue.Count > 0)
    11.             {
    12.                 positionAltered = true;
    13.                 PositionSet translation = movementQueue.Dequeue();
    14.  
    15.                 if (translation.TranslationType == TranslationType.Additive)
    16.                     desiredPosition += translation.Translation;
    17.                 else if (translation.TranslationType == TranslationType.Override)
    18.                     desiredPosition = translation.Translation;
    19.             }
    20.  
    21.             while (rotationQueue.Count > 0)
    22.             {
    23.                 rotationAltered = true;
    24.                 RotationSet rotation = rotationQueue.Dequeue();
    25.  
    26.                 Debug.Log("[Maarten] Adding Rotation: " + rotation.Rotation.eulerAngles + " | Old Rotation: " + desiredRotation.eulerAngles);
    27.  
    28.                 if (rotation.TranslationType == TranslationType.Additive)
    29.                     desiredRotation *= rotation.Rotation; // This is executing.
    30.                 else if (rotation.TranslationType == TranslationType.Override)
    31.                     desiredRotation = rotation.Rotation;
    32.  
    33.                 Debug.Log("[Maarten] Current Rotation: " + desiredRotation.eulerAngles);
    34.             }
    35.  
    36.             if (attachedMove.Rigidbody)
    37.             {
    38.                 if (positionAltered)
    39.                     attachedMove.Rigidbody.MovePosition(desiredPosition); // This is also executing.
    40.  
    41.                 if (rotationAltered)
    42.                     attachedMove.Rigidbody.MoveRotation(desiredRotation);
    43.             }
    44.             else
    45.             {
    46.                 if (positionAltered)
    47.                     attachedMove.transform.position = desiredPosition;
    48.  
    49.                 if (rotationAltered)
    50.                     attachedMove.transform.rotation = desiredRotation;
    51.             }
    52.         }
    53.  
    54.  

    Thanks in advance!


    - Maarten
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    When working with Quaterions, order matters. A * B != B * A
     
  3. MaartenB

    MaartenB

    Joined:
    Nov 6, 2014
    Posts:
    67
    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?
     
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    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.
     
    MaartenB likes this.
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    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.
     
    TadaImHere and MaartenB like this.
  6. MaartenB

    MaartenB

    Joined:
    Nov 6, 2014
    Posts:
    67
    Interesting. Will try that out tomorrow. Will keep you posted!
     
  7. MaartenB

    MaartenB

    Joined:
    Nov 6, 2014
    Posts:
    67
    It's working. Great! Thanks a lot, guys!