keywords:Unity, Linear Algebra & Plane Geometry API

Basic concept

Quaternion

Please explain Quaternions!
https://answers.unity.com/questions/645903/please-explain-quaternions.html

unity3d四元数和旋转矩阵
https://blog.csdn.net/kfqcome/article/details/10729551

unity3D 彻底搞懂 Quaternion LookRotation FromToRotation SetLookRotation方法
https://blog.csdn.net/narutojzm1/article/details/51276433

Transition

Convert Quaternion to Direction Vector

A quaternion doesn’t have a direction by itself. It is a rotation. It can be used to rotate any vector by the rotation it represents. Just multiply a Vector3 by the quaternion:

Vector3 targetForward = targetRot * Vector3.forward;
Vector3 targetUp = targetRot * Vector3.up;

Reference:
https://answers.unity.com/questions/525952/how-i-can-converting-a-quaternion-to-a-direction-v.html

Convert Direction Vector to Quaternion
Vector3 BaseDire = new Vector3(0f, 1f, 0f);
Quaternion Rot = Quaternion.LookRotation(DestDirection, BaseDire);
Convert Quaternion to Matrix4x4
Quaternion rotation = GameOjb.transform.rotation;
Matrix4x4 m = Matrix4x4.Rotate(rotation);

Reference:
https://docs.unity3d.com/ScriptReference/Matrix4x4.Rotate.html

Vector Calculating

Converting Matrix4x4 (Rotation Matrix) to Quaternion
public static Quaternion QuaternionFromMatrix(Matrix4x4 m) 
{
    return Quaternion.LookRotation(m.GetColumn(2), m.GetColumn(1)); 
}

Reference:
https://answers.unity.com/questions/11363/converting-matrix4x4-to-quaternion-vector3.html

Converting Matrix4x4 (Rotation Matrix) to Vector3 (Direction)
Matrix4x4 Matrix = Matrix4x4.Rotate(Obj.transform.rotaion);
Vector3 MoveDirection = Matrix.MultiplyVector(Vector3.forward);
Transforms direction from local space to world space
public Vector3 TransformDirection(Vector3 direction);

You should use Transform.TransformPoint for the conversion if the vector represents a position rather than a direction.

public Vector3 TransformPoint(Vector3 position);

Reference:
https://docs.unity3d.com/ScriptReference/Transform.TransformDirection.html

Transforms a direction from world space to local space
public Vector3 InverseTransformDirection(Vector3 direction);

You should use Transform.InverseTransformPoint if the vector represents a position in space rather than a direction.

public Vector3 InverseTransformPoint(Vector3 position);

Reference:
https://docs.unity3d.com/ScriptReference/Transform.InverseTransformDirection.html

Quaternion Calculating

Converts a rotation to angle-axis representation (angles in degrees).
// Extracts the angle - axis rotation from the transform rotation
float angle = 0.0f;
Vector3 axis = Vector3.zero;
transform.rotation.ToAngleAxis(out angle, out axis);

Reference:
https://docs.unity3d.com/ScriptReference/Quaternion.ToAngleAxis.html

Creates a rotation which rotates angle degrees around axis.
// Sets the transform's current rotation to a new rotation that rotates 30 degrees around the y-axis(Vector3.up)
transform.rotation = Quaternion.AngleAxis(30, Vector3.up);

Reference:
https://docs.unity3d.com/ScriptReference/Quaternion.AngleAxis.html

how do I modify Quaternion to zero rotations along certain axes?
//Reset x-axis and z-axis to zero.
Transform TraWS = CameraComp.transform;
TraWS.localEulerAngles = new Vector3(0, TraWS.localEulerAngles.y, 0);

Reference:
https://answers.unity.com/questions/159203/quaternion-equivalent-of-setting-restricting-an-ax.html

Get angle of a Quaternion in axes
Vector3 AnglesVec = RotNewRot.eulerAngles;
Get normalize direction of Quaternion in certain axis
Vector3 forwardDirAfterRoll = rollRotation * Vector3.forward;
Vector3 upDirAfterRoll = rollRotation * Vector3.up;
Vector3 rightDirAfterRoll = rollRotation * Vector3.right;

Reference:
https://stackoverflow.com/questions/57089225/normalizing-euler-angles-rotation-vector


In individuals, insanity is rare; but in groups, parties, nations and epochs, it is the rule. ― Friedrich Nietzsche