The difference of quaternions between objects in the real world & # 8594; Unity

I am creating a unity app on PC that reads the orientation of a mobile device and rotates a game object based on the angle of the phone with the PC screen.

void Update () {
    // Set main screen orientation when left trigger is pressed
    if(leftPad.GetTrigger())
        mainScreenOrientation = InputState.orientation;

    Quaternion delta = InputState.orientation * Quaternion.Inverse(mainScreenOrientation);
    transform.localRotation = delta;
}

      

InputState.orientation is the orientation of the phone and is updated every frame. When the user first launches the application, the phone must be pointed at the screen in order to register the mainScreenOrientation.

The delta between the current phone and the PC screen orientation is calculated and set on the transform object of the .localRotation object

All quaternions are specified with y indicating magnetic north, x pointing east, and z pointing up enter image description here

The orientation of the game object is correctly set to identity after registration, but the object rotates on the wrong axis when the phone is rotated. Any idea what's wrong with this?

Edit:

It turns out there are several issues with the code

  • As Jerome mentioned, the orientations needed to be reset as the phone was used in landscape mode.

    Quaternion.Euler (0, 0, 90) * InputState.orientation

  • Unity uses left coordinates, while the input read by the sensor is right. Cancel z to convert.

The last problem I didn't solve with this implementation is a sudden jump in a corner around a certain angle. Raising the phone's height above the horizon in landscape mode causes the object to rotate 90 degrees in the z-axis.

Not sure why this is happening as I only deal with quaternions and there shouldn't be any breaks.

+3


source to share


2 answers


What sensor are you using? Accelerometer or gyroscope?

The axis used by the phone can be absolute and therefore needs to be repackaged depending on the orientation of the device to align with the 3D axis in Unity3D.



Most likely, the y and x axes may need to be switched depending on portrait or landscape orientation.

+1


source


As you are using the euler, I think you need to adjust for the zero line crossing. for example add or subtract 360 depending on which direction you rotate. This is not so important for angles> 360, but I remember that this problem was in Unity as it doesn't like negative angle values.



0


source







All Articles