"Unity" 6.8 quaternion class (four Yuan)

Source: Internet
Author: User
Tags cos sin

Categories: Unity, C #, VS2015

Date Created: 2016-04-20 The concept of one or four dollars

The four-tuple consists of a scalar component and a three-dimensional vector component, and a four-tuple Q can be recorded as:

Q=[w, (x, Y, z)]

In 3D Mathematics, a unit of four yuan is used to represent rotation, for the three-dimensional space of the axis of rotation is n, the rotation angle is a rotating, if the four-dollar number, four components are:

W=cos (A/2)

X=sin (A/2) cos (BX)

Y=sin (A/2) cos (by)

Z=sin (A/2) cos (BZ)

The BX, by, and BZ are the x, Y, z components of the rotating axis respectively.

You can see from the above description that the rotation of the four-dollar number is not intuitive. In addition, you can use Euler angles and matrices to represent rotations. However, each representation method has its own merits and demerits, and simply compares the representations of the 3 types of rotations:

Because of the advantages and disadvantages of each of the 3 methods that represent rotation, it is necessary to choose different methods according to the actual requirements during the development process. Second, quaternion class

In unity, four of millions of dollars are represented using the quaternion class.

is a variable provided by the Quaternion class:

is a function provided by the quaternion class:

The following C # code demonstrates how to make a game object (such as a cube) rotate around the y axis:

float rotatespeed = 50f; Set the speed of rotation around the y axis

void Update ()

{

Rotation around the y-axis

Transform.rotation =quaternion.euler (0f,rotatespeed*time.time,0);

} Three, example

Transform.rotation is the rotation of the object in the world coordinate system, and transform.localrotation is the rotation of the object under the local coordinate system of the parent object, both of which have a result type of $ four. Therefore, you can set the rotation of the game object as long as you assign the result of the four-dollar number to both variables (transform.rotation or transform.localrotation).

Here are some examples of the basic usage of rotation controlled by a four-dollar number.

1. Example 1 (demo8_1_toangleaxis.unity)

This example shows how to get the angle-axis of the current rotation of the game object.

The script used in the example (AngleAxis.cs) is as follows:

usingUnityengine;usingSystem.Collections; Public classtoangleaxis:monobehaviour{ Public floatAngle =0.0f;//Rotation Angle     PublicVector3 axis = Vector3.zero;//Rotary Axis    voidStart () {Transform.rotation.ToAngleAxis ( outAngle outaxis);        print (angle);    print (axis); }}

The effect is as follows:

2. Example 2 (demo8_2_quaternionexample.unity)

The following line of code shows how to first rotate the game object to zero:

Transform.rotation = quaternion.identity;

After zeroing, the axes of the local coordinate system are parallel to the axes of the world coordinate system.

This example combines the previous examples to achieve the sun rise and fall of the simulator, while moving the object's front direction toward target, the upper direction toward the vector.up.

The effect is as follows:

3. Example 3 (demo8_3_cameralookat.unity)

This example takes the rotation of the object from the from smooth interpolation to the to, which simulates the effect that the camera's viewing direction is filtered from object A to object B.

The code is as follows (CameraLookAt.cs file):

usingUnityengine;usingSystem.Collections; Public classcameralookat:monobehaviour{ PublicTransform from;  PublicTransform to; //the time, in seconds, required for the camera to observe the direction of transition from A to B.     Public floatTrantime =20.0f; //used to record the start time    Private floatStartTime; voidStart () {startTime= Time.time;//Set start time    }    voidUpdate () {//calculate the coefficients used for interpolation        varFraccomplete = (time.time-starttime)/Trantime; //Smooth interpolationTransform.rotation = Quaternion.slerp ( from. Rotation, to.rotation, fraccomplete); } transform[] spawnpoints;}

"Unity" 6.8 quaternion class (four Yuan)

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.