Unity3d-Detailed quaternion Class (ii) __ Graphics

Source: Internet
Author: User
Tags cos sin

Turn from: http://www.cnblogs.com/tgycoder/p/5106463.html


four, quaternion class static method

There are 9 static methods in Quaternion: Angle method, Dot method, Euler method, Fromtorotation method, inverse method, Lerp method, Lookrotation method, Rotatetowards method and Slerp method. The use of static methods is to call their static methods directly with class names, such as Quaternion.angle (Q1,Q2), and to do analysis under these static methods. 1, Angle method 1.1 Function Prototypes

public static float Angle (quaternion a,quaternion b);

The method can calculate the minimum angle at which two rotational states need to rotate when they reach B. 1.2 Example Demo

Using Unityengine;
Using System.Collections;

public class Angle_ts:monobehaviour {
   

    //initialization
    void Start () {

        quaternion q1 = Quatern ion.identity;
        quaternion q2 = quaternion.identity;
        Q1.eulerangles = new Vector3 (30.0f, 40.0f, 50.0f);
        FLOAT A1 = Quaternion.angle (Q1, q2);
        float a2 = 0.0f;
        Vector3 v = vector3.zero;
        Q1. Toangleaxis (out A2,out v);

        Debug.Log ("A1:" + A1);
        Debug.Log ("A2:" + A2);
        Debug.Log ("Euler angle of the Q1:" + q1.eulerangles + "Q1 Rotation:" + Q1);
        Debug.Log ("Euler angle of the Q2:" + q2.eulerangles + "Q2 Rotation:" + q2);
    }
    
    Update is called once per frame
    void Update ()}

Run results

From the output, you can see that the values of A1 and A2 are equal, that is, the angle return value is the smallest angle between the two quaternion instance conversions. 2, Dot Method-point multiply 2.1 Function Prototypes

public static float Dot (quaternion a,quaternion b);

The method can judge the relationship between A and b corresponding to Euler angle according to the result of dot multiplication.

For example, there are two quaternion instances Q1 (X1,Y1,Z1,W1) and Q2 (X2,Y2,Z2,W2), then float f = Quaternion.dot (Q1,Q2), or F = x1*x2+y1*y2+z1*z2+w1*w2, The range of the result value f is [ -1,1].

When f=+ (-) 1 o'clock, the Euler angles corresponding to the Q1 and Q2 are equal, that is, the rotational state is consistent. In particular, when f =-1, it is stated that one of the rotation rotates 360° more than the other one rotation. 2.2 Example Demo

Using Unityengine;
Using System.Collections;

public class Dot_ts:monobehaviour {public
   
    Transform A, B;
    quaternion q1 = quaternion.identity;
    quaternion q2 = quaternion.identity;
    Use this for initialization
    void Start () {

        a.eulerangles = new Vector3 (0.0f, 40.0f, 0.0f);
        B.eulerangles = new Vector3 (0.0f, 360.0f + 40.0f, 0.0f);
        q1 = a.rotation;
        q2 = b.rotation;
        float f = quaternion.dot (Q1, q2);
        
        Debug.Log ("Euler angle of the Q1:" + q1.eulerangles + "Q1 Rotation:" + Q1);
        Debug.Log ("Euler angle of the Q2:" + q2.eulerangles + "Q2 Rotation:" + Q2);
        Debug.Log ("Dot (Q1,Q2):" + f);
    }
    
    Update is called once per frame
    void Update ()}

Run output

The results show that the Euler angles of Q1 and Q2 are equal, but the rotation values are the opposite, and that the angle of the two parameters is 360 ° when the DOT's return value is-1. 3, Euler method 3.1 Function Prototypes

public static quaternion Euler (Vector3 Euler);
public static quaternion Euler (float x,float y,float z);

The method is used to return the four-quaternion q (QX,QY,QZ,QW) corresponding to the Euler angle Vector3 (Ex,ey,ez). The corresponding relationship is as follows:

Known PIover180 = 3.141592/180 = 0.0174532925f is a constant light in computer graphics, and its transformation process is as follows:

ex = ex * PIOVER180/2.0F;
EY = ey * PIOVER180/2.0F;
EZ = EZ * PIOVER180/2.0F;

