Introduction to Rotation & Gimbal Lock

Notes:

Introduction to Rotation, Quaternions, Euler Angles & Gimbal Lock :

Introduction to rotation in Unity:
Rotation means changing orientation of a game object

Rotating game objects:
Steps:
- Select the game object that we want to rotate
- Click on the rotate tool (E) from the tool bar if not enabled, we find a rotate gizmo appeared at the center or around the selected game object.

Rotate gizmo:
Click & drag
- Red circle to change the x rotation
- Green circle to change the y rotation
- Blue circle to change the z rotation

Rotation in a 3D space:
Rotation of an object in a 3D space can be represented in many forms
Ex: Euler angles, Quaternions, Axis Angle etc.

Rotation property in the inspector displays orientation of a game object in the form Euler angles, but behind the scene Unity stores the orientation or rotation of a game object in the form of Quaternions.

Difference between Euler Angles and Quaternions:

Euler angles:
Euler angles represent rotation around x, y and z axis in degrees
Ex:
(90, 0, 0): object is rotated around x axis by 90 degrees
(45, 0, 0): object is rotated around x axis by 45 degrees

Euler angles are simpler and easy to understand; But Euler angles suffer from Gimbal lock problem, which prevents representing rotation under some circumstances.

Quaternions:
Quaternions represent rotation of a game object using 4 numbers x, y, z, and w.
Ex:
(0.707, 0, 0, 0.707): object is rotated around x axis by 90 degrees
(0.383, 0, 0, 0.924): object is rotated around x axis by 45 degrees

Quaternions are complex and difficult to understand; But Quaternions do not suffer from Gimbal lock problem.

Rotation Using Euler Angles:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EulerRotation : MonoBehaviour
{
public Vector3 c_rot;
public Vector3 anglesToRotate;

void Start ()
{
//c_rot = new Vector3 (0f, 90f, 0f);
this.transform.eulerAngles = c_rot;

//anglesToRotate = new Vector3 (0f, 0f, 90f);
}

// Update is called once per frame
void Update ()
{
this.transform.eulerAngles = c_rot;
c_rot += anglesToRotate * Time.deltaTime;
}
}

Rotation Using Quaternions:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class QuaternionRotation : MonoBehaviour
{
public Vector3 c_rot;
public Vector3 anglesToRotate;

void Start()
{
//c_rot = new Vector3 (0f, 90f, 0f);

Quaternion yRotation = Quaternion.AngleAxis (c_rot.y, Vector3.up);
Quaternion xRotation = Quaternion.AngleAxis (c_rot.x, Vector3.right);
Quaternion zRotation = Quaternion.AngleAxis (c_rot.z, Vector3.forward);
this.transform.rotation = yRotation * xRotation * zRotation;

//anglesToRotate = new Vector3 (0f, 0f, 90f);
}

void Update()
{
Quaternion yRotation = Quaternion.AngleAxis (anglesToRotate.y * Time.deltaTime, Vector3.up);
Quaternion xRotation = Quaternion.AngleAxis (anglesToRotate.x * Time.deltaTime, Vector3.right);
Quaternion zRotation = Quaternion.AngleAxis (anglesToRotate.z * Time.deltaTime, Vector3.forward);
this.transform.rotation = yRotation * xRotation * zRotation * this.transform.rotation;

c_rot += anglesToRotate * Time.deltaTime;
}
}