http://blog.csdn.net/janeky/article/details/17272625
Today we're going to talk about rotation in unity. There are three main ways of doing this. Transformation matrices, four-and Euler-angles.
Defined
You can perform arbitrary 3d transformations (panning, rotating, zooming, trimming) and perspective transformations using homogeneous coordinates. Generally less used. A matrix4x4 matrix class is available in unity
The four-dollar number is the simplest super-plural. Complex numbers are composed of real numbers plus element I, where i^2 =-1. Similarly, four of dollars are composed of real numbers plus three elements I, J, K, and they are like the following relationship: i^2 = j^2 = k^2 = Ijk = 1, each four is a linear combination of 1, I, J, and K, that is, the four-tuple can generally be represented as a + bi + CJ + DK, its In A, B, C, D is a real number ". These concepts are hard to understand. Just remember that the quaternion in Unity has 4 components (X,Y,Z,W)
"A set of 3 independent angular parameters used to determine the position of a fixed-point rotating rigid body, consisting of the nutation angle θ, the precession angle (i.e., the precession angle) ψ and the rotation angle J"
In unity, Quaternion.eulerangles returns the angle of rotation, rotates the euler.z angle around the z axis, rotates euler.x degrees around the x axis, rotates euler.y degrees around the y axis
Transformation
- 1. Four meta-to-transform matrix
[CSharp]View Plaincopy
- quaternion q = quaternion.lookrotation (new Vector3 (0,0.5,1));
- matrix4x4 rot = new matrix4x4 ();
- Rot. Settrs (new Vector3 (0,0,0), Q,new Vector3 (1,1,1));
- 2. Transform matrix to four-dollar number
[CSharp]View Plaincopy
- matrix4x4 rot = new matrix4x4 ();
- Rot. Settrs (new Vector3 (0,0,0), Q,new Vector3 (1,1,1));
- Vector4 vy = rot. GetColumn (1);
- Vector4 vz = rot. GetColumn (2);
- quaternion newq = quaternion.lookrotation (new Vector3 (VZ.X,VZ.Y,VZ.Z),new Vector3 (vy.x,vy.y,vy.z));
Common functions
function Toangleaxis (out angle:float, out Axis:vector3): void
Rotate the angle around the axis axis to create a rotation
static function Angle (A:quaternion, b:quaternion): float
Returns the angle between A and B.
var Eulerangles:vector3
Returns the Euler angle that represents the rotation. Represents the angle of rotation, rotates euler.z degrees around the z axis, rotates euler.x degrees around the x-axis, rotates euler.y degrees around the y-axis (in this order).
function Setfromtorotation (Fromdirection:vector3, Todirection:vector3): void
Rotate the fromdirection of the object to the Todirection
function Setlookrotation (View:vector3, Up:vector3 = vector3.up): void
Create a rotation to move the z axis toward the view Y axis up
static function Slerp (From:quaternion, To:quaternion, t:float): quaternion
From the from range to the to, the moving distance is T
static function Lerp (A:quaternion, B:quaternion, t:float): quaternion
Similar to Slerp, and faster than slerp. But if the rotation angle is far apart, it will look bad.
Comparison
Can do a variety of complex transformations, but the learning curve is relatively large, using more memory, because the amount of data stored is larger.
Simple to understand, especially for art and planning colleagues. Operation speed and memory consumption are relatively small. There may be a universal lock problem (two axes of rotation coincident)
Avoid the problem of universal lock. Not so straightforward to understand.
Have any questions welcome to explore [email protected]
Unity Hand Tour < four >3d rotation-four yuan, Euler angle and change matrix