当前位置:网站首页>Unity - 3D mathematics -vector3
Unity - 3D mathematics -vector3
2022-07-23 21:11:00 【Small digital media members】
Reissue yesterday's , Continue today and tomorrow 3D mathematics , Learn spoilers :25、26 Will release a different version of “ Synthetic watermelon ” Detailed explanation of , Look forward to it ! There are colored eggs at the end !
One sentence per day : Young people are Utopian , The heart is still growing towards the sun
Catalog
demand :t3 Along t1-t2 Direction of movement
Multiplication and division of vector and scalar
angle Degree And radians Radin
Conversion of angle and radian
practice : Calculate the right front of the object 30 degree ,10 Meter distant coordinates
Vector3
vector
The size of the vector
API:float dis=Vector.magnitude
Vector3 pos=this.transform.position;
// There are three forms of representing the size of a vector
float m01=Mathf.Sqrt(Mathf.Pow(pos.x,2)+Mathf.Pow(pos.y,2)+Mathf.Pow(pos.z,2));
// The formula
float m02=pos.magnitude;//API
float m03=Vector3.Distance(Vector3.zero,pos);// distance
The direction of the vector
Getting vector direction is also called “ Normalized vectors ”,“ Normalized vectors ” or “ Unit vector ”, That is, the vector is lengthened or shortened , Make the die length equal to 1
API:Vector3 n02=vector.normalized;
n02 by vector Unit vector
private void Update()
{
Vector3 pos = this.transform.position;
Vector3 n01 = pos / pos.magnitude;
Vector3 n02 = pos.normalized;
Debug.DrawLine(Vector3.zero, pos);
Debug.DrawLine(Vector3.zero, n02, Color.red);
}
Vector operations
Vector subtraction 、 Add up
Vector3 relativeDirection=t1.position-t2.position;
size : Distance between two points
Direction : Point to the subtracted vector
because t1,t2 It's the world coordinates , therefore t1-t2 The actual position is translated to the world Coordinate origin

demand :t3 Along t1-t2 Direction of movement
public Transform t1, t2, t3;
private void Update()
{
Vector3 relativeDirection = t1.position - t2.position;
if (Input.GetKeyDown(KeyCode.A))
{
t3.Translate(relativeDirection.normalized);
}
Debug.DrawLine(Vector3.zero, relativeDirection, Color.red);
}t3.Translate(relativeDirection.normalized);// Get directions , Avoid the influence of the distance between two objects on the speed
=t3.position = t3.position + relativeDirection.normalized;

