How is a quaternion defined/ represented in Unity3D? Is there any documentation on this?
I learned, that Unity uses a left handed coordinate system (COS). Also, it seems, that the rotation caused by the quaternions w value around the vector defined by the three values x,y and z is counterclockwise.
However, with my limited understanding of Quaternions, I would have expected said vector to behave differently. The most intuitive quaternion representation I can think of, is that the vector x,y,z would be a metric vector in Unity’s left handed COS.
For instance, lets say my quaternion {x,y,z,w} was {1,0,0,0} I would expect my object to be not rotated at all. The x-axis would point towards the x-axis and there is not rotation around it (w = 0). Instead, Unity rotates my object around the x-axis by 180 degrees.
If the quaternion was {0,1,0,0} I would expect the objects y-axis to be aligned with the worlds x-axis.
Or, lets say my quaternion was {0,sqrt(1/2),0,sqrt(1/2)} I would expect my object to be rotated by 0.7 rounds, counterclockwise around the y-axis.
Again, my knowledge of quaternions is limited, but I have used rotation matrices for years. Currently, I have no other 3D visualisation software that I could use to see how their implementation works. I have however checked wolframalpha.com
If you search there [1] for
quaternion: 0 +1 i +0 j +0 k
quaternion: 0 +0 i +1 j +0 k
or
quaternion: sqrt(1/2) +0 i + sqrt(1/2) j +0 k
You get exactly the visualisations that I described above.
So how does Unity3D differ?
I have tried to force Unity to behave like Wolframalpha but had only limited success so far.
using UnityEngine;
using System.Collections;
public class quaternion : MonoBehaviour {
public Quaternion q;
public Vector3 a; //vector x,y,z (the complex value i,j,k in Wolframalpha
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 look = new Vector3(-a.x, -a.y, a.z);
q = Quaternion.Euler(0,90,0)* Quaternion.LookRotation(look);
transform.rotation = q;
}
}
Using the Quaternion.LookRotation function suggested elsewhere in this forum [2]. This works for simple cases like the second example {0,1,0,0} but it doesn’t let me define the w value so the third example can’t be implemented like this.
Any help is very much appreciated!
[1]
You need to have their “CDF player” plugin installed and registered for the Wolframalpha Pro trial. Its annoying I know. I’m sure there are many well documented 3D tools that use quaternions that would do the same. Any recomendations? OpenGL + some wrapper?
[2] http://answers.unity3d.com/questions/14852/Convert-vector3-to-quaternion.html