Search Unity

Help with converting data being used for turning

Discussion in 'Scripting' started by shaundavies, Aug 10, 2018.

  1. shaundavies

    shaundavies

    Joined:
    Jan 31, 2017
    Posts:
    44
    Hi I am importing data into unity with an MPU and have made it so it sends the data between 0-360 with its rotation.

    However when I use that info with a gameobject and the numbers hit either 0 or 360 the gameobject spins once. I was using unity's euler function for this. I figured this was gimbal lock so according to wikipedia what follows should convert my Euler angle to a quaternion. However I am getting the same problem after using this.

    Code (CSharp):
    1.     public static Quaternion Euler(float yaw, float pitch, float roll)
    2.     {
    3.         yaw *= Mathf.Deg2Rad;
    4.         pitch *= Mathf.Deg2Rad;
    5.         roll *= Mathf.Deg2Rad;
    6.  
    7.         double yawOver2 = yaw * 0.5f;
    8.         float cosYawOver2 = (float)System.Math.Cos(yawOver2);
    9.         float sinYawOver2 = (float)System.Math.Sin(yawOver2);
    10.         double pitchOver2 = pitch * 0.5f;
    11.         float cosPitchOver2 = (float)System.Math.Cos(pitchOver2);
    12.         float sinPitchOver2 = (float)System.Math.Sin(pitchOver2);
    13.         double rollOver2 = roll * 0.5f;
    14.         float cosRollOver2 = (float)System.Math.Cos(rollOver2);
    15.         float sinRollOver2 = (float)System.Math.Sin(rollOver2);
    16.         Quaternion result;
    17.         result.w = cosYawOver2 * cosPitchOver2 * cosRollOver2 + sinYawOver2 * sinPitchOver2 * sinRollOver2;
    18.         result.x = sinYawOver2 * cosPitchOver2 * cosRollOver2 + cosYawOver2 * sinPitchOver2 * sinRollOver2;
    19.         result.y = cosYawOver2 * sinPitchOver2 * cosRollOver2 - sinYawOver2 * cosPitchOver2 * sinRollOver2;
    20.         result.z = cosYawOver2 * cosPitchOver2 * sinRollOver2 - sinYawOver2 * sinPitchOver2 * cosRollOver2;
    21.  
    22.  
    23.         return result;
    24.     }
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    You are using quaternions which do not handle values larger than 180. That is unrelated to gimbal locks.

    Both quaternions and euler angles have their pros and cons. For details read the Unity help page on handling angles.

    So you could handle absolute angles that can have any numeric value at all (as you are doing above). In which case you can stick with using euler angles - with their associated limitations (including gimbal locking).

    Or you could switch the method to using relative angles in which case you can use quaternions. To do that, the method could take a current angle (quaternion) and an offset to apply to it (quaternion with angle less than 180 degrees).

    Does that help?
     
    shaundavies likes this.
  3. shaundavies

    shaundavies

    Joined:
    Jan 31, 2017
    Posts:
    44

    Yeah it does. Thanks. I am not gonna reset the degrees to 0 after a full rotation and instead sequentially add or subtract after each rotation. This approach is working in unity.