Multiplication and division of vector and scalar
Multiplication : Each component of the vector is multiplied by the scalar k[x,y,z]=[kx,ky,kz]
Geometric meaning : Scale vector length
demand : There is a length of 8 Vector , How to get a length of 13 Vector
Let's take the vector normalized, cube 13
Angle measurement :
angle Degree And radians Radin
Two rays shoot out from the center of the circle , Form an included angle and an arc directly opposite to the included angle .
1 radian : When the arc length is equal to the radius of the circle , The included angle is 1 radian
Half circle =π(3.14159) radian
Conversion of angle and radian
1 radian =180 degree /π 1 angle =π/180 degree
API radian = Angle degrees *Mathf.Deg2Rad;
API angle = Radians *MAthf.Rad2Deg;
angle —> radian
float d1=60;
float r1=d1*Mathf.PI/180;
float r2=d1*Mathf.Deg2Rad;
radian —> angle
float r1=3;
float d1=r1*180/Mathf.PI;
float d2=r1*Marhf.Rad2Deg;
Trigonometric functions
The relationship between the ratio of the middle angle to the side length of a right triangle is established
It can be used according to one corner , Calculate the length of the other side
The formula :sin x=a/c cos x=b/c tan x=a.b
API:
Mathf.Sin(float radian radian );
Mathf.Cos(float radian radian );
Mathf.Tan(float radian radian );
30 degree —> radian :30*Mathf.Deg2Rad// Radian to degree conversion constant
· Given the angle x And adjacent edges a, Find the opposite side
float x = 50, b = 20;
float a = Mathf.Tan(Mathf.Deg2Rad * x) * b;
Anti trigonometric function
It can be used to calculate the angle according to the length of both sides
The formula : Anti sine arcsin a/c=x arccos b/c=x arctan a/b=x
API:
Mathf.Asin(float radin);
Mathf.Acos(float radin);
Mathf.Atan(float radin);
· Known side length a and b, Find the angle angle
float a = 30, b = 20;
float angle = Mathf.Atan(a / b) * Mathf.Rad2Deg;
practice : Calculate the right front of the object 30 degree ,10 Meter distant coordinates
Vector3 worldPoint=this.transform.TransformPoint(Vector3 point)// Convert the point from its own coordinate system to the world coordinate system
public static void DrawLine (Vector3 start, Vector3 end, Color color= Color.white, float duration= 0.0f, bool depthTest= true);// Draw a straight line between the specified start point and end point .
private void Update()
{
float x = Mathf.Sin(30 * Mathf.Deg2Rad) * 10;
float z = Mathf.Cos(30 * Mathf.Deg2Rad) * 10;
Vector3 worldPoint = transform.TransformPoint(x, 0, z);
Debug.DrawLine(this.transform.position, worldPoint, Color.blue);
}Colored eggs !!!
Three characteristics of object-oriented : encapsulation 、 inheritance 、 polymorphism
1. encapsulation : Hide object properties and implementation details , Only public interfaces , Control the access level of property reading and modification in the program , Will abstract the resulting data and behavior ( Or function ) Combination , Form an organic whole (“ class ”).( Combine data and behavior , Modify data through behavior constraint code ; Wrap some complex logic , Others only need to pass in the required parameters , You can get the results you want )
2. inheritance : That is, the subclass inherits the characteristics and behavior of the parent class , Make subclass objects ( example ) Instance fields and methods with parents , Or subclasses inherit methods from their parents , Make subclasses have the same behavior as the parent .
3. polymorphism : The ability of a behavior to have multiple forms or forms of expression . An instance of a class ( object ) The same method has different forms in different situations . Polymorphism mechanism enables objects with different internal structures to share the same external interface . signify , Although specific operations for different objects are different , But through a public class , they ( Those operations ) It can be called in the same way .
Polymorphism exists Three necessary conditions :
- Inherit
- rewrite ( After the subclass inherits the parent class, redefine the parent class method )
- The parent class reference points to the subclass object
Polymorphism is actually based on inheritance .
encapsulation Can enhance the enhanced type of data , Hide implementation details , Making code modular ; Inherit You can extend the functions of the original class , It improves the code repeatability and development efficiency , An important means to enhance software maintainability , Comply with opening and closing principle ; and polymorphic Is to achieve another purpose —— Interface reuse !
* The meaning of the opening and closing principle is : When the requirements of the application change , Without modifying the source code or binary code of the software entity , It can extend the function of the module , Make it meet new needs .
边栏推荐
- [leetcode] day101 rotating image
- 最小生成树:Kruskal
- ES6 feature: Promise (custom encapsulation)
- 1063 Set Similarity
- First acquaintance with JS (programming suitable for beginners)
- [attack and defense world web] difficulty four-star 12 point advanced question: confusion1
- 初识js(适合新手的编程)
- 221. 最大正方形 ●● & 1277. 统计全为 1 的正方形子矩阵 ●●
- Green Tao theorem (4): energy increment method
- 【攻防世界WEB】难度四星12分进阶题:FlatScience
猜你喜欢
随机推荐
Understanding of signals
1309_STM32F103上增加GPIO的翻转并用FreeRTOS调度测试
Leetcode hot topic hot52-100
Jetson nano recording stepping on the pit (it will definitely solve your problem)
现在完全不知道怎么同步
High numbers | calculation of double integral 3 | high numbers | handwritten notes
An interview question about common pitfalls in golang for range
Green-Tao 定理 (4): 能量增量方法
高数下|二重积分的计算4|高数叔|手写笔记
比较关注证券公司究竟哪个佣金最低?请问网上开户安全么?
TCP half connection queue and full connection queue (the most complete in History)
(note) learning rate setting of optimizer Adam
Chapter 3 business function development (creating clues)
最小生成树:Kruskal
对接湖南CA使用U_KEY登录
Protocol buffers 的问题和滥用
Major optimization of openim - Message loading on demand, consistent cache, uniapp Publishing
Comment présenter votre expérience de projet lors d'une entrevue
Problems and abuse of protocol buffers
SQLite数据库






![[wechat applet] do you know about applet development?](/img/3d/da58255aeb6bf6bc5021d988906bcc.png)


