Search Unity

Phone rotation angle - relative to itself and not the world?

Discussion in 'Scripting' started by LaurynasLubys, Jul 18, 2021.

  1. LaurynasLubys

    LaurynasLubys

    Joined:
    Mar 7, 2012
    Posts:
    80
    HI, I would like to get phone rotation angles using gyroscope, relative to itself rather than world rotation.
    Is it possible ?

    E.g. if I rotate phone in x axis, I would like to get 0 - 360 angle.

    Thank you.
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,000
    Uhm I would like you take a step back and think about what that would mean. The rotation of an object in it's own coordinate space is always 0. So this request makes no sense at all. This is like asking for the direction a car is pointing relative to itself. The answer is always "forward". Even when the car has been flipped upside down, relative to itself it's not rotated at all, how could it be?

    The gyro actually is only picking up relative rotations (rotation does not equal orientation). Those relative rotations around the 3 axis of the phone are actually integrated / summed up to track the actual orientation of the phone in space. This orientation is calibrated automatically by the detected gravitational acceleration and other sources for the orientation in the real world (compass, GPS, ...)

    If you just want to track the rotation around one of the device axis, you just have to integrate the rotation rate around the axis you're interested in with your own variable. However keep in mind that the gyro is not that accurate. The current rotation rate is already an integration of the detected rotational acceleration. So there will always be tiny amounts of drift.

    So when you simply track the rotation around one axis yourself, this value does not have any reasonable relationship with the orientation in space.
     
  3. LaurynasLubys

    LaurynasLubys

    Joined:
    Mar 7, 2012
    Posts:
    80
    Thanks for reply. Accelerometer does what I want, and rotations are "relative to itself" - I can point phone to any direction but lets say value x won't change untill I rotate the phone. However, I want 0-360 (0 to 1) angles, rather than these given by accelerometer. I want to do "calibration" - reseting current angles as a zero in both axis (don't care about Y at this point), so having values that are going up or down from certain phone orientation (what accelerometer gives) does not help at all.



     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,000
    No, the accelerometer gives you the linear acceleration of the device which, yes, it is relative to it's own coordinate system. It still does not give you an orientation. What you will see as acceleration is gravity pulling down permanently. Yes, it can be used to construct a rough orientation, however it is still bound to the world. Have you even tried what happens to the x value when you hold the phone upright and in comparison when you hold the phone rotated 90° to the left or right? So for example when you're laying down on a sofa looking sideways. In this case the "x" axis points downwards. So the value of the acceleration in x will be 1g and a rotation around the local x axis won't change the value at all.

    In addition to this issue, the accelerometer detects linear acceleration. So moving the phone sideways will deflect the overall acceleration vector sideways. In your case if you assume an upright position, acceleration forwards or backwards would skew the results. Think of the vector a bit like a pendulum that is pulled down due to gravity. However when you move the point it's suspended it will deflect sideways. Though this is just a rough analogy.

    The accelerometer can be used as a rough orientation aid on one axis only. However such a system is not linear and suffers from gimbal lock (in the case I've mentioned above). So the axis you're interested has be to roughly 90° from the gravity vector. If the angle is not 90° the results would need to be compensated. The gyroscope class actually provides the linear acceleration seperated into gravity and userAcceleration which would help a bit to ignore user induced acceleration. However this system also is not perfect. From a pure relativistic view it's impossible to distinguish between gravity and user acceleration. What they use is time to calibrate those two readings and keep them roughly accurate by applying the relative rotations picked up by the gyro. Though you still have the issue that the rotation you may construct from the gravity vector still depends on the actual orientation relative to gravity.

    You can get a rotation around the x axis from the gravity vector by using Mathf.ATan2 on the y and z values off the gravity vector. See this image for reference. Note that it's a long time ago since I actually worked with Android and iOS. The device itself uses the usual right handed system while Untiy uses a left handed system. I can't remember if Unity actually converts those values from right to left handed. However the difference would just be that the rotation direction is reversed, unless some axis are actually swapped.
     
  5. LaurynasLubys

    LaurynasLubys

    Joined:
    Mar 7, 2012
    Posts:
    80
    Thanks. Now understood Accelerometer better, I forgot that values are affected by forces to the phone itself, and that it is not just angles of phone. Doesn't the same goes with the suggestion you offer? Gravity vector will be different if the phone moves itself, and that will bias the rotation? I tried Mathf.ATan2 between y and z of gravity from gyro, that doesn't give values that I understand nor can use.

    Basically I also want this:
    https://answers.unity.com/questions/1414928/how-can-i-get-the-phone-orientation.html?sort=votes


     
    Last edited: Jul 18, 2021