Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Delta Quaternion?

Discussion in 'Scripting' started by dogzerx2, Aug 6, 2012.

  1. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,962
    Hey, I'm trying to calculate the angular velocity of transforms... so I thin I need to calculate the delta quaternion of current and previous quaternion rotation... but I can't find a function that helps me calculate it? Any ideas?

    I could convert to Vector3, but is that reliable?
     
  2. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550
    Invert the old orientation and multiply it by the new orientation. Like with vectors, if you didn't have a subtraction operator, you'd use unary minus (which is a kind of inverse operator for addition): newPos - oldPos equals newPos + (-oldPos)

    Code (csharp):
    1. newOrientation * Quaternion.Inverse(oldOrientation)
    To understand this, so you can deduce it for yourself next time, imagine you have a point P in world space, and you consider it attached to this orientation and want to advance it forwards. You first undo oldOrientation, then apply newOrientation:

    Code (csharp):
    1.  
    2. Vector3 localPos = Quaternion.Inverse(oldOrientation) * oldPos;
    3. Vector3 newPos = newOrientation * localPos;
    4.  
    So by associativity (i.e. inline it and move the brackets), the overall, one-shot quaternion representing both lines at once is newOrientation * Quaternion.Inverse(oldOrientation).
     
  3. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,962
    This kind of info is gold! Thank gfoot!!!

    Still a bit confusing, multiplying Quaternions is kind of like adding vectors right? So multiplying by the inverse is like substracting?
     
    Last edited: Aug 6, 2012
  4. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550
    Sort of. It's maybe more like saying that dividing one number by another is the same as multiplying the first number by the second's reciprocal (1/x). If you want to see what's behind the analogy, look up what groups, rings, and fields are. It might be too abstract for your taste though!

    You need to be a bit careful with the analogy with scalar or vector addition though because composition of transforms is not commutative - whether you represent the transforms with matrices or quaternions, A*B is generally different to B*A. So you must be careful.
     
    grubertm likes this.
  5. brandf_unity

    brandf_unity

    Joined:
    Aug 11, 2021
    Posts:
    6
    For angular velocity you should absolutely NOT use a quaterion (from the difference between two quaternions). why? because velocity can become faster than a quaternion can represent. Your typical unit-length quaternion like found in Unity can only really represent an axis and between 0 and +/- 180 degree of rotation about that axis. After that it wraps around, whereas you don't want the velocities to wrap around. I should be able to do 2000 degree/sec, for example.

    A better representation for velocity imo is a Vector3 representing the axis of rotation and the length representing the amount of rotation.
     
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,572
    While this is true, he wanted to get the velocity from the current and previous orientation. You would not be able to determine any rotation larger than 180