Search Unity

How do I know when a Quaternion has rotated 360 degrees in any specified axis?

Discussion in 'Scripting' started by Mycroft, Oct 31, 2014.

  1. Mycroft

    Mycroft

    Joined:
    Aug 29, 2012
    Posts:
    160
    I've been working on this problem for the afternoon and I think I'm probably missing something easy.

    I'm getting the rotation quaternion from the Input.gyro.attitude on iOS. All I want to do is keep track of the player moving around and know when the player has spun around in a circle.

    This is a different axis depending on the way the user is holding the device (and we really don't care if someone just spins in their hands) so I want to know when any axis has been rotated 360.

    Any suggestions?
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    each frame add the delta from the last rotation to this rotation to a variable, accounting for wrap if necessary.
     
  3. Mycroft

    Mycroft

    Joined:
    Aug 29, 2012
    Posts:
    160

    Is this the Dot product of the two Quaternions or does "QuatDelta = QuatA - QuatB" work?
     
    Last edited: Nov 3, 2014
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I'd have used dot but I haven't checked quaternions like this.
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Quaternion only has a multiplication ( * ) arithmetic operator defined.


    If you want the angle between two quats, there's a method defined (which does partially use the dot product).

    Quaternion.Angle
    http://docs.unity3d.com/ScriptReference/Quaternion.Angle.html

    Just track the 'last rotation' every frame so you can compare the delta (change) every frame. Get the angle between last and current. And sum those up as you go.

    You'll have to decide when to start summing them up though. May that be when they go from not moving to moving, or when they click a button, or something.
     
  6. Mycroft

    Mycroft

    Joined:
    Aug 29, 2012
    Posts:
    160
    Quaternion.Angle is any angle so it includes all 3 axis and is always a positive number; ie its the difference between the two angles with no signed direction value.

    I have been attempting to use Mathf.DeltaAngle( LastFrameQuat.eulerAngles.x, ThisFrameQuat.eulerAngles.x ), but I seem to get gimbal lock (or something like it).