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]

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

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).