Home > Software engineering >  How to correctly transform Quaternion to itself
How to correctly transform Quaternion to itself

Time:04-12

In Unity project I have to convert Quaternion class to Vector3 and then convert back into Quaternion.

So I wrote the code:

public void method(Quaternion rotation)
{
    Vector3 vector = rotation.eulerAngles;
    process(vector); // doesn't change vector
    Quaternion result = Quaternion.Euler(vector.x, vector.y, vector.z);

    if (rotation != result)
    {
        using (StreamWriter writer = new StreamWriter("Quaternions", true))
        {
            writer.Write(rotation   "\t->\t"   vector   "\t->\t"   result   "\n");
        }
    }
}

And got results means Quaternions rotation and result are not same.

(0.1, 0.0, 0.0, 1.0)    ->  (13.4, 348.3, 359.3)    ->  (0.1, -0.1, 0.0, 1.0)
(0.1, -0.1, 0.0, 1.0)   ->  (10.3, 0.1, 358.6)  ->  (-0.1, 0.0, 0.0, -1.0)
(0.1, 0.1, 0.0, 1.0)    ->  (13.2, 349.1, 359.3)    ->  (0.1, -0.1, 0.0, 1.0)
(0.1, -0.1, 0.0, 1.0)   ->  (10.3, 0.1, 358.6)  ->  (-0.1, 0.0, 0.0, -1.0)
(0.1, 0.1, 0.0, 1.0)    ->  (13.9, 346.0, 359.6)    ->  (0.1, -0.1, 0.0, 1.0)

I tried to change Quaternion.Euler(vector.x, vector.y, vector.z) to Quaternion.Euler(vector), if (rotation != result) to if (rotation.normalized != result.normalized), but nothing changed.

Please teach me how I can correctly transform Quaternion to Vector3 and back into Quaternion.

Thanks in advance.

Edit: precise version

(0.1208651, -0.1202484, 0.0108935, 0.9852986)   ->  (13.9, 346.3, 359.4)    ->  (0.1205529, -0.1181887, 0.0094058, 0.9856012)
(0.1117781, -0.0784635, 0.0001221, 0.9906307)   ->  (13.9, 346.6, 359.4)    ->  (0.1206529, -0.1155783, 0.0093524, 0.9858990)
(0.1206530, -0.1155784, 0.0093524, 0.9858992)   ->  (13.2, 349.1, 359.3)    ->  (0.1146793, -0.0939169, 0.0047323, 0.9889418)
(0.1102806, -0.0695964, -0.0030001, 0.9914563)  ->  (13.7, 347.4, 359.1)    ->  (0.1191836, -0.1079238, 0.0057138, 0.9869727)
(0.1191836, -0.1079238, 0.0057138, 0.9869727)   ->  (12.9, 350.0, 359.1)    ->  (0.1130020, -0.0854713, 0.0017047, 0.9899103)

Upd: Thank you everyone who answered! I decided to not convert Quaternion to Vector3 and back.

CodePudding user response:

first off, odd way to debug something in Unity.

And now to the question. Why are you trying to convert something to something else and then back to itself? According to your code you are not changing the created Vector3, so why not just use the original Quaternion?

EulerAngles can't really hold as much information about the rotation of a Quaternion than the Quaternion itself, so It's very likely that information can get lost or altered when working with Eulers.

Here are the two links to Unity-Documentation that you might wanna look at. The first explains, why working with EulerAngles tends to be inconsistent and the other might have a solution for whatever it is you're trying to do.

https://docs.unity3d.com/ScriptReference/Quaternion-eulerAngles.html

https://docs.unity3d.com/ScriptReference/Quaternion-operator_multiply.html

  • Related