I am going to learn and study the unity3d Quaternary Quaternion.
The Quaternary element is used in computer graphics to indicate the rotation of an object. In unity, x, y, z, and w represent four values.
The Quaternary element is the simplest super-plural element. The plural number is composed of a real number and an element I, where I ^ 2 =-1 ,. Likewise, the four elements are composed of real numbers plus three elements I, j, and k, and they have the following relationships: I ^ 2 = j ^ 2 = k ^ 2 = ijk =-1, each Quaternary element is a linear combination of 1, I, j, and k, that is, the Quaternary element can be expressed as a + bi + cj + dk ,.
You can refer to Baidu, Wikipedia, and other websites to learn about the specific four-element concept.
Http://baike.baidu.com/view/319754.htm
Now let's talk about how to use Quaternion in unity3D to express the rotation of an object.
For basic rotation, we can use the built-in rotation function transform. Rotate () in the script.
Function Rotate (eulerAngles: Vector3, relativeTo: Space = Space. Self): void
However, when we want to calculate the rotation angle, we need to use the ry Quaternion. I am a newbie for advanced mathematics. I can only use the simplest method to see the effect.
Quaternion has few variables and you can understand them at a glance. The only difference is that the value range of xyzw is [-]. An object does not return the initial values of all values after one week of rotation, but two weeks.
Initial Value: (0, 0, 1)
Rotate along the Y axis: 180 ° (360, 540) 720 ° (, 0, 0,-1) ° (0,-, 0) °)
Rotate along the X axis: 180 ° (-360, 540) 720 ° (, 0,-1) ° (,) °)
The method without rotation is Quaternion. identify.
Now, I want to study the functions of Quaternion.
Function
1) function ToAngleAxis (out angle: float, out axis: Vector3): void
Description
Converts a rotation to angle-axis representation
This function is used to return the Rotation angle of the object (the angle between the Z axis of the object and the Z axis of the world coordinate) and the vector of the three-dimensional rotation axis to the variables out angle and out axis.
Script:
Var a = 0.0;
Var B = Vector3.zero;
Transform. rotation. ToAngleAxis (a, B );
Input: transform. localEularAngles = (0, 0 );
Output: a = 0, B = (1, 0, 0 );
Input: transform. localEularAngles = (0, 90, 0 );
Output: a = 90, B = (0, 1 );
Input: transform. localEularAngles = (, 0, 0 );
Output: a = 90, B = (-1, 0, 0)
2) function SetFromToRotation (fromDirection: Vector3, toDirection: Vector3): void
Description
Creates a rotation which rotates from fromDirection to toDirection.
This function is used to rotate the fromDirection of an object to toDirection.
Script:
Var a: Vector3;
Var B: Vector3;
Var q: Quaternion;
Var headUpDir: Vector3;
Q. SetFromToRotation (a, B );
Transform. rotation = q;
HeadUpDir = transform. TransformDirection (Vector3.Forward );
Input: a = Vector3 (, 1); B = Vector3 (, 0) // orientation the Z axis to the Y axis
Output: q = (-0.7, 0.7,); headUpDir = (, 0)
Input: a = Vector3 (, 1); B = Vector3 (, 0) // orientation the Z axis to the X axis
Output: q = (0, 0.7, 0, 0.7); headUpDir = (, 0)
Input: a = Vector3 (, 0); B = Vector3 (, 0) // orientation the Y axis to the X axis
Output: q = (0.7,-0.7,); headUpDir = (, 1)
3) function SetLookRotation (view: Vector3, up: Vector3 = Vector3.up): void
Description
Creates a rotation that looks along forward with the head upwards along upwards
Logs an error if the forward direction is zero.
This function creates a rotation to make the Z axis orientation toward the view Y axis up. This feature reminds me of a camera lol in Maya, which is interesting for everyone.
Script:
Var obj1: Transform;
Var obj2: Transform;
Var q: Quaternion;
Q. SetLookRotation (obj1.position, obj2.position );
Transform. rotation = q;
Then you can drag obj1 and obj2 to see that the object always keeps the Z axis toward obj1 and keeps the Y axis skew at the position of obj2.
I have been playing this function for a long time.
4) function ToString (): string
Description
Returns a nicely formatted string of the Quaternion
This is generally not needed, right? Look up the dictionary while you cannot understand it ~
Class Functions
1) quaternary multiplication *
We recommend that you do not use it if you are not familiar with it.
The function is very simple. c = a * B (c, a, B, Quaternion) can be understood as Objective c = then a + then B
However, a * B and B * a have different effects.
2) = and! =
No explanation
3) static function Dot (a: Quaternion, B: Quaternion): float
Description
The dot product between two rotations
Point product, returns a float. It feels useless. Vector3.Angle () is commonly used.
4) static function AngleAxis (angle: float, axis: Vector3): Quaternion
Description
Creates a rotation which rotates angle degrees around axis.
An object rotates the angle along a specified axial axis. So is a useful function.
Script:
Var obj1: Transform;
Var obj2: Transform;
Var q: Quaternion;
// The object is rotated along the Z axis of obj2, and the angle is equal to the Z axis of obj1.
Q = Quaternion. AngleAxis (obj1.localEularAngle. z, obj2.TransformDirection (Vector3.forward ));
Transform. rotation = q;
5) static function FromToRotation (fromDirection: Vector3, toDirection: Vector3): Quaternion
Description
Creates a rotation which rotates from fromDirection to toDirection.
Usually you use this to rotate a transform so that one of its axes eg. the y-axis-follows a target direction toDirection in world space.
Similar to SetFromToRotation, the difference is that a Quaternion can be returned. It is usually used to synchronize an axial (such as the Y axis) of the transform with the toDirection in the world coordinate.
6) static function LookRotation (forward: Vector3, upwards: Vector3 = Vector3.up): Quaternion
Description
Creates a rotation that looks along forward with the head upwards along upwards
Logs an error if the forward direction is zero.
Similar to SetLootRotation, the difference is that a Quaternion can be returned.
7) static function Slerp (from: Quaternion, to: Quaternion, t: float): Quaternion
Description
Spherically interpolates from towards to by t.
From to, the moving distance is t. It is also a very common function. It is used in many ways and is difficult to control. When the two quaternion approaches, the conversion speed is slow.
Script:
Var obj1: Transform;
Var t = 0.1;
Var q: Quaternion;
// Let the object rotate in the same direction as obj1.
Q = Quaternion. Slerp (transform. rotation, obj1.rotation, t );
Transform. rotation = q;
According to my personal estimation, t may represent the ratio of the distance between from and. For this reason, I did an experiment and proved this:
Q = Quaternion. Slerp (a, B, t );
Q, a, B, Quaternion
T [0, 1]
Q = a + (B-a) * t
And the maximum valid range of t is 0 ~ 1
Script:
Var obj1: Transform;
Var obj2: Transform;
Var t = 0.1;
Var q: Quaternion;
// Place the object obj1 and obj2 in different directions, and then change t.
Q = Quaternion. Slerp (obj1.rotation, obj2.rotation, t );
Transform. rotation = q;
T + = Input. GetAxis ("horizontal") * 0.1 * Time. deltaTime;
7) static function Lerp (a: Quaternion, B: Quaternion, t: float): Quaternion
Description
Interpolates from towards to by t and normalizes the result afterwards.
This is faster than Slerp but looks worse if the rotations are far apart
It is similar to Slerp and faster than Slerp, but it looks poor if the rotation angle is far apart.
8) static function Inverse (rotation: Quaternion): Quaternion
Description
Returns the Inverse of rotation.
Returns the opposite direction of rotation.
9) static function Angle (a: Quaternion, B: Quaternion): float
Description
Returns the angle in degrees between two rotations a and B.
Calculates the angle between two rotations. Same as Vector3.Angle.
10) static function Euler (x: float, y: float, z: float): Quaternion
Description
Returns a rotation that rotates z degrees around the z axis, x degrees around the x axis, and y degrees around the y axis (in that order ).
Convert the rotation angle to the corresponding Quaternion
The above are all functions of Quaternion.
Let's talk about the application. Other applications need to be supplemented.
Slerp functions are very common functions used to generate rotation.
Static function Slerp (from: Quaternion, to: Quaternion, t: float): Quaternion
For beginners, the most difficult thing is how to use it to produce a uniform rotation. If you want to use it to generate a uniform rotation, the simplest way is to fix form and to, and then increase t at a constant speed
Script:
Var obj1: Transform;
Var obj2: Transform;
Var speed: float;
Var t = 0.1;
Var q: Quaternion;
Q = Quaternion. Slerp (obj1.rotation, obj2.rotation, t );
Transform. rotation = q;
T + = Time. deltaTime;
However, this does not solve all the problems. In many cases, from and to are not fixed, and the previous script cannot guarantee the same rotation speed at all angles. So I wrote this script to ensure that it can handle most cases.
Script:
Var target: Transform;
Var rotatespeed= 30.0;
Var t = float;
Var q: Quaternion;
Var wantedRotation = Quaternion. FromToRotation (transform. position, target. position );
T = rotateSpeed/Quaternion. Angle (transform. rotation, wantedRotation) * Time. deltaTime;
Q = Quaternion. Slerp (transform. rotation, target. rotation, t );
Transform. rotation = q;
This script ensures that the rotation speed of an object is always rotateSpeed.
In the seventh row, divide the rotation rate by the angle between the two to get a ratio.
If the angle between the coordinates and the target is X degrees, and we want to rotate to the target direction at the speed of s = 30 degrees per second, the rotation angle per second is s/X. Multiplied by the Time of each rotation, we get the tvalue for uniform rotation.