C # programmer's Unity 3D note (10): Unity3D mathematical model of displacement and Rotation

Source: Internet
Author: User
Tags api manual translate function

C # programmer's Unity 3D note (10): Unity3D mathematical model of displacement and Rotation
??

The core reason is that the concept of 3D mathematics in U3D is not flexible enough. So the system learns again-the third learning 3D mathematics.

 

This time, we hope to implement the following simple functions:

For example, in a small map, you want to dynamically draw the current position of the Player and the orientation of z: use three lines, the z axis is positive, 30 ° rotation, and-30 ° rotation.

The problem is: can be obtained, P1? Point P2 is unknown.

I tried for two hours and the results were unsatisfactory, less than frustrated.

 

You have to spend some time learning 3D mathematics:

1. Displacement-vector and point: the point and vector are consistent in mathematics. The concept of the point in real life is better understood. coordinate points are used to locate the South second ring and North second ring. [Points are marked with parentheses (3, 4, 5), or P for short]Vector: "commonly known as incremental", with a large and small direction, such as "one step forward. Turn left 90 degrees ". Go forward until you hit the wall and you stop. Before that, you cannot accurately obtain the coordinates of the points hitting the wall or the distance between you and the wall. . The vector is marked with <3, 4, 5> Angle brackets (V]In U3D, Vector3 objects are used to express vectors and points in a unified manner, which is also a very important reason for beginners to turn dizzy. One technique is to use position and Point in APIs, v3 certainly represents a point. All Vector and ction are vectors. Read the official API manual and write it clearly.

Make a game and list the Vector3 APIs you have used. analyze whether Vector3 is a vector (V) or a vertex (P)

Requirement

API

Vector (V)

Point (P)

Remarks

Smooth displacement

Vector3 MoveTowards (Vector3 current, Vector3 target, float maxDistanceDelta );

P

 

Smooth displacement

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

P

 

New coordinate point

This. m_transform.position = pos;

P

Compliant with conventional thinking

Coordinates changed once

M_transform.Translate (new Vector3 (rx, 0,-m_Speed * Time. deltaTime ));

V

Can be considered as incremental

Direction

Vector3 relativePos = this. m_transform.position-m_Player.position;

V

Vector subtraction, hard to understand

Set new coordinate points

LineRenderer. SetPosition (0, this. gameObject. transform. position );

M_NavAgent.SetDestination (this. m_Player.transform.position );

P

Compliant with conventional thinking

New location

Pointer. transform. position = hitInfo. point + (transform. position-hitInfo. point) * 0.01f;

P, V

Force superposition?

Distance

Float dis = Vector3.Distance (v1, v2 );

Float f1 = (v1-v2). sqrmagn.pdf;

Float f3 = (v1-v2). magn.pdf;

P, V

Distance can be clicked.

Vector Subtraction is also possible.

Forward Scaling

Vector3 v3 = this. m_transform.forward * 200f;

V

Multiplication of V

Ray Detection

Ray r = new Ray (source, dest );

Physics. Raycast (r, out hit, 1000, m_ShootMask)

V, P

Source: P

Dest: V

Add one force

Rigidbody. AddForce (0, 10, 0 );

V

 

 

Based on this game, we can figure out that four components (including Vector 3) are currently used for Displacement:

Displacement of commonly used four components and Vector3:

Components

Function

API description of the Unity Template

Whether

Used

Transform component

Translate Function

How long does one move an object in a certain direction? [Default local coordinate system]

Or move relative to an object

Yes

 

Position attribute

Location of the world space coordinate transform

Yes

RigidBody component

[FixedUpdate function]

Velocity attributes

Velocity vector of a rigid body

The official Unity demo Done is very cool.

Yes

 

AddForce Function

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

Yes

 

MovePosition Function

Move a rigid body to position

 

NavMeshAgent component

SetDestination Function

Set automatic Path target point

Yes

CharacterController

Components

Move Function

A more complex motion function, absolutely moving each time

Yes

 

SimpleMove Function

Move a role at a certain speed

 

Vector3 Vector

Lerp Functions

Linear interpolation between two vectors.

"Like spring, a target object following"

 
 

Slerp Functions

The spherical interpolation is between two vectors.

"Animated arc between sunrise and sunset"

 
 

MoveToward Function

Current location moves to target

Same as Vector3.Lerp, maxDistanceDelta Speed Limit

 
 

SmoothDamp Function

Over time, a vector is gradually changed to the expected target.

 

 

 

 

Some of my vector-related questions:

How can we determine whether vector A and vector B are in the same direction? How can we determine the front, back, left, and right of vector A and vector B? How can we determine the angle between vector A and vector B? Does the subtraction of vectors seem more useful than the addition method? Vector. Forward and this. transform. Forward both express the local coordinate system. Why are the values different?

 

2 rotation-Quaternion, eulerAngles, Quaternion. Euler

 

Rotation is complex in 3D. In Unity 3D, Quaternion is generally used for rotation. rotation only involves the concept of vectors (vector direction ), do you have any significance for coordinate point or zero vector rotation?

According to the official statement of the Unity 3D API, there are only seven methods or operators that are commonly used and account for 99% of the probability. So far, I have not used so many methods or operators, I have used about four Quaterniong functions.

7 APIs for rotation:

Quaternion API

API description of the Unity Template

Whether

Used

Quaternion. LookRotation

Create a rotation and follow the constraints of forward (Z axis) and the header along the upwards (Y axis. That is, create a rotation to make the Z axis orientation to the Y axis up. Commonly used is transform. LookAt.

Yes

Quaternion. Angle

Returns the angle between a and B.

 

Quaternion. Euler

Returns a rotation angle, which rotates z degrees around the z axis, x degrees around the x axis, and y degrees around the y axis (in this Order ).

Yes

Quaternion. Slerp

Spherical interpolation: interpolation between the from and (from) values.

 

Quaternion. FromToRotation

Create a rotation from fromDirection to toDirection.

 

Quaternion. identity

Returns the constant equation rotation (read-only ). This element is "non-rotating": the object is perfectly aligned with the world or its parent axis.

Yes

Quaternion. operator *

Rotate a rotation angle from another element or a vector from a rotation angle.

Yes

 

 

 

Some of my rotating problems:

1 what is the difference between Quaternion. LookRotation and Vector3.RotateTowards?

2 what is the difference between Quaternion. Angle and Vector3.Angle?

3 what is the difference between Quaternion. LookRotation and transform. LookAt?

[Official answer: you can replace it with transform. LookAt most of the time.

Quaternion. LookRotation]

4 how to implement two GameObject face to face, that is, the Z axis is relative?

 

 

 

After a week of review and verification, You can implement the following functions after studying 3D mathematics in the system:

 

 

 

Summary:

It is said that 80% and 90% Unity 3D programmers are self-taught. Most of them are reading books, practical videos, and source code analysis; students born from the cool class have the core course of 3D mathematics, which is estimated to take one semester and have homework. In order to catch up with the level of students in the course class, taking some time to complete the mathematical knowledge will reduce your detours. One sentence: "3D mathematical models are critical. "

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.