Beginner question related to quaternions

Hi, I am having trouble understanding how quaternions work. Is there a conversion from degrees to quaternions? I was just creating a flashlight for an FPS game where the flashlight’s pitch changes with the player. I went into the MouseLook script (imported with the default FPS controller) and found this code:

void Update ()
	{
		if (axes == RotationAxes.MouseXAndY)
		{
			float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
			
			rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
			rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
			
			transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
		}
		else if (axes == RotationAxes.MouseX)
		{
			transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
		}
		else
		{
			rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
			rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
			
			transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
		}

	}

What confused me was “transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);” and “transform.Rotate(0, Input.GetAxis(“Mouse X”) * sensitivityX, 0);” because the parameters for Vector3 were (Y, X, Z) rather than (X, Y, Z). Is this because up/down pitch is based around the X axis whereas left/right yaw is based around the Y axis?


http://en.wikipedia.org/wiki/File:Rollpitchyawplain.png

Is this relationship between pitch, yaw and roll also the reason for the reference to “localEulerAngles?”

What are Euler angles? What are quaternions?

An explanation would be greatly appreciated. Thanks in advance.

The first element of the vector is the rotation about the X axis. That makes your “look” vector go up and down, so the author called it rotationY. The second element is the rotation about the Y axis, which makes your look vector go left and right, so the author called it rotationX. It is a confusing terminology - line 5 in particular just looks wrong as a result, although it functions as intended.

Euler angles are an amalgam of the 3 seperate angles that make up 3d rotation. They are then ported into a Quaternion, which is a 4 number system based off a vector an float. (read this article: Quaternions and 3D Rotations - Cprogramming.com )

The core problem with Euler’s is this: In parsing them to a Quaternion, you first rotate the X axis, then the Z axis then the Y axis. Each rotation is globally configured. (or parent configured if you are using localEulerAngle.)

So a good test is to play with a quaternion. Assume that the X,Y and Z are a directional vector, then the W is the amount that is rotated on that directional vector. use the W and S keys to control that W vector and observe the results. Should teach you alot.

Thanks for the help guys! I think that I have a better understanding now. I will read that article, too.