C # Programmer's Consolidation of Unity 3D notes (10): 3D mathematical model of displacement and rotation of unity3d

Source: Internet
Author: User
Tags api manual translate function

Encountered a want to do the function, but can not achieve, the core reason is because the u3d 3D mathematical concept is not flexible thoroughly understand. So again system learning-the third time to learn 3D mathematics.

This time, the features you want to implement are simple:

As in the small map, you want to be able to dynamically draw the current position of the player, Z direction: With 3 lines, z-axis forward, 30° rotation, -30° rotation.

The question is: 0 points can be obtained, P1 points? The P2 point is unknown.

I tried for 2 hours, and it turned out to be less than disappointing.

Have to, take a little time to learn 3D math again:

1-bit shift – vectors and points:
    • Points: points and vectors are mathematically consistent, the concept of real life midpoint is better understood, sit punctuation to locate – South two ring, North two ring. "dot (3,4,5) parentheses to mark, abbreviation P"
    • Vector: "Commonly known as increment", there are size and direction, such as "forward 1 step walk." Left 90 degrees. " Go ahead until you reach the wall and you stop-before you can get exactly the coordinates of the point you hit the wall or the distance between you and the wall . "vector with <3,4,5> angle brackets, referred to as V"
    • In the u3d, the unification uses the Vector3 object to express the vector and the point, this also causes the novice to be disoriented a very important reason; A skill, usually in the API with position, point, V3 is definitely a dot, all vectors, direction is a vector, read more official API manual, written very clearly.

Make a game, list out the API of Vector3 you used, analyze the use of Vector3 is vector (V) or point (P)

Demand

Api

Vector (V)

Point (P)

Note

Smooth displacement

Vector3 Movetowards (Vector3 current, Vector3 target, float maxdistancedelta);

P

Smooth displacement

Vector3 Slerp (Vector3 from, Vector3 to, float t)

P

The new coordinate point

This.m_transform.position = pos;

P

Conform to conventional thinking

Change the coordinates of one time

M_transform. Translate (New Vector3 (Rx, 0,-m_speed * time.deltatime));

V

Can be considered an increment

Seeking direction

Vector3 relativepos = this.m_transform.position–m_player.position;

V

Vector subtraction, more difficult to understand

Set new coordinate points

Linerenderer.setposition (0,this.gameobject.transform.position);

M_navagent.setdestination (this.m_Player.transform.position);

P

Conform to conventional thinking

The new location

Pointer.transform.position = Hitinfo.point + (transform.position–hitinfo.point) * 0.01F;

P, V

The superposition of force?

Ask for distance

float dis = vector3.distance (v1, v2);

Float F1 = (v1–v2). Sqrmagnitude;

float F3 = (v1–v2). magnitude;

P, V

Distance can be used.

Vector subtraction can also

To scale forward

Vector3 v3 = This.m_transform.forward * 200F;

V

The multiplication of V

Radiographic Inspection

Ray R = new Ray (source, dest);

Physics.raycast (R, out hit, M_shootmask)

V, P

Source:p

Dest:v

Add a force

Rigidbody. Addforce (0, 10, 0);

V

According to this game, you can sort out the current 4 components (including Vector 3) will be shifted:

displacement is commonly used 4 Components and Vector3 :

Component

Function

API description for the Unity holy Scriptures

Whether

Have used

Transform components

Translate function

How many distances to move an object in a direction "default local coordinate system"

or moving relative to an object.

Yes

Position property

Position of transform in world space coordinates

Yes

Rigidbody components

[Fixedupdate Function]

Velocity Property

Velocity vectors for rigid bodies

Unity official demo done used to be cool

Yes

Addforce function

Add a force to the rigid body. As a result, the rigid body will begin to move.

Yes

moveposition function

Moving rigid bodies to position

Navmeshagent components

setdestination function

Set Automatic path target point

Yes

Charactercontroller

Component

Move function

A more complex motion function, with absolute motion every time.

Yes

Simplemove function

Move a character at a certain speed

Vector3 Vector

Lerp function

The linear interpolation between the two vectors.

"Like a spring that follows a target object"

Slerp function

Spherical interpolation between two vectors

"Animated arcs between sunrise and sunset"

Movetoward function

Move the current location to the target

Same as VECTOR3.LERP, Maxdistancedelta speed limit

Smoothdamp function

Over time, gradually change a vector toward the desired target.

Some of my vector-related questions:

    • How to tell if a vector and B vector are in the same direction?
    • How to judge a vector and B vectors before, after, left, right?
    • How to determine the angle between a vector and B vector?
    • Does subtraction of vectors seem to be more useful than addition?
    • Vector.forward and This.transform.Forward both express the local coordinate system, so why are the values different?

2 rotating –quaternion, eulerangles, Quaternion.euler

Rotation in the 3D is more complex, in unity 3D is generally used quaternion to rotate, rotation only involves the concept of vector (vector direction), think about the coordinate point or 0 vector rotation is meaningless?

And according to the Unity 3D API official say, only about 7 methods or operators are more commonly used, and accounted for 99% of the probability, I have not used so far so much, I used the quaterniong about 4 functions.

Rotate commonly used 7 APIs:

Quaternion API

API description for the Unity holy Scriptures

Whether

Have used

Quaternion.lookrotation

Creates a rotation along the forward (z-axis) and the head along the upwards (Y-axis) constraint gaze. That is, create a rotation so that the z axis is facing up toward the Y axis. Commonly used is the transform. LookAt

Yes

Quaternion.angle

Returns the angle between A and B.

Quaternion.euler

Returns a rotation angle , rotates the Z-degree around the z-axis, rotates the X-degree around the x-axis, and rotates the Y-degree (in order of this) around the y-axis.

Yes

Quaternion.slerp

The spherical interpolation, interpolated from the T-value from.

Quaternion.fromtorotation

Create a rotation from fromdirection to todirection.

Quaternion.identity

Returns the identity rotation (read-only). This four-dollar number is for "No rotation": This object is completely aligned to the world or to the parent axis.

Yes

Quaternion.operator *

Rotates a rotation angle by another four-dollar number, or rotates a vector by a rotation angle

Yes

Some of my spin-related issues:

1 The difference between quaternion.lookrotation and vector3.rotatetowards?

2 The difference between Quaternion.angle and vector3.angle?

3 The difference between quaternion.lookrotation and Transform.lookat?

[Official answer: Most of the time you can use transform. LookAt instead

Quaternion.lookrotation]

4 How do I achieve 2 gameobject face-to-face, the z-axis relative?

Ax: After a week of review, repeated verification, sure enough in the system to learn 3D math, to achieve the function can be, as follows:

Reference blog:

A brief introduction to the coordinate system: C # Programmer's Consolidation of Unity 3D notes (eight): Introduction to Unity 3D coordinate system

Xuanhusung's Blog: http://www.xuanyusong.com/archives/1977

Summarize:

It is said that 80%, 90%unity 3D programmers are self-taught, most of them are reading books, actual combat video, source analysis, and the students will be born in mathematics this core class-estimated to occupy a semester, there will be homework. In order to catch up with the level of the students, spend some time, fill the math knowledge, will make oneself less go some detours. Bottom line: "3D math model is critical. “

C # Programmer's Consolidation of Unity 3D notes (10): 3D mathematical model of displacement and rotation of unity3d

Related Article

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.