QX = Mathf.sin (ex) * MATHF.COS (EY) * MATHF.COS (EZ) + Mathf.cos (ex) * Mathf.sin (EY) * Mathf.sin (EZ);
QY = Mathf.cos (ex) * Mathf.sin (EY) * MATHF.COS (EZ)-Mathf.sin (ex) * MATHF.COS (EY) * Mathf.sin (EZ);
QZ = Mathf.cos (ex) * MATHF.COS (EY) * Mathf.sin (EZ)-Mathf.sin (ex) * Mathf.sin (EY) * MATHF.COS (EZ);
QW = Mathf.cos (ex) * MATHF.COS (EY) * MATHF.COS (EZ) + Mathf.sin (ex) * Mathf.sin (EY) * Mathf.sin (EZ);
3.2 Verifying the transformation process
Using Unityengine;

Using System.Collections;
    public class Euler_ts:monobehaviour {public float ex, EY, EZ;
    Float QX, qy, QZ,QW;
    float PIover180 = 0.0174532925f;
    quaternion q = quaternion.identity;
  
    Vector3 Euler; void Ongui () {if (GUI).
            Button (New Rect (10.0f,10.0f,100.0f,45.0f), "compute")) {Euler = new Vector3 (Ex,ey,ez);
            Debug.Log ("Euler angle Euler (Ex,ey,ez):" + Euler);
            Q = Quaternion.euler (ex, EY, EZ);
            Debug.Log ("the corresponding four yuan number is:" + Q);
            Debug.Log ("q.x:" + q.x + "Q.Y:" + q.y + "q.z:" + q.z + "Q.W:" + Q.W);
            Validation algorithm ex = ex * PIOVER180/2.0F;
            EY = ey * PIOVER180/2.0F;

            EZ = EZ * PIOVER180/2.0F;
            QX = Mathf.sin (ex) * MATHF.COS (EY) * MATHF.COS (EZ) + Mathf.cos (ex) * Mathf.sin (EY) * Mathf.sin (EZ);
            QY = Mathf.cos (ex) * Mathf.sin (EY) * MATHF.COS (EZ)-Mathf.sin (ex) * MATHF.COS (EY) * Mathf.sin (EZ); QZ = Mathf.cos(ex) * MATHF.COS (EY) * Mathf.sin (EZ)-Mathf.sin (ex) * Mathf.sin (EY) * MATHF.COS (EZ);
            QW = Mathf.cos (ex) * MATHF.COS (EY) * MATHF.COS (EZ) + Mathf.sin (ex) * Mathf.sin (EY) * Mathf.sin (EZ);
        Debug.Log ("QX:" + qx + "QY:" + qy + "QZ:" + QZ + "QW:" + QW); }
    }
}

Run results

From the output can prove that the formula is correct, and the conversion of the four-yuan number of direct output, as follows:

Q = Quaternion.euler (ex, EY, EZ);
 Debug.Log ("the corresponding four yuan number is:" + Q);

The output value is rounded. 4, Fromtorotation method

Function prototypes

public static quaternion fromtorotation (Vector3 Fromdirection,vector3 todirection);

The Setfromtorotation instance methods are described earlier, and they all have the same functionality except for a slightly different usage. Using the Fromtorotation class static method, you need to call directly using the class name, such as Quaternion.fromtorotation (V1,V2);

This will not be a demonstration. 5, Inverse method 5.1 function Prototypes

public static quaternion inverse (quaternion rotation);

This method can return the inverse quaternion value of the parameter rotation.

For example rotation (X,Y,Z,W), then quaternion.inverse (rotation) = (-x,-y,-z,w). Assuming that the Euler angle of the rotation is (a,b,c), then the transform.rotation = Quaternion.inverse (rotation) corresponds to transform, which rotates-c°, x, and y axes, respectively, around its own coordinate system. ° and-z°. Because it is a transformation in a local coordinate system, the values of the Euler angles of the last transform are not necessarily equal to-a, b, or-C. 5.2 Example Demo

Using Unityengine;
Using System.Collections;

public class Invers_ts:monobehaviour {public
    Transform A, B;

    Use this for initialization
    void Start () {

        quaternion q1 = quaternion.identity;
        quaternion q2 = quaternion.identity;
        Q1.eulerangles = new Vector3 (30.0f,40.0f,50.0f);
        Q2 = Quaternion.inverse (Q1);
        a.rotation = Q1;
        b.rotation = Q2;

        Debug.Log ("Euler angle of the Q1:" + q1.eulerangles + "Q1 Rotation:" + Q1);
        Debug.Log ("Euler angle of the Q2:" + q2.eulerangles + "Q2 Rotation:" + q2);
    }
    
    Update is called once per frame
    void Update ()}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.