- Home /
Representation of a Quaternion in Unity3d?
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] http://www.wolframalpha.com/input/?i=quaternion%3A+0.3+%2B0+i++%2B1+j+%2B0+k 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
Hi Fattie,
thanks a lot for your reply. I think I found my mistake and your hint
quats represent twists or rotations (they are like VERBS, not like NOUNS)
got me there. So, if I understand correct, the values x,y,z define the axis around which I rotate the object and w defines by how much I rotate. And, as I see now, the visualisation in Wolframalpha does exactly that, too.
However, it appears to me, that my idea would be an equivalent powerful representation. And a more intuitive one, at least for me. That is, x,y,z defines the orientation of the object with the last degree of freedom, which is by how much you rotate around that new orientation axis still being undefined. This could then be represented by w. I see if I can find the mathematical transformation between these two representations.
I guess the point is: why do you want to know?
Quaternions are great tools to provide non-gimbal locked rotations. They are easy to combine to create rotations from logical parts. As @Fattie says they are a 4D representation of a rotation that uses complex numbers - but I've never found the need to fully understand the math, because it just hasn't been necessary and I can imagine it beco$$anonymous$$g necessary any time soon!
Answer by aldonaletto · Oct 06, 2012 at 07:54 PM
Good point!
Unity seems to use a different representation for quaternions.
A quaternion is a convenient (from the mathematicians point of view, of course) representation of a rotation around an arbitrary axis. Most papers say that the W component is the cosine of half the rotation angle, while the XYZ components are the unit vector representing the axis multiplied by the sine of (rotation angle/2):
XYZ = sin(angle/2)*axis, W = cos(angle/2)
Unity also uses this notation,
but the rotation angle seems to be mirrored about 180 degrees
- this way, the zero rotation W should be 1 instead of 0 (the Quaternion.identity constant, which means zero rotation in Unity, is 0,0,0,1).
The functions Quaternion.AngleAxis and Quaternion.ToAngleAxis correctly convert angle/axis->quaternion and quaternion->angle/axis - at least in the Unity quaternion format.
All in all, I should agree with @Fattie: don't mess directly with quaternion components - use the Quaternion class functions instead.
EDITED: Dear God! We have been missing the obvious: W isn't the angle - it is the cosine of (rotation angle/2), and cos(0) is 1. The zero rotation quaternion should therefore be 0,0,0,1 - as it in fact is! Conclusion: Unity uses the standard quaternion format, were W is cos(angle/2) and XYZ is the rotation axis * sin(angle/2).
Right. They should never, ever have mentioned the internals of Quaternions in the documentation. I mean they don't mention the internals of, say, rigidbody.
The documentation page explains incredibly clearly that you NEVER USE THE INTERNALS of a quaternion. But evidently users continually miss that and the identical message has to be repeated here! :)
I do kind of think that it would have been wise to use different variable names than x,y,z which is bound to look like changing them should be changing the angle.
that is an incredible insight - so true. $$anonymous$$an, that's sharp, you are so right. Excellent!!!!!!!!!!!!!
You have easily won "pedagogical insight of the Quarter" award, damn
@whydoidoit: yes, using XYZ is somewhat confusing - and Unity doesn't help showing localEulerAngles in the Inspector under the caption Rotation: almost everybody (me included) gets fooled at first, modifying transform.rotation components as if they were Euler angles.